Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16558   Accepted: 6056

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

题目链接:

http://poj.org/problem?id=3279

题目大意:

给你一个n*m的由0和1组成的矩阵A。一个对应的n*m的01矩阵B,若B的某元素为1,则矩阵A对应位置上下左右及自己五个位置发生反转,即0变为1,1变为0。求字典序最小的矩阵B,使得A变为全0。(做完发现该字典序是反的即从右到左,算一个坑点吧)。由于重复反转效果是一样的,所以这题理解成这样是没有错误的。

简单搜索题。

这题思路比较奇特。从i枚举到1<<n,模拟第一行的反转状态。用位运算获得每一位是1还是0。就是代码中的 i&(i<<j)。然后根据第一行的反转结果,就能完全确定接下来所有行的反转状态和反转结果。这是因为若第一行某一位反转后为1,只有通过下一行此处反转才能变为0。如此,最后只要判断最后一行是否为全0。若全为0,立即输出便是字典序最小的结果。具体细节可以参考我的代码体会一哈^_^。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue> using namespace std; int tile[][];
int ttile[][];
int oper[][]; int main()
{
int m,n;
scanf("%d%d",&m,&n);
for(int i=;i<m;i++)
for(int j=;j<n;j++)
scanf("%d",tile[i]+j); int fflag=false;
for(int i=;i<(<<n);i++)//枚举第一行的翻转
{
memcpy(ttile,tile,sizeof(tile)); for(int j=;j<n;j++)//处理第一行的翻转
{
int op;
if((i&(<<j))>)
op=oper[][j]=;
else
op=oper[][j]=;
ttile[][j]^=op;
if(j->=)
ttile[][j-]^=op;
if(j+<n)
ttile[][j+]^=op;
if(m>)
ttile[][j]^=op;
} for(int j=;j<m;j++)//处理2..m行的翻转
{
for(int k=;k<n;k++)
{
int op;
if(ttile[j-][k]==)
op=oper[j][k]=;
else
op=oper[j][k]=;
ttile[j][k]^=op;
ttile[j-][k]^=op;
if(k->=)
ttile[j][k-]^=op;
if(k+<n)
ttile[j][k+]^=op;
if(j+<m)
ttile[j+][k]^=op;
}
} int flag=true;
for(int i=;i<n;i++)//判断最后一行是否符合条件
{
if(ttile[m-][i]==)
{
flag=false;
break;
}
} if(flag)
{
fflag=true;
break;
}
} if(fflag)
{
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(j==n-)
printf("%d\n",oper[i][j]);
else
printf("%d ",oper[i][j]);
}
}
else
printf("IMPOSSIBLE\n");
return ;
}

最新文章

  1. Java获取用户ip
  2. linux系统安装(下)
  3. URL特殊字符的转义
  4. JQuery图片轮播滚动效果(网页效果--每日一更)
  5. x8086汇编实现dos清屏(clear screen)
  6. 使用redis-dump进行Redis数据库合并
  7. Java使用ZXing生成二维码条形码
  8. java内存模型及GC原理
  9. MeasureSpec学习
  10. 【Alpha】Daily Scrum Meeting——Day4
  11. GridView 翻页 索引超出范围
  12. 读书笔记--Android Gradle权威指南(下)
  13. webpack打包后该如何访问项目?
  14. flask 跨域请求
  15. mybatis 按in 函数参数顺序排序
  16. java web service 写入图片到web/img/
  17. Linux pwn入门教程(3)——ROP技术
  18. MySQL 基础二 创建表格
  19. HDU 3032 (SG打表找规律)
  20. C# 最小化到托盘,托盘右击菜单显示

热门文章

  1. python+appium搭建的测试环境
  2. tornado install
  3. 小白学 Python 爬虫(5):前置准备(四)数据库基础
  4. vue 原生添加滚动加载更多
  5. day 24 组合的补充
  6. python遍历所有盘符下的图片并拷贝下来
  7. 窗体的FormBorderStyle属性的不同效果
  8. C语言I作业003
  9. 阿里架构师花近十年时间整理出来的Java核心知识pdf(Java岗)
  10. go变量