UVA10085-不知错在何处
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<map> 6 #include<queue> 7 #include<string> 8 using namespace std; 9 typedef struct state STA; 10 struct state 11 { 12 int st[3][3]; 13 int stnum; 14 int step; 15 string way; 16 bool operator< (const state p)const 17 { 18 return this->step<p.step; 19 } 20 }; 21 22 const int dx[]={1,-1,0,0}; 23 const int dy[]={0,0,1,-1}; 24 const char Wy[]={'D','U','R','L'}; 25 26 int ans[3][3]; 27 string answay; 28 int ans_step; 29 30 map<int,bool> cyc; 31 queue<STA> psd; 32 33 int bfs(STA start) 34 { 35 int nowstep=start.step; 36 int nowstate=start.stnum; 37 int nowst[3][3]; 38 memcpy(nowst,start.st,sizeof(start.st)); 39 string noway=start.way; 40 while(1) 41 { 42 cyc[nowstate]=true; 43 //cout<<noway<<endl; 44 if(nowstep>ans_step) 45 { 46 ans_step=nowstep; 47 memcpy(ans,nowst,sizeof(nowst)); 48 answay=noway; 49 } 50 int x,y; int ok=0; 51 for(x=0;x<3;++x) 52 { 53 for(y=0;y<3;++y) 54 if(nowst[x][y]==0){ok=1;break;} 55 if(ok)break; 56 } 57 // 58 STA m; 59 for(int i=0;i<4;++i) 60 { 61 int newx=x+dx[i]; 62 int newy=y+dy[i]; 63 ok=(newx>=0&&newx<3&&newy>=0&&newy<3); 64 if(!ok)continue; 65 memcpy(m.st,nowst,sizeof(m.st)); 66 m.st[x][y]=nowst[newx][newy]; 67 m.st[newx][newy]=0; 68 m.stnum=0; 69 for(int k=0;k<3;++k) 70 for(int j=0;j<3;++j) 71 m.stnum=m.stnum*10+m.st[k][j]; 72 if(cyc[m.stnum]==true)continue; 73 m.step=nowstep+1; 74 //m.way=noway.append(1,Wy[i]); 75 m.way=noway; 76 (m.way).append(1,Wy[i]); 77 psd.push(m); 78 } 79 if(psd.empty())break; 80 m=psd.front();psd.pop(); 81 // 82 nowstep=m.step; 83 nowstate=m.stnum; 84 memcpy(nowst,m.st,sizeof(m.st));//st[][] 85 noway=m.way;//string 86 } 87 return 0; 88 } 89 90 int main() 91 { 92 freopen("input.txt","r",stdin); 93 freopen("ans.txt","w",stdout); 94 int n; 95 scanf("%d",&n); 96 for(int c=1;c<=n;c++) 97 { 98 ans_step=0; 99 cyc.clear(); 100 // 101 STA m; 102 m.way=""; 103 m.step=0; 104 int sum=0; 105 for(int i=0;i<3;++i) 106 { 107 for(int k=0;k<3;++k) 108 { 109 scanf("%d",&m.st[i][k]); 110 sum=sum*10+m.st[i][k]; 111 } 112 } 113 m.stnum=sum; 114 // 115 bfs(m); 116 // 117 printf("Puzzle #%d\n",c); 118 for(int i=0;i<3;++i) 119 printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]); 120 cout<<answay<<"\n"<<endl; 121 } 122 return 0; 123 }
下面是Udebug提供的查错:
样例:
9 0 1 2 3 4 5 6 7 8 1 0 2 3 4 5 6 7 8 1 2 0 3 4 5 6 7 8 1 2 3 0 4 5 6 7 8 1 2 3 4 0 5 6 7 8 1 2 3 4 5 0 6 7 8 1 2 3 4 5 6 0 7 8 1 2 3 4 5 6 7 0 8 1 2 3 4 5 6 7 8 0
我的结果:
Puzzle #1 8 0 6 5 4 7 2 3 1 DDRUURDLLDRRULLURRDLDLUURDDLUUR Puzzle #2 0 8 6 7 4 3 2 5 1 DDRUULLDDRURULLDDRURULLDDRRUULL Puzzle #3 8 7 6 2 4 0 5 3 1 DDLUURDDLLUURDLURRDDLURDLLUURRD Puzzle #4 8 7 6 3 4 2 0 5 1 DRUULDDRRUULDRULLDDRULDRRUULLDD Puzzle #5 8 4 6 1 0 7 3 2 5 DRUULDDRUULDDRULLDRULURDDRUULD Puzzle #6 8 7 6 2 5 1 3 4 0 DLUURDDLLUURDLURRDDLURDLLUURRDD Puzzle #7 8 4 7 6 5 2 3 0 1 UURDDRULLURRDLLDRRULULDDRUULDDR Puzzle #8 8 6 7 2 5 4 0 3 1 UURDDLLUURDRDLLUURDRDLLUURRDDLL Puzzle #9 6 4 7 8 5 0 3 2 1 UULDDRUULLDDRULDRRUULDRULLDDRRU
正确答案:
Puzzle #1 8 7 6 0 4 1 2 5 3 DDRUULDDRRUULDRULLDDRULDRRUULLD Puzzle #2 8 6 0 5 4 7 2 3 1 DDRULLURRDLLDRRULULDDRUULDDRRUU Puzzle #3 8 0 6 7 4 3 2 5 1 DDLUULDRRDLLURRULLDRDRUULDDRUUL Puzzle #4 8 7 6 3 4 2 0 5 1 URDRDLULDRUURDDLULURRDLLURRDLDL Puzzle #5 8 6 0 5 2 7 3 4 1 DRULLURRDLLDRRULULDDRUULDDRRUU Puzzle #6 8 7 6 2 5 1 3 4 0 ULDRDLULDRUURDDLULURRDLLURRDLDR Puzzle #7 8 6 4 0 5 7 3 2 1 UURDDLUURRDDLURDLLUURDLURRDDLLU Puzzle #8 8 4 7 6 5 2 3 1 0 UURDLLDRRULLURRDLDLUURDDLUURRDD Puzzle #9 8 6 7 2 5 4 3 0 1 UULDDLURRULLDRRDLLURURDDLUURDDL