1.2 POJ 2965 The Pilots Brothers' refrigerator
2018-02-12 14:20
316 查看
题目链接:http://poj.org/problem?id=2965
题意:就是把题目中给出的状态图,全部翻转成--------
----
----状态
翻转: 每次翻转一个,那么它所在的行和列都要翻转问最小翻转次数,同时输出翻转路径.
算法:
暴力 + 枚举 + dfs回溯思路:可以证明每个把手要么翻转,要么不翻转,那么从左到右,从上到下依次枚举每个把手,同时深搜一遍即可。和前面的棋盘翻转一样.#include<cstdio> #include<iostream> #include<string.h> #include<string> using namespace std; bool p[5][5],flag; int ans; int v[20][2]; bool check(){ for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ if(p[i][j]!=0){ return false; } } } return true; } void change(int a,int b){ for(int i=1;i<=a;i++){ p[i][b]=!p[i][b]; } for(int i=a+1;i<=4;i++){ p[i][b]=!p[i][b]; } for(int i=1;i<b;i++){ p[a][i]=!p[a][i]; } for(int i=b+1;i<=4;i++){ p[a][i]=!p[a][i]; } } void dfs(int row,int col,int num){ if(num==ans){ flag=check(); return; } if(flag||row>=5) return; change(row,col); v[num][0]=row;v[num][1]=col; if(col<4)dfs(row,col+1,num+1); else dfs(row+1,1,num+1); change(row,col); if(col<4)dfs(row,col+1,num); else dfs(row+1,1,num); } int main () { char ch; while(cin>>ch){ if(ch=='+') p[1][1]=1; else p[1][1]=0; for(int i=2;i<=4;i++){ cin>>ch; if(ch=='+') p[1][i]=1; else p[1][i]=0; } for(int i=2;i<=4;i++){ for(int j=1;j<=4;j++){ cin>>ch; if(ch=='+') p[i][j]=1; else p[i][j]=0; } } flag=false; for(ans=0;ans<=16;ans++){ dfs(1,1,0); if(flag){ cout<<ans<<endl; break; } } if(flag){ for(int i=0;i<ans;i++){ cout<<v[i][0]<<" "<<v[i][1]; cout<<endl; } } } }
相关文章推荐
- POJ 2965, The Pilots Brothers' refrigerator
- poj2965 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
- POJ2965 The Pilots Brothers' refrigerator 「BFS+状态压缩」
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
- POJ 2965 The Pilots Brothers' refrigerator(高斯消元三种方法)
- poj dfs相关之2965 The Pilots Brothers' refrigerator
- POJ 2965-The Pilots Brothers' refrigerator(枚举&&DFS&&输出过程)
- POJ 2965:The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- poj_2965 The Pilots Brothers' refrigerator(bfs+位运算)
- POJ2965-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(BFS+二进制判重)