poj 2965 The Pilots Brothers' refrigerator (状压+bfs)
来源:互联网 发布:app软件代理加盟 编辑:程序博客网 时间:2024/06/14 23:15
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
-+-----------+--
61 11 31 44 14 34 4
#include <iostream>#include <cstdio>#include <cmath>#include <queue>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N = 6;struct node{ int val, step;};struct node1{ int x, y;}res[20];char str[N][N];int f[N][N], s[N][N];int full(int x,int y){ int cnt=0; for(int i=x;i>=0;i--) cnt|=(1<<(i*4+y)); for(int i=x+1;i<4;i++) cnt|=(1<<(i*4+y)); for(int i=y;i>=0;i--) cnt|=(1<<(x*4+i)); for(int i=y+1;i<4;i++) cnt|=(1<<(x*4+i)); return cnt;}int mp[20], vis[(1<<16)+10], pre[(1<<16)+10], pos[(1<<16)+10];int bfs(int x){ queue<node>q; node e; e.step=0, e.val=x; vis[x]=1; q.push(e); while(!q.empty()) { node u=q.front();q.pop(); if(u.val==0) return u.step; for(int i=0;i<16;i++) { int v=u.val^mp[i]; if(vis[v]) continue; vis[v]=1,pre[v]=u.val, pos[v]=i; e.step=u.step+1, e.val=v; if(e.val==0) return e.step; q.push(e); } } return -1;}int main(){ for(int i=0;i<4;i++) scanf("%s",str[i]); int cnt=0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { int k=(i*4+j); if(str[i][j]=='+') cnt|=(1<<k); mp[k]=full(i,j); } } memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); printf("%d\n",bfs(cnt)); int k=0, l=0; while(pre[l]!=-1) { res[k].x=pos[l]/4, res[k].y=pos[l]%4; k++; l=pre[l]; } for(int i=k-1;i>=0;i--) { printf("%d %d\n",res[i].x+1, res[i].y+1); } return 0;}
- POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】
- poj 2965 The Pilots Brothers' refrigerator (状压+bfs)
- POJ 2965 The Pilots Brothers' refrigerator BFS+位运算
- POJ 2965 The Pilots Brothers' refrigerator(BFS+二进制判重)
- BFS + 状态压缩 POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965The Pilots Brothers' refrigerator (bfs)
- POJ-2965-The Pilots Brothers' refrigerator-bfs枚举-位运算
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- springmvc mysql 笔记
- Leetcode 492
- [编程题] 好多鱼!
- Linux命令行中的快捷键
- Java集合框架
- poj 2965 The Pilots Brothers' refrigerator (状压+bfs)
- nand flash 与 nor flash [转]
- test
- windows下搭建bugzilla
- 测试java死锁,锁定对象为什么要用static修饰呢?
- 【程序员技能点】——Linux
- Linux查看系统信息常用命令
- 测试心得
- Erlang term_to_binary 和 binary_to_term 数据封包解包例子