首页 > 代码库 > BZOJ1054(搜索)
BZOJ1054(搜索)
大力搜,状态用一个16位的数字表示。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 7 const int A = 30 + 1; 8 9 struct node{int x, y; } op[A]; 10 struct Node{int num, step;} now, np; 11 12 char st[A][A]; 13 int f[A][A]; 14 int h[1 << 18]; 15 int x, y, nx, ny, s, t, cnt = 0; 16 17 queue <Node> Q; 18 19 int main(){ 20 21 rep(i, 1, 8) scanf("%s", st[i] + 1); 22 cnt = 0; 23 rep(i, 1, 4) rep(j, 1, 4) f[i][j] = cnt++; 24 rep(i, 5, 8) rep(j, 1, 4) f[i][j] = f[i - 4][j]; 25 26 s = 0, t = 0; 27 28 rep(i, 1, 4) rep(j, 1, 4) if (st[i][j] == ‘1‘) s |= (1 << f[i][j]); 29 rep(i, 5, 8) rep(j, 1, 4) if (st[i][j] == ‘1‘) t |= (1 << f[i][j]); 30 31 cnt = 0; 32 33 rep(i, 1, 3) rep(j, 1, 4){ op[++cnt].x = f[i][j], op[cnt].y = f[i + 1][j];} 34 rep(i, 1, 4) rep(j, 1, 3){ op[++cnt].x = f[i][j], op[cnt].y = f[i][j + 1];} 35 36 memset(h, 0, sizeof h); h[s] = 1; Q.push({s, 0}); 37 38 while (!Q.empty()){ 39 np = Q.front(); Q.pop(); 40 if (np.num == t){ 41 printf("%d\n", np.step); 42 break; 43 } 44 rep(i, 1, cnt){ 45 now = np; 46 x = op[i].x, y = op[i].y; 47 nx = (now.num >> x) & 1, ny = (now.num >> y) & 1; 48 if (nx ^ ny){ 49 now.num ^= (1 << x); 50 now.num ^= (1 << y); 51 } 52 53 if (!h[now.num]){ 54 h[now.num] = 1; 55 Q.push({now.num, now.step + 1}); 56 } 57 } 58 } 59 60 return 0; 61 62 }
BZOJ1054(搜索)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。