POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末。那么先是题目分析:
【一句话题意】
给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑色所需翻转的是哪些棋子(这句话好长...)。
【题目分析】
盯着奇怪的题目看了半天发现和奶牛没什么卵关系..奶牛智商高了产奶高是什么鬼说法...
这题刚开始被放到搜索的分类下了..然而这和搜索有什么关系..没经验所以想了各种和搜索沾边的方法,结果没想出解法,直到看了网上的题解,压根不是搜索啊有木有。
然后这题网上给的分类是简单题..简单题..蒟蒻跪了..
好了开始说解法。首先根据题目,每次操作都会影响到周围的“棋子”,而要使得每个1都被反转为0,那么我们就应当每次都反转1下方的棋子以改变1为0.
那么,当我们处理过1到n-1行的时候,前n-1行就都已经是0了,最后我们只需要检查最后一行是不是全部为0就可以检查这次的出操作是否正确了。如果正确且最小,那就存起来。最后输出,万事大吉。
当然,因为我们要改变第x行的1为0需要反转的是x+1行的位置。而这个整个规则是我们验证一组操作是否能达到目的所用的,那么我们需要在验证前先确定操作(没操作怎么验证..)。
于是根据规则内容可知,只需要能确认第一行的翻转情况,就能够推出下面所有的翻转情况并验证是否正确。于是需要做的就是枚举第一行的情况了。
【算法流程】
整个代码分了四部分,处理输入部分没啥可说的,接下来就是doWork干活部分了..
需要干的活就是枚举第一行的所有情况然后对于每次枚举都计算验证是否符合要求以及反转所需的次数。其中,第一行的状态数量可以使用左移运算优化(效率比pow高),于是总共枚举的数量就有1<<col次。另外,因为这使得一行的状态是由一个数字保存的,所以依然使用位运算取得是否翻转的状态。
将枚举好的一次首行状态存好后就可以交给calc计算和验证了。验证就是通过上述翻转规则操作。其中get(x,y)为取得某个位置是否为1的状态的方法(过程)。
最后检查结果就好。下面是代码。
哦对了,我似乎滥用了each宏定义...无视即可。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <queue>
#define each(i,n) (int i=1;i<=(n);++i)
#define INF 0x3f3f3f3f using namespace std; int row,col;
int arr[][];
int flip[][],ans[][];
int dir[][] = {
,, ,, ,-, -,, ,
}; int get(int x,int y) {
int c = arr[x+][y+];
for(int i = ;i<;i++) {
int dx = x + dir[i][];
int dy = y + dir[i][];
if(dx >= && dx<row && dy>= && dy<col) {
c+=flip[dx][dy];
}
}
return c&; // with flip state, if odd return 1
} int calc() {
for each(i,row-) {
for(int j = ;j<col;j++) {
if(get(i-,j)) ++flip[i][j];
}
}
for(int i=;i<col;i++) { // check last line
if(get(row-,i)) return ;
}
int cnt = ;
for (int i=;i<row;i++) { //统计翻转的次数
for (int j=;j<col;j++) {
cnt += flip[i][j];
}
}
return cnt;
} void doWork() {
int cnt = INF;
for(int i=;i<(<<col);i++) { //from 0000 to 1111
memset(flip,,sizeof(flip));
for(int j=;j<col;j++) {
flip[][col-j-] = (i>>j)&; //get pos state form binary number
}
int num = calc();
if (num<cnt && num!=) {
cnt = num;
memcpy(ans,flip,sizeof(flip));
}
}
if (cnt==INF) printf("IMPOSSIBLE\n");
else {
for (int i=;i<row;i++) {
printf("%d",ans[i][]);
for each(j,col-) {
printf(" %d",ans[i][j]);
}
printf("\n");
}
}
} int main() { while(~scanf("%d%d",&row,&col)) {
memset(arr,,sizeof(arr));
for each(i,row) {
for each(j,col) {
scanf("%d",&arr[i][j]);
}
} doWork();
} }
题目链接:Fliptile(POJ 3279)
题目属性:枚举 简单优化 (我真不知道算不算搜索...)
相关题目:poj3276
题目原文:
【desc】
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".
【In】
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
【Out】
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
【SampIn】
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
【SampOut】
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
POJ 3279(Fliptile)题解的更多相关文章
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279 - Fliptile - [状压+暴力枚举]
题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...
- poj 3279 Fliptile (简单搜索)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16558 Accepted: 6056 Descrip ...
- 【POJ 3279 Fliptile】开关问题,模拟
题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...
- POJ 3279 Fliptile[二进制状压DP]
题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...
- POJ - 3279 Fliptile (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
- poj 3279 Fliptile(二进制)
http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
随机推荐
- 由于jsp include 很多文件后导致java类大小超过65535 bytes 的解决方法(转载)
昨天,我遇到了一個讓我很頭疼的問題. 我做了一個共通的jsp,單只測它是ok的,可是,放在別的jsp中include它,就會報錯如標題所示:The code of method _jspService ...
- QT-利用C++仿制windown自带的记事本程序V1.0
下班无事, 发现QT还是很好用的, 就仿制windows的记事本做了一个,未彻底DEBUG, 先拿来分享下. windows记事本大概是这样的: 大概分为以下几步: 1. 界面用QT代码写,即可, Q ...
- GDAL 生成shp文件
附件:http://pan.baidu.com/s/1i3GPwrV(C#版GDAL接口.dll) 示例程序: http://pan.baidu.com/s/1jpIKQ (程序是在vs2008 x ...
- mysql学习(补充)
修改表名 rename table olderName to newerName; \c 结束不执行 设置字符集 set names gbk; mysql类型 数值型 属性修饰符 zerofill u ...
- Android code wiki
Android code wiki Tip1: 类的全局静态变量的使用,这样可以静态变量只分配一次内存,可以不通过类的对象也就是可以通过类名直接使用该变量.(使用场景:Request_Code ,Re ...
- PHP扩展开发之PHP的启动与终止
PHP程序的启动可以看做是两个概念上的启动,终止也有两个概念上的终止.其中一个是PHP作为Apache(拿它举例,板砖勿扔)的一个模块的启动与终止, 这次启动php会初始化一些必要数据,比如与宿主Ap ...
- nginx自定义模块编写-根据post参数路由到不同服务器
nginx可以轻松实现根据不同的url 或者 get参数来转发到不同的服务器,然而当我们需要根据http包体来进行请求路由时,nginx默认的配置规则就捉襟见肘了,但是没关系,nginx提供了强大的自 ...
- GDAL库学习笔记(1):无缝拼接Google卫星图
开工之前要先了解一下瓦片地图,瓦片地图金字塔模型是一种多分辨率层次模型,从瓦片金字塔的底层到顶层,分辨率越来越低,但表示的地理范围不变.实现原理就是,首先确定地图服务平台所要提供的缩放级别的数量N,把 ...
- QT:程序忙碌时的进度条——开启时间循环,等结束的时候再退出
当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了.PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程 ...
- QT:“下载速度柱状图”的模拟实现——思路真好,会动脑筋,连我都有了启发(这个思路好像是通用的)
不知是哪个版本的迅雷,有个“下载速度柱状图”的小界面,我比较喜欢(只不过最新版本的迅雷却没了),所以决定来山寨一个.当然,这个山寨品不能下载文件,呵呵. 思路:1:将界面的背景涂成黑色2:每隔0.1秒 ...