POJ 3279 Fliptile(开关问题)

Fliptile

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12908 Accepted: 4739

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的MN次方反转的方法。
以本题中的sample 为例子,翻转最左上角的(1,1) 除了翻转(1,1) 还可以翻(1,2)(2,1)
所以这里设计一种新的思路,我们不妨指定好最上面一行的翻转方法,此时能翻转(1,1)的只剩下(2,1),所以可以直接判断(2,1)是否需要翻转,类似(2,1)·········(2,N) 都需要类似的判断,反复执行下去, 最后(M,1)·····(M,N)如果并非全为白色,那么则是 IMPOSSIBLE
第一行有2的N次方种

#include<iostream>
#include<stdio.h>
#include<cstring>
using  namespace std;
int M,N;
int a[20][20],flip[20][20],opt[20][20];
int dx[]={-1,0,0,0,1};
int dy[]={0,-1,0,1,0};
//得到颜色
int get(int x,int y)
{
    int c=a[x][y];
    for(int i=0;i<5;i++)
    {
        int tx=x+dx[i],ty=y+dy[i];
        if(tx>=0&&tx<M&&ty>=0&&ty<N)
        {
             c+=flip[tx][ty];
        }
    }
    return c%2;
}
//求出第1行确定情况下的最小操作次数
//不存在解的话返回-1
int fun()
{
    for(int i=1;i<M;i++)
    {
        for(int j=0;j<N;j++)
         {
            if(get(i-1,j)!=0) //若i-1是黑色,则必须反转
            {
                flip[i][j]=1;
            }
         }
    }
    //判断最后一行全白
    for(int j=0;j<N;j++)
    {
          if(get(M-1,j)!=0)
           {
               return -1;
           }
    }
    //统计翻的次数
    int res=0;
    for(int i=0;i<M;i++)
    {
        for(int j=0;j<N;j++)
        {
            res+=flip[i][j];
        }
    }
    return res;
}
void solve()
{
     int res=-1;
     //按照字典序尝试第一行所有的可能性
     for(int i=0;i<(1<<N);i++)
     {
         memset(flip,0,sizeof(flip));
         for(int j=0;j<N;j++)
         {
             flip[0][N-j-1]=i>>j&1;
         }
         int num=fun();
         if(num>=0&&(res<0||res>num))
         {
             res=num;
             memcpy(opt,flip,sizeof(flip));
         }
     }
     if(res<0)
         cout<<"IMPOSSIBLE"<<endl;
     else
     {
         for(int i=0;i<M;i++)
         {
             for(int j=0;j<N;j++)
             {
                   printf("%d%c",opt[i][j],j+1==N ? '\n' : ' ');
             }
         }
     }
}
int main()
{
    while(cin>>M>>N)
    {
       for(int i=0;i<M;i++)
          for(int j=0;j<N;j++)
               cin>>a[i][j];
       solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值