poj 2965 dfs+枚举
来源:互联网 发布:怎么在手机淘宝买彩票 编辑:程序博客网 时间:2024/06/05 15:57
题目
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
题意 :给你一个密码锁 让你判断最少需要改变多少个位置 可以打开锁(即全部为‘-’) 每选择一个位置 该位置对应的行和列
也会反转
题解 : 和前面那个棋盘的题一样 对步数枚举 然后把需要走的步数看成树状图 用dfs 跑一遍就好了
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;bool m[6][6]={false};bool flag;int dis[6][6]={0};int step;bool judge(){ for(int i = 1; i < 5; i++) { for(int j = 1; j < 5; j++) { if(m[i][j] != true)//这里刚开始受前面一题的影响写成了 m[i][j] != m[1][1] return false; } } return true;}void change(int c,int r){ for(int i = 1; i < 5; i++) { m[c][i] = (m[c][i] == true)?false:true; m[i][r] = (m[i][r] == true)?false:true; } m[c][r] = (m[c][r] == true)?false:true;}void dfs(int c,int r,int deep){ if(deep == step) { flag = judge(); return ; } if(flag || c == 5) return ; change(c,r); if(r<4) dfs(c,r+1,deep+1); else dfs(c+1,1,deep+1); change(c,r);//想一下为什么判定是在下面而不是放在上面 if(flag) { dis[c][r] = 1; } if(r<4) dfs(c,r+1,deep); else dfs(c+1,1,deep);}int main(){ char temp; for(int i =1; i < 5; i++) for(int j = 1; j < 5; j++) { cin>>temp; if(temp == '-') m[i][j] = true; } for(step = 0; step <=16;step++) { dfs(1,1,0); if(flag) break; } if(flag) { printf("%d\n",step); for(int i =1; i < 5;i++) for(int j = 1; j < 5;j++) { if(dis[i][j] == 1) printf("%d %d\n",i,j); } } return 0;}
第一次写的时候 真的是错误百出 判断合格的条件写错 判断步数的位置也写错
判断步数的位置 应该放在 dfs 之后 这样返回的值才会是正确的 不然就是返回之前的值
最后附上几组测试样列 帮助寻找错误
-+--++++-+---+--12 2++++++++++++++++161 11 21 31 42 12 22 32 43 13 23 33 44 14 24 34 4+--++---+++-+++-31 12 22 3+----+++++++++++41 12 22 32 4-+++++++-+++-+++32 22 32 4++-+++++++++++++92 12 22 43 13 23 44 14 24 4
- poj 2965 dfs+枚举
- poj 2965 dfs+枚举
- poj 2965 枚举加dfs
- POJ 2965(DFS+枚举)
- poj 2965 枚举+DFS
- poj 2965 枚举+DFS
- poj 2965 枚举+DFS
- poj 1753||poj 2965 枚举+dfs
- POJ 2965做题笔记 #DFS# #枚举#
- poj 1562 DFS+枚举
- POJ 1753 dfs+枚举
- poj 1753 枚举+dfs
- poj 1753 枚举加dfs
- poj Flip game【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&&输出过程)
- Visual Studio 2017离线安装包获取和安装教程
- servlet/filter/listener/interceptor区别与联系
- Qt string与QString之间的转换
- CentOS 7系统时间与实际时间差8个小时
- 【模板】并查集
- poj 2965 dfs+枚举
- Zend Framework安装
- JDBC—总结(2).Dao模式
- 优先级队列PriorityQueue
- HTTPS证书验证流程及SSL证书生成步骤【附nginx开启https配置】
- JAVA基础(二)
- React 组件性能优化
- Caffe
- Codeforces Round #443 (Div. 2) C. Short Program 【模拟】