Title Description:
There is a 4*4 matrix, for the least number of operations, the 16 lattice into a '-', each flip (i,j), the first row, the J column will also become the opposite state.
Problem Solving Ideas:
Saying all roads to Rome, this topic also has a lot of methods, 1:bfs+ state compression, 2: State compression + enumeration, 3: Gaussian elimination. These methods are all right, I'll tell you my way here.
According to the change of each operation can know, for a matrix, if only (I,J) is the ' + ' state, we will be the first row, column J of the grid operation once all become the Legal state ('-', become the legal state), We are now doing this for all the illegal states, we will find some lattice we operate two or even several times, because each lattice has only two states, in order to make the operation meaningful, we have the most operation of each lattice, and finally we only need to statistical operation odd number of lattice and output its corresponding subscript can.
Topic Links:
http://poj.org/problem?id=2965
Code:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4 5 #defineN 56 7 CharMap[n];8 intVis[n][n];9 voidChange (intXinty);Ten voidAdd (); One A intMain () - { - intI, J, Sum, N; thememset (Vis,0,sizeof(Vis)); - - for(i=0; i<4; i++) - { +scanf ("%s", map); - for(j=0; j<4; J + +) + if(Map[j] = ='+') A Change (i, j); at } - - Add (); - return 0; - } - in voidChange (intXinty) - { to for(intI=0; i<4; i++) +vis[x][i]++, vis[i][y]++; - theVis[x][y]--; * } $ voidAdd ()Panax Notoginseng { - intI, j, num =0; the + for(i=0; i<4; i++) A for(j=0; j<4; J + +) the if(Vis[i][j]%2) +Num + +; -printf ("%d\n", num); $ for(i=0; i<4; i++) $ for(j=0; j<4; J + +) - if(Vis[i][j]%2) -printf ("%d%d\n", i+1, j+1); the}
View Code
POJ 2965 the pilots ' refrigerator