博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3279 Fliptile (简单搜索)
阅读量:5225 次
发布时间:2019-06-14

本文共 2659 字,大约阅读时间需要 8 分钟。

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

题目链接:

 

题目大意:

给你一个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
#include
#include
#include
using namespace std;int tile[20][20];int ttile[20][20];int oper[20][20];int main(){ int m,n; scanf("%d%d",&m,&n); for(int i=0;i
0) op=oper[0][j]=1; else op=oper[0][j]=0; ttile[0][j]^=op; if(j-1>=0) ttile[0][j-1]^=op; if(j+1
1) ttile[1][j]^=op; } for(int j=1;j
=0) ttile[j][k-1]^=op; if(k+1
View Code

 

转载于:https://www.cnblogs.com/acboyty/p/9527359.html

你可能感兴趣的文章
记录一些容易忘记的属性 -- UILabel
查看>>
STL之map UVa156
查看>>
再谈Vmware NAT的配置和路由流程
查看>>
javaScript数组去重方法汇总
查看>>
评价意见整合
查看>>
二、create-react-app自定义配置
查看>>
Android PullToRefreshExpandableListView的点击事件
查看>>
系统的横向结构(AOP)
查看>>
linux常用命令
查看>>
NHibernate.3.0.Cookbook第四章第6节的翻译
查看>>
例1-1
查看>>
马达调速器,直流马达调速器,直流调速器
查看>>
前端编码规范小记
查看>>
C#编程(二十五)----------接口
查看>>
c如何弹出保存路径/保存文件对话框
查看>>
HTML标签二
查看>>
Python 3语法小记(九) 异常 Exception
查看>>
使用shared memory 计算矩阵乘法 (其实并没有加速多少)
查看>>
Django 相关
查看>>
git init
查看>>