《kuangbin专题一》Fliptile POJ - 3279

《kuangbin专题一》Fliptile POJ - 3279

题目描述:

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”.

输入:

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

输出:

Lines 1… M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

输入样例:

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

输出样例

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

题意:

给出一个M× N大小的二维数组,数组仅含0、1,可以通过翻动,将1变为0,当翻动某位置时其周围的四块格子也随之翻转。问能否将所有的格子都翻转成0,如果能,则输出翻动次数最少的结果,若次数相同则输出字典序最小的结果(翻动一次记为1,未翻动为0);如果不能,输出"IMPOSSIBLE" 。

思路:

这道题是一道二进制搜索的题目,可以先确定第一行的翻转方式,第一行翻转方案确定后,此时仍有1未变为0。这时候只能通过第二行来使第一行的1变为0,同理第二行需要第三行来翻转,依次类推,最后一行完成满足倒数第二行的任务后,只需要判断最后一行的是否全为0即可,判断方案是否满足题意。
从上面描述可以看出,第一行确定后,其余行也就确定了。因此只需要枚举第一行的初始翻转方案即可,采用二进制枚举,首先要使翻转次数最少,其次为使相同的次数最后的字典序最小,应从0向 2 N 2^{N} 2N -1枚举。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int m,n;
int s[18][18]; //地图
int temp[18][18];//过程中的每种方案
int ans[18][18];//结果
int minans=0x3f3f3f3f;//最少的次数
void solve(int k)
{
    int cnt=0;//记录翻转的次数
 for(int i=0;i<n;i++)
    if(((1<<i)&k)!=0)     //记录第一行翻的情况
 {
    cnt++;
    temp[1][i+1]=1;
 }
 for(int i=1;i<m;i++)
  for(int j=1;j<=n;j++)  //对于某一位置,它是0还是1取决于它本身的值、它是否翻过、它上面一行是否翻了以及它的左右行
 {
    if((s[i][j]+temp[i][j]+temp[i-1][j]+temp[i][j-1]+temp[i][j+1])%2)//若余1,则表明通过之前的翻转,它的值仍为1,此时需要下一行来改变
    {
     temp[i+1][j]=1;//下一行需要翻转
     cnt++;
    }
 }

 for(int i=1;i<=n;i++)//判断最后一行是否全为0
  if((s[m][i]+temp[m][i]+temp[m-1][i]+temp[m][i-1]+temp[m][i+1])%2)
 {
    cnt=0x3f3f3f3f;
    break;
 }
 if(cnt<minans)//如果翻转次数少,则替换
 {
  minans=cnt;
  for(int i=1;i<=m;i++)
   for(int j=1;j<=n;j++)
    ans[i][j]=temp[i][j];
 }
}
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
 for(int i=1;i<=m;i++)
  for(int j=1;j<=n;j++)
    scanf("%d",&s[i][j]);
 int len=(1<<n)-1;//第一行全都翻一次即n位1
 minans=0x3f3f3f3f;
 for(int i=0;i<=len;i++)//开始枚举第一行的所有翻转方案
    {
        memset(temp,0,sizeof(temp));
        solve(i);
    }
 if(minans==0x3f3f3f3f)
    cout<<"IMPOSSIBLE"<<endl;
 else{
    for(int i=1;i<=m;i++)
     for(int j=1;j<=n;j++)
      if(j!=n)
       cout<<ans[i][j]<<' ';
      else
       cout<<ans[i][j]<<endl;
 }
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值