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.
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.
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.
-+-- ---- ---- -+--
6 1 1 1 3 1 4 4 1 4 3 4 4
/* 本题与POJ-1753差不多都是枚举状态 只不过本题多了个DFS中的路径输出 把1753代码稍微改一下就可以过了 */ #include <iostream> #include<math.h> #include<algorithm> using namespace std; int mp[6][6] = {0}; int flag = 0; int step; struct pos { int row, col; }nowpos[50]; bool juge() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { //有一个地方不开放就返回false if (mp[i][j] != 1) { return false; } } } return true; } void fan(int row, int col)//翻转 { //整行整列翻转 mp[row][col] = !mp[row][col]; for (int i = 1; i <= 4; i++) { mp[i][col] = !mp[i][col]; mp[row][i] = !mp[row][i]; } } void dfs(int row, int col, int deep)//行,列,当前翻的次数 { if (deep == step) { flag = juge(); return; } if (flag || row == 5) return; //翻棋 fan(row, col); //每次记录步伐 nowpos[deep].row = row; nowpos[deep].col = col; if (col < 4) dfs(row, col + 1, deep + 1); else dfs(row + 1, 1, deep + 1); //不符合条件翻转回来 fan(row, col); if (col < 4) dfs(row, col + 1, deep); else dfs(row + 1, 1, deep); return; } int main() { char k; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { cin >> k; if (k == '-') { mp[i][j] = 1; } else { mp[i][j] = 0; } } } for (step = 0; step <= 16; step++) { dfs(1, 1, 0); if (flag) break; } if (flag) { cout << step << endl; for (int i = 0; i < step; i++) { cout << nowpos[i].row << ' ' << nowpos[i].col << endl; } } else cout << "Impossible" << endl; return 0; }