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 }
  • 相关阅读:
    微信小程序之授权 wx.authorize
    微信小程序之可滚动视图容器组件 scroll-view
    纯 CSS 利用 label + input 实现选项卡
    Nuxt.js + koa2 入门
    koa2 入门(1)koa-generator 脚手架和 mongoose 使用
    vue 自定义指令
    时运赋
    WEBGL 2D游戏引擎研发系列 第一章 <新的开始>
    EasyUI特殊情况下的BUG整理
    数字时钟DigClock
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/6351832.html
Copyright © 2011-2022 走看看