题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667
IDA*的入门题目,状态过多容易超内存,正好体现了IDA*的优势,每次操作移动能使一个数字进入中间的八个位置,所以构造h()=8-max(1,2,3在中间8个位置的个数)
IDA*的入门题目,状态过多容易超内存,正好体现了IDA*的优势,每次操作移动能使一个数字进入中间的八个位置,所以构造h()=8-max(1,2,3在中间8个位置的个数)
#include<cstdio>
#include<cstring>
using namespace std;
int maze[10][10]={0};
bool check(){
if(maze[3][3]!=maze[3][4]) return false;
if(maze[3][3]!=maze[3][5]) return false;
if(maze[3][3]!=maze[4][3]) return false;
if(maze[3][3]!=maze[5][3]) return false;
if(maze[3][3]!=maze[5][4]) return false;
if(maze[3][3]!=maze[5][5]) return false;
if(maze[3][3]!=maze[4][5]) return false;
return true;
}
void roA(){
int tmp=maze[1][3];
for(int i=1;i<=6;i++) maze[i][3]=maze[i+1][3];
maze[7][3]=tmp;
}
void roB(){
int tmp=maze[1][5];
for(int i=1;i<=6;i++) maze[i][5]=maze[i+1][5];
maze[7][5]=tmp;
}
void roC(){
int tmp=maze[3][7];
for(int i=7;i>=2;i--) maze[3][i]=maze[3][i-1];
maze[3][1]=tmp;
}
void roD(){
int tmp=maze[5][7];
for(int i=7;i>=2;i--) maze[5][i]=maze[5][i-1];
maze[5][1]=tmp;
}
void roE(){
int tmp=maze[7][5];
for(int i=7;i>=2;i--) maze[i][5]=maze[i-1][5];
maze[1][5]=tmp;
}
void roF(){
int tmp=maze[7][3];
for(int i=7;i>=2;i--) maze[i][3]=maze[i-1][3];
maze[1][3]=tmp;
}
void roG(){
int tmp=maze[5][1];
for(int i=1;i<=6;i++) maze[5][i]=maze[5][i+1];
maze[5][7]=tmp;
}
void roH(){
int tmp=maze[3][1];
for(int i=1;i<=6;i++) maze[3][i]=maze[3][i+1];
maze[3][7]=tmp;
}
int h(){
int a[4]={0};
for(int i=3;i<=5;i++){
a[maze[3][i]]++;
a[maze[5][i]]++;
}
a[maze[4][3]]++;
a[maze[4][5]]++;
int max=0;
for(int i=1;i<=3;i++){
if(max<a[i]) max=a[i];
}
return 8-max;
}
char ans[100];
int deep;
bool dfs(int d){
if(d==deep) return check();
if(h()+d>deep) return false;
roA();
ans[d]='A';
if(dfs(d+1)) return true;
roF();
roB();
ans[d]='B';
if(dfs(d+1)) return true;
roE();
roC();
ans[d]='C';
if(dfs(d+1)) return true;
roH();
roD();
ans[d]='D';
if(dfs(d+1)) return true;
roG();
roE();
ans[d]='E';
if(dfs(d+1)) return true;
roB();
roF();
ans[d]='F';
if(dfs(d+1)) return true;
roA();
roG();
ans[d]='G';
if(dfs(d+1)) return true;
roD();
roH();
ans[d]='H';
if(dfs(d+1)) return true;
roC();
return false;
}
int main(){
for(int i=1;i<=7;i++) maze[3][i]=maze[5][i]=1;
for(int i=1;i<=7;i++) maze[i][3]=maze[i][5]=1;
while(true){
for(int i=1;i<=7;i++){
for(int j=1;j<=7;j++){
if(maze[i][j]==0) continue;
scanf("%d",&maze[i][j]);
if(maze[i][j]==0) return 0;
}
}
if(check()){
puts("No moves needed");
printf("%d\n",maze[3][3]);
continue;
}
deep=1;
while(true){
if(dfs(0)) break;
deep++;
}
for(int i=0;i<deep;i++) putchar(ans[i]);
puts("");
printf("%d\n",maze[3][3]);
}
return 0;
}