zoukankan      html  css  js  c++  java
  • BZOJ1054(搜索)

    大力搜,状态用一个16位的数字表示。


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define rep(i,a,b)              for(int i(a); i <= (b); ++i)
    
    const int A     =    30          +       1;
    
    struct node{int x, y; } op[A];
    struct Node{int num, step;} now, np;
    
    char st[A][A];
    int f[A][A];
    int h[1 << 18];
    int x, y, nx, ny, s, t, cnt = 0;
    
    queue <Node> Q;
    
    int main(){
    
    	rep(i, 1, 8) scanf("%s", st[i] + 1);
    	cnt = 0;
    	rep(i, 1, 4) rep(j, 1, 4) f[i][j] = cnt++;
    	rep(i, 5, 8) rep(j, 1, 4) f[i][j] = f[i - 4][j];
    	
    	s = 0, t = 0;
    
    	rep(i, 1, 4) rep(j, 1, 4) if (st[i][j] == '1') s |= (1 << f[i][j]);
    	rep(i, 5, 8) rep(j, 1, 4) if (st[i][j] == '1') t |= (1 << f[i][j]);
    
    	cnt = 0;
    
    	rep(i, 1, 3) rep(j, 1, 4){ op[++cnt].x = f[i][j], op[cnt].y = f[i + 1][j];}
    	rep(i, 1, 4) rep(j, 1, 3){ op[++cnt].x = f[i][j], op[cnt].y = f[i][j + 1];}
    
    	memset(h, 0, sizeof h); h[s] = 1; Q.push({s, 0});
    	
    	while (!Q.empty()){
    		np = Q.front(); Q.pop();
    		if (np.num == t){
    			printf("%d
    ", np.step);
    			break;
    		}
    		rep(i, 1, cnt){
    			now = np;
    			x = op[i].x, y = op[i].y;
    			nx = (now.num >> x) & 1, ny = (now.num >> y) & 1;
    			if (nx ^ ny){
    				now.num ^= (1 << x);
    				now.num ^= (1 << y);
    			}
    
    			if (!h[now.num]){
    				h[now.num] = 1;
    				Q.push({now.num, now.step + 1});
    			}
    		}
    	}
    
    	return 0;
    
    }
    
    
    


  • 相关阅读:
    性能百万/s:腾讯轻量级全局流控方案详解
    Swagger2
    shiro 入门
    01、单例模式
    02、工厂方法
    04、模板模式
    13、Adapter 适配器
    14、迭代器
    Java 面向切面 AOP
    spring boot 中使用 Redis 与 Log
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/6648796.html
Copyright © 2011-2022 走看看