poj3279 Fliptile 好题

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 anM ×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


题意:有一个m*n的矩阵,每个各自有个瓦片,每个瓦片有两个颜色,白色和黑色。翻动一次瓦片,会同时翻转周围四个瓦片。现在希望用最少的次数把所有的瓦片变成白色(1 = 黑色, 0 = 白色)。 若有多个解,输出字典序最小的。


分析: 只要第一排的状态确定,就可以知道是否能全部翻成白色。所以我们先枚举第一排所有的状态。一共有2^m种情况。

需要注意的是二维数组的初始化。 还有字典序的问题,即我们应从第一排的最后一个数开始枚举。


代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;

bool maze[20][20];	//记录初始的情况
int res[20][20];	//最后的答案
bool temp[20][20];	//maze的复制品,用于处理每次第一排不同的情况
int ans[20][20];	//中间保存的答案
int m,n;
int cnt = 0;		//记录翻转的次数
int maxn = INF;
void turn (int i, int j)		//翻转
{
    if(i!=0)
        temp[i-1][j] = !temp[i-1][j];
    temp[i][j] = !temp[i][j];
    if(i!=m-1) temp[i+1][j] = !temp[i+1][j];
    if(j!=0) temp[i][j-1] = !temp[i][j-1];
    if(j!=n-1) temp[i][j+1] = !temp[i][j+1];
    return;
}
bool  check()		//对于确定好的第一排,检查是否能全部翻转成为白色。
{
    for(int i=1;i<m;i++){
        for(int j=0;j<n;j++){
            if(temp[i-1][j] == 1){
                turn(i,j);
                ans[i][j]++;
                cnt++;
            }
        }
    }
    int flag = 0;
    for(int i=0;i<n;i++){
        if(temp[m-1][i] == 1){
            flag = 1;
            break;
        }
    }
    if(flag){
       return 0;
    }
    else return 1;
}
int main()
{
    while(cin >> m >> n){
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                cin >> maze[i][j];
            }
        }
        int a = 0>>0; 
        for(int i=0;i< (1<<n);i++){
            cnt = 0;
            memcpy(temp,maze,sizeof(maze[0][0])*20*20);
            memset(ans,0,sizeof(ans[0][0])*20*20);
            for(int j=0;j<n;j++){
                if((i>>j) & 1){		//字典序。 例如i=3,即00011 ,1代表要翻转,0代表不翻转。这是从最后一个瓦片开始翻转。
                    turn(0,n-j-1);
                    ans[0][n-j-1]++;
                    cnt++;
                }
            }
            bool ok = check();
            if(ok && cnt < maxn){
                maxn = cnt;
                memcpy(res,ans,sizeof(ans[0][0])*20*20);
            }
        }
        if(maxn == INF){
            cout << "IMPOSSIBLE" << endl;
        }
        else{
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    cout << res[i][j] << ' ';
                }
                cout << '\n';
            }
        }
    }
}

related game : http://s1.4399.com:8080/4399swf/upload_swf/ftp/20080527/1.swf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值