poj2965 The Pilots Brothers' refrigerator
来源:互联网 发布:php 常量定义和使用 编辑:程序博客网 时间:2024/05/17 05:13
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
这道题和翻黑白棋差不多,用深搜可以做,不同的是要记录每次的路径,可以设个数组b[][].
#include<stdio.h>#include<string.h>int step,flag;char s[10][10];int b[20][2];int panduan(char s[10][10]){int i,j;for(i=0;i<4;i++){for(j=0;j<4;j++){if(s[i][j]=='+')return 0;}}return 1;}void flip(int row,int col){int i,j;for(i=0;i<=3;i++){if(s[i][col]=='+')s[i][col]='-';else s[i][col]='+';}for(i=0;i<=3;i++){if(s[row][i]=='+')s[row][i]='-';else s[row][i]='+';}if(s[row][col]=='+')s[row][col]='-';else s[row][col]='+';return;}void dfs(int row,int col,int dep){int i,j;if(flag)return;if(dep==step){flag=panduan(s);return;}if(flag || row==4)return;flip(row,col);if(col<3){b[dep+1][0]=row;b[dep+1][1]=col;dfs(row,col+1,dep+1);}else{b[dep+1][0]=row;b[dep+1][1]=col;dfs(row+1,0,dep+1);}if(flag)return;flip(row,col);if(col<3){dfs(row,col+1,dep);}else dfs(row+1,0,dep);return;}int main(){int n,m,i,j;while(scanf("%s",s[0])!=EOF){for(i=1;i<=3;i++){scanf("%s",s[i]);}for(step=1;step<=16;step++){memset(b,0,sizeof(b));flag=0;dfs(0,0,0);if(flag)break;}printf("%d\n",step);for(i=1;i<=step;i++){printf("%d %d\n",b[i][0]+1,b[i][1]+1);}}return 0;}
- POJ2965 The Pilots Brothers' refrigerator
- POJ2965----The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965--The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- The Pilots Brothers' refrigerator Poj2965
- POJ2965-The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965-The Pilots Brothers' refrigerator
- poj2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers\' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965 The Pilots Brothers' refrigerator 枚举
- poj2965——The Pilots Brothers' refrigerator
- 浙江大学PAT_乙级_1038. 统计同成绩学生(20)
- 虚拟存储管理
- 【HTML】文字效果
- 使用NIO的直接缓存区复制文件
- 新浪微博开发之十八(文本框)
- poj2965 The Pilots Brothers' refrigerator
- Take advantage of TCP/IP options to optimize data transmission
- Express启动方式区别
- win7(64)+httpd2.4.12+mysql5.5.20+php5.6.7环境配置记录
- LeetCode 112: Path Sum
- 安卓快速集成支付功能
- node.js学习记录(1)
- 新浪微博开发之十九(文本框工具栏)
- Quartus错误集锦