首页 > 代码库 > HDU5012 Dice 记忆化搜索

HDU5012 Dice 记忆化搜索

写错了,坑了自己好久,直接广搜可能更好,我害怕超时,记忆化搜索了一下 ,写的时候小地方写错了,


int dp[7][7][7][7][7][7];

int su,sd,sl,sr,sf,sb;

int eu,ed,el,er,ef,eb;

void init() {
	memset(dp,-1,sizeof(dp));
}

bool input() {
	while(cin>>su>>sd>>sl>>sr>>sf>>sb) {
		cin>>eu>>ed>>el>>er>>ef>>eb;
		return false;
	}
	return true;
}
bool flag;

int dfs(int up,int down,int left,int right,int front,int behind) {
	if(dp[up][down][left][right][front][behind] != -1)
		return dp[up][down][left][right][front][behind];
	if(up == eu && down == ed && left == el && right == er && front == ef && behind == eb) {
		flag = true;
		return dp[up][down][left][right][front][behind] = 0;
	}
	int tmp = inf;
	dp[up][down][left][right][front][behind] = inf;
	tmp = min(tmp,dfs(right,left,up,down,front,behind));//l
	tmp = min(tmp,dfs(left,right,down,up,front,behind));//r
	tmp = min(tmp,dfs(behind,front,left,right,up,down));//f	
	tmp = min(tmp,dfs(front,behind,left,right,down,up));//b
	return dp[up][down][left][right][front][behind] = tmp + 1;
}

void cal() {
	flag = false;
	if(su == eu && sd == ed && sl == el && sr == er && sf == ef && sb == eb){
		puts("0");return ;
	}
	dfs(su,sd,sl,sr,sf,sb);
	if(flag)
		cout<<dp[su][sd][sl][sr][sf][sb]<<endl;
	else puts("-1");
}

void output() {

}

int main() {
	while(true) {
		init();
		if(input())return 0;
		cal();
		output();
	}
	return 0;
}


/*
1 2 3 4 5 6
5 6 3 4 2 1

1 2 3 4 5 6
1 2 6 5 4 3

1 2 3 4 5 6
2 1 3 4 6 5

1 2 3 4 5 6
1 2 5 6 3 4

1 2 3 4 5 6
6 5 3 4 1 2

1 2 3 4 5 6
4 3 6 5 1 2

6 5 4 3 2 1
4 3 5 6 2 1

2 1 3 4 5 6
5 6 3 4 1 2

2 1 3 4 5 6
1 2 3 4 6 5

1 2 3 4 5 6
4 3 2 1 6 5

3 4 1 2 6 5
4 3 5 6 2 1

4 3 5 6 2 1
5 6 3 4 2 1

1 2 3 4 5 6 
1 2 4 3 6 5

ans:

1
-1
2
-1
1
2
1
1
2
3
3
1
*/



HDU5012 Dice 记忆化搜索