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
解题思路:
呃。。。这题被出题人归为BFS和DFS类了,可如果仔细想想该题,可以找出其中的规律。如果一个冰箱门改变状态,那么它所在的行和列都得改变状态,只有改变奇数次状态的门才真正改变状态,而改变偶数次的门就回到了之前的状态。所以我们只需把改变奇数次的当成改变了一次,而改变偶数次的我们当做没有改变过。当其中一个点需要改变,那么就把该点所在的行列上的每个点都改变一次,那么对于需要改变的点一共改变了7次,而该行该列其他的点都只改变了4次,其余剩下的点只改变了2次,都是偶数次相当于没变。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 10; int main() { char map[maxn][maxn]; int count[maxn][maxn], total = 0; // count用来记录每个点改变的次数 memset(count, 0,sizeof(count)); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { scanf("%c",&map[i][j]); if(map[i][j] == '+') // 如果需要改变就改变所在的行与列上所有的点 { for(int k = 0; k < 4; k++) count[i][k]++; for(int k = 0; k < 4; k++) count[k][j]++; count[i][j]--; // 点(i,j)被改变了两次,需要减去一次 } } getchar(); } for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(count[i][j] % 2 != 0) // 改变奇数次就算作改变了一次 total++; // 总共改变了几次 printf("%d\n", total); for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(count[i][j] % 2 != 0) printf("%d %d\n", i + 1, j + 1); // 输出需要改变的点的坐标 return 0; }
The Pilots Brothers' refrigerator