The pilots ' refrigerator
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 19464 |
|
Accepted: 7462 |
|
Special Judge |
Description
The game "The pilots brothers:following the stripy elephant" has a quest where a player needs to open a refrigerator.
There is handles on the refrigerator door. Every handle can is in one of the States:open or closed. The refrigerator is open if all handles is open. The handles is 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 are 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 is several solutions, you could give any one of the them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
Test instructions: The refrigerator has 16 handles, "+" for the off, "-" for open, to open the refrigerator, must be 16 handles all for "-", each change of a "+" number of the row and column of the switch form will change to the opposite, ask at least how many steps can make him all "-". Outputs the changed rows and columns.
Idea: [Source Orz]---First look at a simple question, how to turn ' + ' into '-' without changing the state on the other position? The answer is to update all handle at the location (i,j) and the row (i) and column (j) where the position is located. As a result, the position has been updated 7 times, the corresponding row (i) and column (j) handle have been updated 4 times, and the remainder has been updated 2 times. Handle that are updated even several times will not cause a change in the final state. Therefore, an efficient solution is obtained, and when each input encounters ' + ', Count the number of times the rows and columns need to be changed when the input is finished, the array is traversed, all the odd positions are the positions of the operations, and the sum of the odd positions is the final number of operations. However, this algorithm applies only when n is even.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < Algorithm> #include <queue>using namespace Std;char a[5][5];int b[5][5];int main () {char C; int i,j,k; int cnt=0; memset (b, 0, sizeof (b)); for (i = 1; I <= 4; i++) {for (j = 1; J <= 4; j + +) {scanf ("%c", &a[i][j]) ; if (j==4) GetChar (); if (a[i][j]== ' + ') {for (k = 1; k <= 4; k++) {b I [k]++; b[k][j]++; } b[i][j]--;//The current handle is repeatedly recorded 1 times, so subtract;}}} for (i = 1; I <= 4; i++) for (j = 1; J <= 4; j + +) cnt + b[i][j]%2; printf ("%d\n", CNT); for (i = 1; I <= 4; i++) for (j = 1; J <= 4; j + +) if (b[i][j]%2)//Flip even number of times represents no flip, only flips odd number of times to represent rollover printf ("%d%d\n", i,j); return 0;}
POJ 2965-the pilots "refrigerator (greedy + enumeration)