Fliptile
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16922 | Accepted: 6194 |
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
Source
给你一个棋盘,上面摆满了棋子。这些棋子有两面,一面是黑色,一面是白色。初始状态已经给出,0代表白色,1代表黑色。
当你翻转一个棋子的时候,也会翻转与这个棋子相邻(斜相邻不计入)的所有棋子。问你每个位置最少需要多少次翻转,才能将这个棋盘全部翻转成白色。输出所有方案中字典序最小的那个(即是翻转次数最小的那个)。如果没有的话,输出不可能。
思路:首先我们要枚举第一行的所有状态,这里用到了状压思想。(对于一个棋子来说,翻转两次后又回到了它原本的状态,所以对一个棋子来说,翻转偶数次是没有意义的。)
比如第一行有三个棋子,我们用0代表不翻转,1代表翻转。
那门我们用 000 ,001, 010,100,101,110,111 这六个序列表示第一行所有可能的翻转状态。
把这些序列看做二进制数的话,那么就可以转换为,0,1,2,3,4,5,6
那我们可以利用位运算,用6以内的数来表示所有可能的状态。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 20;
const int INF = 0x3f3f3f3f;
int Case = 1;
int n,m;
int len,flag,sum,ans2;
int X[5] = {-1,0,0,0,1};
int Y[5] = {0,1,-1,0,0};
int MAP[MAXN][MAXN];
int temp[MAXN][MAXN];
int ans[MAXN][MAXN];
int draw(int x,int y)
{
int temp2 = MAP[x][y];///记录这个棋子原本的状态
for(int i=0;i<5;i++)
{
int xx = x+X[i];
int yy = y+Y[i];
if(xx>=0&&xx<m&&yy>=0&&yy<n)
{
temp2+=temp[xx][yy];///加上它周围所有棋子翻转的次数
}
/*
假设所有棋子初始状态均为0
那么0到1相当于翻转了1次
那这个棋子本身的状态(是否已经翻转1次)
加上其周围棋子翻转的次数,如果是偶数的话,就相当于此棋子的状态并没有改变。
*/
}
return temp2&1;///返回1或0, 0代表该棋子的终态为0,反之1代表最终状态为1
}
int main()
{
while(~scanf("%d %d",&m,&n))
{
ans2 = -1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&MAP[i][j]);
}
}
len = 1<<n;///计算所有可能的摆放情况
for(int i=0;i<len;i++)
{
M(temp,0);
for(int j=0;j<n;j++)
{
temp[0][n-j-1] = i>>j &1;
/*
如果i为3 -->101
3>>0 ==> 101 101&1 == 1
3>>1 ==> 10 10&1 == 0
3>>2 ==> 1 1&1 == 1
就相当于把每一位给分离开了
*/
}
for(int j=1;j<m;j++)///从第二行开始
{
for(int k=0;k<n;k++)
{
if(draw(j-1,k)!=0)///检查每一个棋子的终态是否为0,如果不为0的话,则该棋子本身需要再翻转一次
{
temp[j][k] = 1;
}
}
}
flag = 1;
for(int j=0;j<n;j++)///检查最后一行是否全为0
{
if(draw(m-1,j)!=0)
{
flag = 0;
break;
}
}
if(flag==0) continue;
else///如果全是0的话,代表该翻转方法合法。
{
sum = 0;
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
sum+=temp[j][k];///记录所需翻转次数
}
}
if(sum>=0 && (ans2<0||ans2>sum))///记录最小翻转次数的方案
{
ans2 = sum;
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
ans[j][k] = temp[j][k];
}
}
}
}
}
if(ans2<0)
{
printf("IMPOSSIBLE\n");
}
else
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d%c",ans[i][j],j==n-1?'\n':' ');
}
}
}
}
return 0;
}