POJ2965-The Pilots Brothers' refrigerator

来源:互联网 发布:apache ant windows 编辑:程序博客网 时间:2024/06/06 12:42

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26478 Accepted: 10198 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

61 11 31 44 14 34 4

Source

Northeastern Europe 2004, Western Subregion

题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,给一个4*4的矩阵,可以改变任意一个把手的位置,但同时改变其所在的行和列,求最小步骤。

解题思路:暴力枚举每个把手变或不变


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <set>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int a[20][20], r[20], c[20], visit[20][20];char ch[10][10];int mi;struct node{int x, y;} x[20], ans[20];void dfs(int pos, int sum){if (sum >= mi) return;if (pos == 16){int flag = 1;for (int i = 0; i < 4; i++)for (int j = 0; j < 4; j++)if ((a[i][j] + r[i] + c[j] - visit[i][j]) % 2 == 1) flag = 0;if (flag&&sum < mi){mi = sum;for (int i = 0; i < sum; i++)ans[i] = x[i];}return;}visit[pos / 4][pos % 4] = 1;x[sum].x = pos / 4, x[sum].y = pos % 4;r[pos / 4]++, c[pos % 4]++;dfs(pos + 1, sum + 1);visit[pos / 4][pos % 4] = 0;r[pos / 4]--, c[pos % 4]--;dfs(pos + 1, sum);}int main(){while (~scanf("%s", ch[0])){for (int i = 1; i < 4; i++)scanf("%s", ch[i]);for (int i = 0; i < 4; i++)for (int j = 0; j < 4; j++)a[i][j] = ch[i][j] == '+' ? 1 : 0;mi = INF;memset(visit, 0, sizeof visit);memset(r, 0, sizeof r);memset(c, 0, sizeof c);dfs(0, 0);printf("%d\n", mi);for (int i = 0; i < mi; i++)printf("%d %d\n", ans[i].x + 1, ans[i].y + 1);}return 0;}