代码如下:
dbfs:
1 #include <iostream> 2 #include <map> 3 #include <algorithm> 4 #include <string> 5 #include <queue> 6 using namespace std; 7 typedef long long LL; 8 9 int eight[3][3], xx, xy, dx[]={0,0,-1,1,0}, dy[]={0,1,0,0,-1}; 10 char dir[] = "orudl"; 11 12 LL encode(){ 13 LL s=eight[2][2]; 14 for(int i=7; i>=0; --i){ 15 s <<=4; 16 s |= eight[i/3][i%3]; 17 } 18 return s; 19 } 20 void decode(LL s){ 21 for(int i=0; i<9; ++i){ 22 eight[i/3][i%3] = s%16; 23 if(s%16 == 0) 24 xx = i/3, xy = i%3; 25 s >>=4; 26 } 27 } 28 void print_state(LL s){ 29 printf("s:%d,xx,xy:", s); 30 decode(s); 31 printf("%d,%d\n",xx,xy); 32 for(int i=0; i<3; ++i){ 33 for(int j=0; j<3; ++j) 34 printf("%d ", eight[i][j]); 35 printf("\n"); 36 } 37 38 } 39 queue<LL> que[2]; 40 map<LL,pair<int,LL> > mp[2];// pair:d,ps 41 42 void print(LL s){ 43 int d = mp[0][s].first; 44 LL ts=s; 45 string ope; 46 while(d!=-1){ 47 ope.push_back(dir[d]); 48 ts = mp[0][ts].second; 49 d = mp[0][ts].first; 50 } 51 reverse(ope.begin(), ope.end()); 52 cout<< ope; 53 ope.clear(), d = mp[1][s].first, ts = s; 54 while(d!=-1){ 55 ope.push_back(dir[d]); 56 ts = mp[1][ts].second; 57 d = mp[1][ts].first; 58 } 59 cout << ope << "\n"; 60 } 61 void expand(int idx){ 62 static int found = 0; 63 if(found){ 64 while(!que[idx].empty()) que[idx].pop(); 65 return; 66 } 67 int len = que[idx].size(); 68 while(len--){ 69 LL s = que[idx].front(), s2; que[idx].pop(); 70 for(int i=1; i<5; ++i){ 71 decode(s); 72 int r,c; 73 if(idx==0) 74 r = xx+dx[i], c = xy+dy[i]; 75 else 76 r = xx-dx[i], c = xy-dy[i]; 77 if(r<0 || c<0 || r==3 || c==3) 78 continue; 79 swap(eight[xx][xy], eight[r][c]); 80 s2 = encode(); 81 if(mp[idx].find(s2)!=mp[idx].end()) 82 continue; 83 mp[idx][s2] = make_pair(i,s); 84 if(mp[idx^1].find(s2)!=mp[idx^1].end()){ 85 print(s2); 86 found=1; 87 return ; 88 } 89 else que[idx].push(s2); 90 } 91 } 92 } 93 void dbfs(){ 94 mp[0].clear(), mp[1].clear(); 95 LL tar=8; 96 for(int i=7; i>0; --i) 97 tar<<=4, tar |= i; 98 que[1].push(tar); mp[1][tar]=make_pair(-1,0);// 两者相等?? 99 tar = encode(); 100 que[0].push(tar); mp[0][tar] = make_pair(-1,0); 101 while(!que[0].empty() && !que[1].empty()){ 102 if(que[0].size()<que[1].size()) 103 expand(0); 104 else expand(1); 105 } 106 while(!que[0].empty()) 107 expand(0); 108 while(!que[1].empty()) 109 expand(1); 110 } 111 int main(){ 112 std::ios::sync_with_stdio(false); 113 char c; 114 for(int i=0; i<3; ++i){ 115 for(int j=0; j<3; ++j){ 116 cin>>c; 117 if(c==‘x‘) 118 eight[i][j]=0, xx = i, xy = j; 119 else 120 eight[i][j]= c-‘0‘; 121 } 122 } 123 int cnt=0; 124 for(int i=0; i<9; ++i){ 125 for(int pi=0; pi<i; ++pi) 126 if(eight[pi/3][pi%3] && eight[pi/3][pi%3]<eight[i/3][i%3]) 127 cnt++; 128 } 129 if(cnt%2) 130 printf("unsolvable\n"); 131 else 132 dbfs(); 133 return 0; 134 }
bfs:
1 #include <iostream> 2 #include <map> 3 #include <algorithm> 4 #include <string> 5 #include <queue> 6 using namespace std; 7 typedef long long LL; 8 9 int eight[3][3], xx, xy, dx[]={0,0,-1,1,0}, dy[]={0,1,0,0,-1}; 10 char dir[] = "orudl"; 11 LL tar; 12 13 LL encode(){ 14 LL s=eight[2][2]; 15 for(int i=7; i>=0; --i){ 16 s <<=4; 17 s |= eight[i/3][i%3]; 18 } 19 return s; 20 } 21 void decode(LL s){ 22 for(int i=0; i<9; ++i){ 23 eight[i/3][i%3] = s%16; 24 if(s%16 == 0) 25 xx = i/3, xy = i%3; 26 s >>=4; 27 } 28 } 29 void print_state(LL s){ 30 printf("s:%d,xx,xy:", s); 31 decode(s); 32 printf("%d,%d\n",xx,xy); 33 for(int i=0; i<3; ++i){ 34 for(int j=0; j<3; ++j) 35 printf("%d ", eight[i][j]); 36 printf("\n"); 37 } 38 39 } 40 queue<LL> que; 41 map<LL,pair<int,LL> > mp;// pair:d,ps 42 43 void print(LL s){ 44 int d = mp[tar].first; 45 LL ts=s; 46 string ope; 47 while(d!=-1){ 48 ope.push_back(dir[d]); 49 ts = mp[ts].second; 50 d = mp[ts].first; 51 } 52 reverse(ope.begin(), ope.end()); 53 cout<< ope<< "\n"; 54 } 55 void bfs(){ 56 mp.clear(); 57 LL s = encode(), s2; 58 que.push(s); mp[s] = make_pair(-1,0); 59 while(!que.empty()){ 60 s = que.front(); que.pop(); 61 for(int i=1; i<5; ++i){ 62 decode(s); 63 int r,c; 64 r = xx+dx[i], c = xy+dy[i]; 65 if(r<0 || c<0 || r==3 || c==3) 66 continue; 67 swap(eight[xx][xy], eight[r][c]); 68 s2 = encode(); 69 if(mp.find(s2)!=mp.end()) 70 continue; 71 mp[s2] = make_pair(i,s); 72 if(s2==tar){ 73 print(s2); 74 return ; 75 } 76 else que.push(s2); 77 } 78 } 79 } 80 int main(){ 81 std::ios::sync_with_stdio(false); 82 char c; 83 for(int i=0; i<3; ++i){ 84 for(int j=0; j<3; ++j){ 85 cin>>c; 86 if(c==‘x‘) 87 eight[i][j]=0, xx = i, xy = j; 88 else 89 eight[i][j]= c-‘0‘; 90 } 91 } 92 tar=8; 93 for(int i=7; i>0; --i) 94 tar<<=4, tar |= i; 95 int cnt=0; 96 for(int i=0; i<9; ++i){ 97 for(int pi=0; pi<i; ++pi) 98 if(eight[pi/3][pi%3] && eight[pi/3][pi%3]<eight[i/3][i%3]) 99 cnt++; 100 } 101 if(cnt%2) 102 printf("unsolvable\n"); 103 else 104 bfs(); 105 return 0; 106 }
时间: 2024-10-07 07:45:57