poj 2965 枚举+DFS
来源:互联网 发布:linux dd 备份u盘 编辑:程序博客网 时间:2024/06/08 11:08
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
思路:因为每个位置最多翻一次,所以最多翻16次,用枚举来做。(代码是看别人的,有一部分还不能完全理解。。)
#include "cstdio"#include "algorithm"#include "cstring"char map[5][5];int m[5][5];typedef struct { int s,t;}road;road a[20];int k,ans;int P(){ for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ if(m[i][j]==0){ return 0; } } } return 1;}void G(int x,int y){ m[x][y]^=1; for(int i=1;i<=4;i++){ m[x][i]^=1; } for(int i=1;i<=4;i++){ m[i][y]^=1; }}void dfs(int x,int y,int step) {// G(x, y); if (step == ans) { k = P();// return; } if(k||x>=5){//不太理解这里为什么要判断k是否为1,个人觉得只要当ans=step时在dfs外判断即可?? return; } G(x, y); if(y<4){ dfs(x,y+1,step+1); a[step].s=x; a[step].t=y; } else { dfs(x+1,1,step+1); a[step].s=x; a[step].t=y; } G(x,y); if(y<4){ dfs(x,y+1,step); } else { dfs(x+1,1,step); }}int main(){ k=0; for(int i=1;i<=4;i++){ scanf("%s",map[i]); for(int j=0;j<4;j++){ if(map[i][j]=='-'){ m[i][j+1]=1; } else if(map[i][j]=='+'){ m[i][j+1]=0; } } } for(int i=1;i<=16;i++){ ans=i; dfs(1,1,0); if(k){ printf("%d\n",ans); break; } } for(int i=0;i<ans;i++){ printf("%d %d\n",a[i].s,a[i].t); } return 0;}
- 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&&输出过程)
- 博为峰Java技术文章 ——JavaSE Swing JTabbedPane选项卡面板I
- 图论
- dplyr包介绍
- stringr包介绍
- [Educational Codeforces Round 17 F (762F)] Tree nesting
- poj 2965 枚举+DFS
- windows使用sublime text3编译lua
- java设计模式之命令模式
- HDU 1513 Palindrome (动态规划 & LCS)
- Android Studio导入项目问题
- codevs 高低位交换 5641
- List comprehension
- 3D游戏引擎系列九
- 1028