【POJ】2965 - The Pilots Brothers' refrigerator(dfs & 双端队列 & 思维)
来源:互联网 发布:spark submit python 编辑:程序博客网 时间:2024/06/06 05:44
点击打开题目
Description
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.
Input
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.
Output
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.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
Source
这道题和翻转棋的差不多,但是这道题复杂多,要考虑多种情况,输出过程。
我这里用双端队列存过程。
代码如下:
#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <cstring>#include <algorithm>using namespace std;struct node{int x,y;}pr;int mapp[8][8];deque<node> q[88];int ans[88];int ant = 0;bool flag = false;bool check(){for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++)if (mapp[i][j] == 0)return false;}return true;}void change(int x,int y){for (int i = 1 ; i <= 4 ; i++){mapp[x][i] = !mapp[x][i];mapp[i][y] = !mapp[i][y];}mapp[x][y] = !mapp[x][y];}void dfs(int x,int y){if (flag)return;if (check()){flag = true;return;}for (int i = y + 1 ; i <= 4 ; i++){change(x,i);ans[ant]++;pr.x = x;pr.y = i;q[ant].push_back(pr);dfs(x,i);if (flag)return;change(x,i);ans[ant]--;q[ant].pop_back();}for (int i = x + 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){change(i,j);ans[ant]++;pr.x = i;pr.y = j;q[ant].push_back(pr);dfs(i,j);if (flag)return;change(i,j);ans[ant]--;q[ant].pop_back();}}}int main(){for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){char t;scanf ("%c",&t);if (t == '+')mapp[i][j] = 0;elsemapp[i][j] = 1;}getchar();}for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){ans[ant] = 1;flag = false;pr.x = i;pr.y = j;q[ant].push_back(pr);change(i,j);dfs(i,j);change(i,j);if (flag)ant++;else{while (!q[ant].empty())q[ant].pop_back();}}}int pos = max_element(ans,ans+ant) - ans;printf ("%d\n",ans[pos]);while (!q[pos].empty()){pr = q[pos].front();printf ("%d %d\n",pr.x,pr.y);q[pos].pop_front();}return 0;}
- 【POJ】2965 - The Pilots Brothers' refrigerator(dfs & 双端队列 & 思维)
- POJ 2965 The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- poj 2965 The Pilots Brothers' refrigerator(dfs)
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- POJ 2965 The Pilots Brothers' refrigerator(回溯查找,dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+DFS)
- POJ 2965 The Pilots Brothers' refrigerator (DFS + 枚举)
- (枚举,dfs)The Pilots Brothers' refrigerator poj 2965
- The Pilots Brothers' refrigerator(POJ-2965)
- poj The Pilots Brothers' refrigerator(DFS)
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
- poj 2965 The Pilots Brothers' refrigerator (迭代加深dfs)
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- 深入剖析Android音频之AudioTrack
- linux下安装protobuf教程+示例(详细)
- poj 3678 Labeling Balls
- 工厂模式之抽象工厂模式
- xmlpullparser之skip(parser)
- 【POJ】2965 - The Pilots Brothers' refrigerator(dfs & 双端队列 & 思维)
- Android View绘制流程
- web.xml
- 几种协议比较(二)
- 实现单向循环链表的创建、测长、打印、插入、删除及逆置
- Akka actor
- 使用Privoxy将socks5代理转为http代理
- C++继承方式梳理
- 设置侧边栏