标签:
直接暴力广搜即可。。
【网上有大神说双向广搜速度快,然而直接暴力广搜就可以过了】
队列中的状态用二进制来存储。。
我用了一个比较sb的写法,勿喷qaq
1 #include <queue> 2 #include <cstdio> 3 4 using namespace std; 5 6 unsigned int Start, End; 7 bool M[4][4]; 8 9 const int dx[] = {+0, +0, +1, -1}; 10 const int dy[] = {+1, -1, +0, +0}; 11 12 void init() 13 { 14 char Map[4][4]; 15 unsigned int Temp; 16 for (int i = 0;i != 4;++i) 17 { 18 scanf("%s", Map + i); 19 Temp = 0; 20 for (int j = 0;j != 4;++j) 21 (Temp <<= 1) += Map[i][j] - ‘0‘; 22 Start += (Temp << (i << 2)); 23 } 24 for (int i = 0;i != 4;++i) 25 { 26 scanf("%s", Map + i); 27 Temp = 0; 28 for (int j = 0;j != 4;++j) 29 (Temp <<= 1) += Map[i][j] - ‘0‘; 30 End += (Temp << (i << 2)); 31 } 32 } 33 34 int Dist[100050]; 35 36 void BFS() 37 { 38 queue<unsigned int> Q; 39 Q.push(Start); 40 unsigned int Top, Temp, Go; 41 int x, y; 42 Dist[Start] = 1; 43 while (Q.size()) 44 { 45 Top = Q.front(); 46 Q.pop(); 47 for (int i = 0;i != 4;++i) 48 for (int j = 0;j != 4;++j) 49 M[i][j] = (Top & (1 << (i * 4 + (3 - j)))); 50 for (int i = 0;i != 4;++i) 51 for (int j = 0;j != 4;++j) 52 if (M[i][j]) 53 for (int k = 0;k != 4;++k) 54 { 55 x = i + dx[k], y = j + dy[k]; 56 if (x >= 0 && x < 4 && y >= 0 && y < 4) 57 if (!M[x][y]) 58 { 59 swap(M[i][j], M[x][y]); 60 Go = 0; 61 for (int i = 0;i != 4;++i) 62 { 63 Temp = 0; 64 for (int j = 0;j != 4;++j) 65 (Temp <<= 1) += M[i][j]; 66 Go += (Temp << (i << 2)); 67 } 68 if (!Dist[Go]) 69 { 70 Dist[Go] = Dist[Top] + 1; 71 Q.push(Go); 72 } 73 swap(M[i][j], M[x][y]); 74 } 75 } 76 if (Dist[End]) 77 break; 78 } 79 } 80 81 int main() 82 { 83 init(); 84 BFS(); 85 printf("%u", Dist[End] - 1); 86 return 0; 87 }
标签:
原文地址:http://www.cnblogs.com/Created-equal/p/5104004.html