POJ3279 Fliptile
题目链接:https://vjudge.net/problem/POJ-3279
Description
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
题目大意:给你一个01矩阵,矩阵大小为M x N。(1 <= M , N <= 15)
每次操作选择一个格子,使得该格子与上下左右四个格子的值翻转。
至少多少次操作可以使得矩阵中所有的值变为0?
请输出翻转方案,若没有方案,输出"IMPOSSIBLE” 。
若有多种方案符合题意,请首先输出翻转次数最少的方案;若方案个数仍不唯一,则输出字典序最小的方案。
解题思路:
(1)如果从上到下搜索,当前行是否需要反转取决于上一行的状态,通过翻转当前行使上一行为0,而不是通过上一行翻转为0后,看当前行的状态判断自己是否需要翻转,否则还会继续影响上一行。所以枚举一下第一行所有的状态,搜索到最后一行结束,如果可以保证最后一行都是0,那么方案可以,否则重新定义第一行的状态,继续搜索,找出使反转次数最少的方案。
(2)如果一行有N个格子,每个格子有两种状态,那么一共有2^n种,例如n = 6;001100则指第2、3位(i从0开始)翻转。字典序从小到大就是i从0到1<<n遍历(从一行的最后一位开始考虑翻转)
获取第一行定义的状态 i>>j &1:i>>j为 i 状态下 第j位状态,为什么&1?因为题里定义0是不需要反转的,在get函数中判断某行某一位是否翻转也要注意一下,get函数中枚举上下左右及本身的5个格子,累加这几个格子的翻转值,假如这5个格子中有s个翻转过,那么此时这个格子就相当于原来的格子翻转了s次,加上原来的状态,和1相与便是这个格子的是否需要翻转的值。
Cpp Code
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <queue>
#define MAXN 16
#define INF 1<<30
using namespace std;
int a[MAXN][MAXN],flip[MAXN][MAXN],ans[MAXN][MAXN];
int m,n;
int dx[5] = {0,1,0,-1,0};
int dy[5] = {-1,0,0,0,1};
int get(int x,int y)
{
int cnt = a[x][y];//当前为t的格子,翻过n次后,状态与(t+n)%2等价
for(int i = 0;i<5;i++)
{
int a = x + dx[i];
int b = y + dy[i];
if(a>=0&&a<m&&b>=0&&b<n)
cnt += flip[a][b];
}
return cnt&1;
}
int cal()
{
int i,j;
for(i = 1;i<m;i++)
{
for(j = 0;j<n;j++)
{
if(get(i-1,j))
flip[i][j] = 1;
}
}
for(i = 0;i<n;i++)
{
if(get(m-1,i))//(i-1,j)为黑色,则反转(i,j)
return INF;
}
int cnt = 0;
for(i = 0;i<m;i++)
for(j = 0;j<n;j++)
cnt+=flip[i][j];
return cnt;
}
int main()
{
while(cin>>m>>n)
{
int i,j;
for(i = 0;i<m;i++)
for(j = 0;j<n;j++)
cin>>a[i][j];
int cnt = INF;
for(i = 0;i < 1<<n ;i++)
{
memset(flip,0,sizeof(flip));
for(j = 0;j<n;j++)
{
flip[0][j] = i>>j & 1;
}
int temp = cal();
if(temp < cnt)
{
cnt = temp;
for(int p = 0;p<m;p++)
for(int q = 0;q<n;q++)
ans[p][q] = flip[p][q];
}
}
if(cnt == INF)
cout<<"IMPOSSIBLE\n";
else{
for(i = 0;i<m;i++)
{
for(j = 0;j<n;j++)
{
cout<<ans[i][j];
if(j!=n-1)
cout<<' ';
else
cout<<'\n';
}
}
}
}
return 0;
}