当前位置: 代码迷 >> 综合 >> poj 2965 The Pilots Brothers' refrigerator
  详细解决方案

poj 2965 The Pilots Brothers' refrigerator

热度:87   发布时间:2023-10-23 09:01:56.0
The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24007   Accepted: 9265   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

Source

这个题只是在上一题的基础上加了路径了而已
下面是代码
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
char ch;
bool a[6][6];
int dy[16];
int dx[16];
int i;
int fid;
int isgoal()
{int w,j;for(j = 1;j <= 4; j++){for(w = 1;w <= 4; w++){if(a[j][w] != 0){return 0;}}}return 1;
}
void flip(int x,int y)
{int j;for(j = 1;j <= 4; j++){a[x][j] = !a[x][j];}for(j = 1;j <= 4; j++){if(j != x)                          //一定要记住a[x][y]只转换一次就行了a[j][y] = !a[j][y] ;}
}
void dfs(int x,int y,int d)
{if(d == i){fid = isgoal();return;}if(fid == 1)return ;if(x == 5)return ;flip(x,y);dx[d] = x;              //在这里加路径dy[d] = y;if(y < 4)dfs(x,y+1,d+1);elsedfs(x+1,1,d+1);flip(x,y);if(y < 4)dfs(x,y+1,d);elsedfs(x+1,1,d);
}
int main()
{int j;fid = 0;for(i = 1;i <= 4; i++){for(j = 1;j <= 4; j++){cin >> ch;if(ch == '+'){a[i][j] = true;}}}for(i = 0;i <= 16; i++){dfs(1,1,0);if(fid)break;}if(fid)printf("%d\n",i);elseprintf("Impossible\n");for(j = 0;j < i; j++){printf("%d %d\n",dx[j],dy[j]);}
}


这题和上题的代码差不多可以参考上一题的代码哦
代码菜鸟,如有错误,请多包涵!!