POJ3279_Fliptile_反转问题-2

来源:互联网 发布:centos安装php环境 编辑:程序博客网 时间:2024/06/10 00:14

Fliptile
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10116 Accepted: 3748

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 41 0 0 10 1 1 00 1 1 01 0 0 1

Sample Output

0 0 0 01 0 0 11 0 0 10 0 0 0

给出一个 M*N 的网格的初始状态。每个格有黑白两个面。每次把一个格翻面,与它有公共边的四个格子也会跟着反转。从初始状态到所有格子都是白色,求最小反转次数下的反转方案。如果方案不唯一,输出字典序最小的。


这也是一个反转问题,满足两条性质: 1.反转格子的顺序改变不影响结果 2.同一个格子反转偶数次相当于没有反转。

但这个题是不是线性的,是二维的,表现在:左上角第 1 格格子不止被包含在 1 个区间内。

在这种情况下,我们先用枚举的方法确定第一行的每一个格是否反转。第一行确定了,第二行也就跟着确定了。因为在这种情况下第一行只被唯一的区间包含(只能反转它下面的格子来改变它的状态)。因此第 2 行的状态便确定了。循环往复直到 M - 1 行。

最后检查一下第 M 行是否全是白的。如果是,则 Pro 中保存的方案是合法的。


反转问题本质上是一个递推的思想。


#include<cstdio>#include<iostream>#include<cstring>using namespace std;int mx[5] = { 0,  0, 1, -1,  0};int my[5] = { 1, -1, 0,  0,  0};const int maxn = 15 + 10;const int inf  = 0x3f7f7f7f;int Tile[maxn][maxn];//板的初始状态int Pro [maxn][maxn];//记录反转过程int Res [maxn][maxn];//当前的最优解int M, N;int Min = inf;//初始化最小步数//获得 i j 板的状态// 0 白 1 黑int gettile (int i, int j){int temp = Tile[i][j];for(int k= 0; k< 5; k++){int xx = i + mx[k], yy = j + my[k];if(xx<0 || xx>=M || yy<0 || yy>=N) continue;temp += Pro[xx][yy];}return temp % 2;}//反转 2 到 M 行//cnt 第一行反转的次数int Solve (int cnt){for(int i= 0; i< M-1; i++){for(int j= 0; j< N; j++){if(gettile(i, j)){Pro[i+1][j] = 1;cnt ++;}else Pro[i+1][j] = 0;}}//最后一行有黑,返回不合法for(int i= 0; i< N; i++)if(gettile(M-1, i)) return -1;return cnt;}int main (){cin >> M >> N;for(int i= 0; i< M; i++)for(int j= 0; j< N; j++)cin >> Tile[i][j];for(int k= 0; k< 1<<N; k++){memset(Pro, 0, sizeof(Pro));int cnt = 0;//字典序枚举第一行反转状态for(int i= 0; i< N; i++){Pro[0][N-1-i] = k>>i & 1;if(k>>i & 1) cnt ++;}int mov = Solve(cnt);if(mov >= 0 && Min > mov){Min = mov;//更新最小步数//更新最优解for(int i= 0; i< M; i++)for(int j= 0; j< N; j++)Res[i][j] = Pro[i][j];}}if(Min == inf) cout << "IMPOSSIBLE";else for(int i= 0; i< M; i++){cout << Res[i][0];for(int j= 1; j< N; j++)cout << " " << Res[i][j];cout << endl;}return 0;}


原创粉丝点击