雙向廣度優先搜尋
阿新 • • 發佈:2018-12-05
https://www.luogu.org/problemnew/show/P1379
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <algorithm> 7 using namespace std; 8 #define ll long long 9 const int maxn=1e6+10; 10 constint ci=1e6; 11 12 struct node 13 { 14 int a[9],pos; 15 }; 16 17 int f[][5]={ 18 {2,1,3}, 19 {3,0,2,4}, 20 {2,1,5}, 21 {3,0,4,6}, 22 {4,1,3,5,7}, 23 {3,2,4,8}, 24 {2,3,7}, 25 {3,4,6,8}, 26 {2,5,7}, 27 }; 28 29 map<int,int> ms,mt; 30 int step,heads,tails,headt,tailt; 31 node qs[maxn],qt[maxn];32 33 ll cal(int a[]) 34 { 35 int i; 36 ll j=1,sum=0; 37 for (i=0;i<9;i++) 38 { 39 sum+=j*a[i]; 40 j*=9; 41 } 42 return sum; 43 } 44 45 void bfs_s() 46 { 47 int i,tail=tails,pos; 48 ll sum; 49 node d; 50 step++; 51 while(heads!=tail) 52 { 53 heads=(heads+1)%ci; 54 d=qs[heads]; 55 pos=d.pos; 56 for (i=1;i<=f[pos][0];i++) 57 { 58 swap(d.a[pos],d.a[f[pos][i]]); 59 sum=cal(d.a); 60 if (mt.find(sum)!=mt.end()) 61 { 62 printf("%d",step); 63 exit(0); 64 } 65 if (ms.find(sum)==ms.end()) 66 { 67 ms[sum]=1; 68 tails=(tails+1)%ci; 69 qs[tails]=d; 70 qs[tails].pos=f[pos][i]; 71 } 72 swap(d.a[pos],d.a[f[pos][i]]); 73 } 74 } 75 } 76 77 void bfs_t() 78 { 79 int i,tail=tailt,pos; 80 ll sum; 81 node d; 82 step++; 83 while (headt!=tail) 84 { 85 headt=(headt+1)%ci; 86 d=qt[headt]; 87 pos=d.pos; 88 for (i=1;i<=f[pos][0];i++) 89 { 90 swap(d.a[pos],d.a[f[pos][i]]); 91 sum=cal(d.a); 92 if (ms.find(sum)!=ms.end()) 93 { 94 printf("%d",step); 95 exit(0); 96 } 97 if (mt.find(sum)==mt.end()) 98 { 99 mt[sum]=1; 100 tailt=(tailt+1)%ci; 101 qt[tailt]=d; 102 qt[tailt].pos=f[pos][i]; 103 } 104 swap(d.a[pos],d.a[f[pos][i]]); 105 } 106 } 107 } 108 109 int main() 110 { 111 char s[10]; 112 int i; 113 ll sum; 114 scanf("%s",s); 115 for (i=0;i<9;i++) 116 { 117 qs[1].a[i]=s[i]-48; 118 if (qs[1].a[i]==0) 119 qs[1].pos=i; 120 } 121 sum=cal(qs[1].a); 122 ms[sum]=1; 123 124 strcpy(s,"123804765"); 125 for (i=0;i<9;i++) 126 { 127 qt[1].a[i]=s[i]-48; 128 if (qt[1].a[i]==0) 129 qt[1].pos=i; 130 } 131 sum=cal(qt[1].a); 132 mt[sum]=1; 133 if (ms.find(sum)!=ms.end()) 134 { 135 printf("0"); 136 return 0; 137 } 138 139 heads=0,tails=1,headt=0,tailt=1; 140 while (1) 141 bfs_s(),bfs_t(); 142 return 0; 143 } 144 /* 145 023184765 146 147 021345678 148 */