The Pilots Brothers‘ refrigerator
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 25343 | Accepted: 9786 | Special Judge |
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
思路:因为每个位置最多翻一次,所以最多翻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; }