您的位置:首页 > 其它

poj 3279 Fliptile

2017-05-17 11:13 357 查看
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


题意,翻转格子,翻转之后相邻的4个格子也会一起翻转。问你一种最小的翻转次数的翻转情况。如果答案不唯1输出字典序最小的。

照这个数据来看答案是唯1的。

我们不难想的是暴力,但是不可能去暴力所有的格子翻转或者不翻转。实际上我们只需要枚举第一行的翻转状态就可以。

然后后面一行的状态就会被上一行的状态唯一确定了。想一想是不是。最后检测一下最后一行的状态是不是全为0就可以了。

如果是,说明是答案的一种,更新答案即可。

字典序最小的话,我们枚举的时候从000000...到111111....枚举就可以了。

#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;
const int MAXN=10+7;
const int inf=1e9;
int tu[MAXN][MAXN];
int state[MAXN][MAXN];//中间翻转状态
int ans[MAXN][MAXN];//最终结果
int temp[MAXN][MAXN];//每一次枚举中间结果
int dx[5] = {0,0,0,-1,1};
int dy[5] = {0,1,-1,0,0};
int n,m;
int Ans;
bool isOk()
{
for(int j = 0 ;j < m; ++j)if(state[n-1][j])return 0;
return 1;
}

void updata(int i,int j)
{
temp[i][j] = 1;
for(int k = 0 ;k < 5; ++k)
{
int nx = i + dx[k];
int ny = j + dy[k];
if(nx >= 0 && nx < n && ny >= 0 && ny < m )state[nx][ny] = !state[nx][ny];
}
}

void check(int key)
{
memcpy(state,tu,sizeof(tu));
memset(temp,0,sizeof(temp));
int cnt = 0 ;
//赋值第一行的翻转状态
for(int j = 0 ;j < m; ++j)
{
if(key & (1<<(m-1-j)))
{
updata(0,j);
cnt++;
}
}
//从第二行消灭1
for(int i = 1 ; i < n ; ++i)
for(int j = 0 ; j< m ; ++j)
{
if(state[i-1][j])
{
cnt++;
updata(i , j);
}
}
//检测并更新答案
if(isOk() && cnt < Ans)
{
Ans=cnt;
memcpy(ans,temp,sizeof(temp));
}
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(ans,0,sizeof(ans));
for(int i = 0; i < n; ++i)
for(int j = 0 ;j < m ; ++j)scanf("%d",&tu[i][j]);
Ans = inf ;
for(int key = 0 ; key < (1<<m) ; ++key)check(key);
if(Ans == inf)
{
puts("IMPOSSIBLE");
continue;
}
for(int i = 0 ;i< n ; ++i)
{
for(int j = 0 ;j < m ; ++j)
{
if(j)printf(" ");
printf("%d",ans[i][j]);
}
puts("");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: