The Pilots Brothers' refrigerator(POJ_2965)
来源:互联网 发布:黑马炒股软件 编辑:程序博客网 时间:2024/06/08 12:38
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
6
1 1
1 3
1 4
4 1
4 3
4 4
代码
额,递归暴力枚举了一遍。。。然后发现之前的代码是个现在又看不懂了的参考的别人的思路。。。果然还是要自己想出来才能记住。。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool str[5][5];int ans[20][2];bool flag = 0;void flip(int r, int c){ for(int i = 0; i < 4; i++) { str[r][i] = !str[r][i]; str[i][c] = !str[i][c]; } str[r][c] = !str[r][c];}bool all(){ for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { if(str[i][j] != 1) return 0; } } return 1;}void Solve(int r, int c, int k, int f, int cnt){ if(f == k) { if(all()) { printf("%d\n", cnt); for(int i = 0; i < cnt; i++) printf("%d %d\n", ans[i][0], ans[i][1]); flag = 1; } return ; } if(c >= 4) { if(r >= 3) return ; else { r++; c = 0; } } if(!flag) { Solve(r, c+1, k, f, cnt); flip(r, c); ans[cnt][0] = r+1; ans[cnt][1] = c+1; Solve(r, c+1, k, f+1, cnt+1); flip(r, c); }}int main(){ char c[4]; for(int i = 0; i < 4; i++) { scanf("%s", c); for(int j = 0; j < 4; j++) { if(c[j] == '+') str[i][j] = 0; else str[i][j] = 1; } } for(int i = 0; i <= 16; i++) { int f = 0, cnt = 0; Solve(0, 0, i, f, cnt); } return 0;}
->简便算法链接
- The Pilots Brothers' refrigerator(POJ_2965)
- The Pilots Brothers' refrigerator(POJ_2965)
- poj_2965 The Pilots Brothers' refrigerator(bfs+位运算)
- Poj_2965 The Pilots Brothers' refrigerator(状态压缩,bfs)
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator
- PKU2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator
- POJ2965----The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- mac开发尽量用oc
- web弹层组件layer
- 查看、修改ORACLE最大进程数 及 ORA-00064 处理
- go中的main函数和init函数
- MFC 数据库查找
- The Pilots Brothers' refrigerator(POJ_2965)
- 关于目前自己iOS项目使用的第三方开源库
- 导入项目发生UnsupportedClassVersionError:Unsupported major.minor version错误的解决办法
- POJ 1321 棋盘问题
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- freemarker的空值和默认值
- 易企CMS文章/产品标题太长怎么办?
- php创建微信二维码40052错误提示
- iOS FMDB 增加字段