- 搜索
- 2017-02-13 22:40:31 @
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int dx[9][9]={
{1,1,0,1,1,0,0,0,0},
{1,1,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0},
{1,0,0,1,0,0,1,0,0},
{0,1,0,1,1,1,0,1,0},
{0,0,1,0,0,1,0,0,1},
{0,0,0,1,1,0,1,1,0},
{0,0,0,0,0,0,1,1,1},
{0,0,0,0,1,1,0,1,1},
};
int start[3][3];
int goal[3][3];
int ans[20];
struct a{
int step;
int father;
int no;
int map[3][3];
}que[20000];
int temp[3][3];
int judge(int taill)//判重 改SET
{
for(int i=1;i<taill;i++)
{
int k=0;
for(int s=0;s<3;s++)
for(int n=0;n<3;n++)
if(temp[s][n]==que[i].map[s][n])k++;
if(k==9)return 0;
}
return 1;
}
int jjudge()//判目标
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(temp[i][j]!=goal[i][j])return 0;
return 1;
}
void bfs()
{
int u,v,head=0,tail=1;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
que[head].map[i][j]=start[i][j];
que[1].father=0;
while(head<tail)
{
head++;
for(int i=0;i<9;i++){
memcpy(temp,que[head].map,sizeof(temp));
int k=0;
for(int t=0;t<3;t++)
for(int s=0;s<3;s++)
{
temp[t][s]+=dx[i][k];
temp[t][s]%=4;
k++;
}
if(judge(tail))
{
tail++;
memcpy(que[tail].map,temp,sizeof(que[tail].map));
que[tail].no=i;
que[tail].father=que[head].no;
if(jjudge())
{
u=tail,v=0;
while(u!=0){
ans[++v]=que[u].no;
u=que[u].father;
}
}
}
}
}
}
int main()
{
int i,j,n,s;
freopen("clock.in","r",stdin);
freopen("clock.out","w",stdout);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>start[i][j];
bfs();
for(i=1;i<=20;i++)cout<<ans[i]<<" ";
/*for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<<dx[i][j];
cout<<endl;
}*/
return 0;
}
帮忙看下错在哪里了,谢谢蛤
2 条评论
-
mouae LV 7 @ 2017-02-13 22:52:12
听取人生经验233
-
2017-02-13 22:49:53@
谢谢蛤?????? 不知道他老先生会不会C++
- 1