Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6229 Accepted: 2376
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
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6229 Accepted: 2376
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
解题思路:首先我们知道 一个格子如果被翻过来两次,那么就会恢复原状,所以多次的翻转是多余的,这时候如果翻转的各自的集合相同的话,那么翻转的次序则无关紧要。
如果直接暴力那么时间复杂度是2^nm,这样做肯定会超时。
我们不妨来这样思考,加入对于一个格子(0,0),影响他的是(1,0),(0,1).但是如果我们把第一行确定了,那么影响他的就是(1,0)。就这样一行一行的思考,直至倒数第二行。再来检查最后一行是否全为白色,如果全是白色,那么该解成立,再检查是否为最优解。第一行如果枚举有2^n种情况。
那么复杂度一下子变为(nm2^n)了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=10000;
const int maxn = 16;
int m,n,res,temp;
int dirx[5]={-1,1,0,0,0};
int diry[5]={0,0,0,-1,1};
int Map[maxn][maxn];
int flip[maxn][maxn];//储存每次翻转的临时状态
int ans[maxn][maxn];//保存翻转的最优状态
int get(int x,int y)//获取(x,y)当前的颜色信息
{
int pos = Map[x][y];
for(int i=0;i<5;i++)
{
int a = x+dirx[i];
int b = y+diry[i];
if(a>=0&&a<m&&b>=0&&b<n)
pos+=flip[a][b];
}
return pos&1;
}
int Cal()//求出在第一行确定的情况下的最小操作数
{
for(int i=1;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(get(i-1,j))//上一行是1,这行必换
flip[i][j] = 1;
}
}
for(int i=0;i<n;i++)//看看最后一行全为白色
if(get(m-1,i))//如不是则无解
return INF;
int tmp = 0;
for(int i=0;i<m;i++)//确定最小操作步数
for(int j=0;j<n;j++)
tmp += flip[i][j];
return tmp;
}
int main()
{
while(scanf("%d %d",&m,&n)!=EOF)
{
res = INF;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
scanf("%d",&Map[i][j]);
memset(ans,0,sizeof(ans));
//memset(filp,0,sizeof(filp));
for(int i=0;i < 1<<n;i++)//枚举第一行所有可能状态
{
memset(flip,0,sizeof(flip));
for(int j=0;j<n;j++)
{
flip[0][j] = i>>j&1;//在i的状态下,每个格子的状态
}
temp = Cal();
if(temp < res)
{
res = temp;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
ans[i][j] = flip[i][j];
}
}
if(res == INF)
{
printf("IMPOSSIBLE\n");
continue;
}
else
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d",ans[i][j]);
printf("%c",j==n-1?'\n':' ');
}
}
}
}
return 0;
}