POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
2016-01-28 18:46
344 查看
The Pilots Brothers' refrigerator
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
View Code
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 22057 | Accepted: 8521 | Special Judge |
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 同1753一样的代码,但是这题有一点不是很明白,就是没有Impossible的可能,
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int handle[10][10]; int flag,step; int r[20],c[20]; int all_open() { for(int i = 1; i <= 4; i++) { for(int j = 1; j <= 4; j++) if(!handle[i][j]) return false; } return true; } void change(int row, int col) { handle[row][col] = !handle[row][col]; //没写这个DFS里面就是死循环了 for(int i = 1; i <= 4; i++) { handle[row][i] = !handle[row][i]; handle[i][col] = !handle[i][col]; } } void dfs(int row, int col, int deep) { if(deep == step) { flag = all_open(); return; } if(flag || row > 4) return; change(row, col); r[deep] = row; c[deep] = col; if(col < 4) { dfs(row, col + 1, deep + 1); } else { dfs(row + 1, 1, deep + 1); } change(row, col); if(col < 4) { dfs(row, col + 1, deep); } else { dfs(row + 1, 1, deep); } return; } int main() { char s[10]; while(scanf("%s", s) != EOF) { memset(handle, 0, sizeof(handle)); for(int i = 0; i < 4; i++) if(s[i] == '-') handle[1][i + 1] = 1; for(int i = 2; i <= 4; i++) { scanf("%s", s); for(int j = 0; j < 4; j++) if(s[j] == '-') handle[i][j + 1] = 1; } flag = 0; for(step = 0; step <= 16; step++) { dfs(1, 1, 0); if(flag) break; } if(flag) { printf("%d\n", step); for(int i = 0; i < step; i++) printf("%d %d\n", r[i],c[i]); } } return 0; }
View Code
相关文章推荐
- HDU-ACM-2023
- 摄影师挨饿受冻拍下的三清山雪景美图
- mysql 存储过程
- 1.4字符串-空格替换
- 在Java代码中使用pdfBox将PDF转换为图片
- Qt之进程间通信(QProcess)
- Qt之进程间通信(QProcess)
- HDU-ACM-2015
- android适配器SimpleCursorAdapter的使用以及最后一个参数的作用
- PyCharm 修改新建文件时的头部模板
- Autosqli——一个sqlmapapi的web管理应用
- 2016年1月-前端开发月刊
- MySQL数据库事务隔离级别(Transaction Isolation Level)
- su-sudo
- HDU-ACM-2012
- gdb没有调试信息
- winform exe图标更改
- wamp的mysql设置用户名和密码
- 在Java代码中使用iTextPDF生成PDF
- ubuntu14.04安装与配置cloudmonkey