码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ1054(搜索)

时间:2017-01-26 19:45:40      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:front   names   step   状态   define   style   const   ret   ++i   

        大力搜,状态用一个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(搜索)

标签:front   names   step   状态   define   style   const   ret   ++i   

原文地址:http://www.cnblogs.com/cxhscst2/p/6351832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!