zoukankan      html  css  js  c++  java
  • 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
    ", 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 }
  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/6351832.html
Copyright © 2011-2022 走看看