Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
Sample Output
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
一道状态压缩+搜索的题目。。。第一次对状压理解了一点,因为当第一行确定了之后,后面所有行的操作都可通过上一行来推出,且方法唯一,所以只需要枚举第一行的所有操作方法就好了
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
#define LL long long
using namespace std;
const int MAX = 20;
int INF= 1e8;
int n, m;
int a[MAX][MAX];
int b[MAX][MAX];
int vis[MAX][MAX];
int tx[4] = {0, 0, -1, 1};
int ty[4] = {1, -1, 0, 0};
int cnt = 0;
int ans, p;
int isout(int i, int j){
if(i >= 1 && i <= n && j >= 1 && j <= m){
return 1;
}
return 0;
}
void flip(int i, int j){
cnt++;
b[i][j] = !b[i][j];
vis[i][j] = 1;
for(int k = 0; k < 4; k++){
int nx = i + tx[k];
int ny = j + ty[k];
if(isout(nx, ny)){
b[nx][ny] ^= 1;
}
}
}
int ok(int k){
cnt = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
b[i][j] = a[i][j];
}
}
for(int j = 0; j < m; j++){
if(k & (1 << (m - 1 - j))){ // 注意是要减一
flip(1, j + 1);
}
}
for(int i = 2; i <= n; i++){
for(int j = 1; j <= m; j++){
if(b[i - 1][j]){
flip(i, j);
}
}
}
for(int j = 1; j <= m; j++){
if(b[n][j]){
return 0;
}
}
return 1;
}
int main(int argc, char const *argv[])
{
while(~scanf("%d%d", &n, &m)){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%d", &a[i][j]);
}
}
ans = n * m + 1;
p = -1;
for(int i = 0; i < (1 << m); i++){
if(ok(i) && cnt < ans){
ans = cnt;
p = i;
}
}
memset(vis, 0, sizeof(vis)); // 在这之前虽然对vis有操作,但没有实际作用
if(p >= 0){
ok(p);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
printf("%d", vis[i][j]);
if(j != m){
printf(" ");
} else{
printf("\n");
}
}
}
} else{
printf("IMPOSSIBLE\n");
}
}
return 0;
}