(POJ-3279)Fliptile (dfs经典---也可以枚举)
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
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
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 嗯,这个题我觉得很好啊,是dfs入门的经典题型啊,关键是翻转一次会影响周围的状态,除了用dfs以外,也可以用枚举(开关问题),
这里我只使用了dfs(枚举还不会orz)。
dfs搜索的话,肯定不是按一条一条路径来搜,我们可以dfs第一行的所有翻转情况,然后从第一行的情况可以递推出下面行的状态,
下面行中只要上行对应列有黑色就把这个位置翻转,因为会影响周围,所有上面行也会翻转。
至于字典序,因为一开始dfs第一行的时候是直接搜到底的,所以后面的dfs情况都是从最右边开始向左翻转的,如果有相同的最小值,那么记录下的一定是最右边的情况。
注意路径的回溯
#include<bits/stdc++.h> using namespace std; int m,n;
int ans; int maps[][];
int vis[][];
int vistmp[][];
int way[][] = {,,,,,,,-,-,};
void inif()
{
memset(vis,,sizeof(vis));
memset(vistmp,,sizeof(vistmp));
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&maps[i][j]);
}
}
}
void flip(int x,int y)
{
vistmp[x][y] ^= ;
for(int i=;i<;i++)
{
int xx = x+way[i][];
int yy = y+way[i][];
if(<xx && xx<=m && yy> && yy <=n)
{
maps[xx][yy] ^= ;
}
}
}
void solve(int k,int cnt)
{
if(cnt >= ans)return;
if(k == m)
{
int i;
for(i=;i<=n;i++)
if(maps[m][i])break;
if(i <= n)return;
ans = cnt;
memcpy(vis,vistmp,sizeof(vistmp));
return;
}
for(int i=;i<=n;i++)
{
if(maps[k][i])
{
flip(k+,i);
cnt++;
}
}
solve(k+,cnt);
for(int i=;i<=n;i++)
{
if(vistmp[k+][i])flip(k+,i);
}
return;
}
void dfs(int k,int cnt)
{
if(cnt >= ans)return;
else if(k == n+)
{
solve(,cnt);
return;
}
else
{
dfs(k+,cnt);
flip(,k);
dfs(k+,cnt+);
flip(,k);
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
ans = 0x3f3f3f3f;
inif();
dfs(,);
if(ans == 0x3f3f3f3f)printf("IMPOSSIBLE\n");
else
{
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(j != )
printf(" %d",vis[i][j]);
else
printf("%d",vis[i][j]);
}
puts("");
}
}
}
}
(POJ-3279)Fliptile (dfs经典---也可以枚举)的更多相关文章
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(DFS+反转)
题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子, ...
- 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 (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
- POJ 3279 Fliptile(反转 +二进制枚举)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13631 Accepted: 5027 Descrip ...
- POJ 3279 Fliptile (dfs+二进制)
Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...
随机推荐
- 3790:最短路径问题(HDU)
Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Inp ...
- laravel 框架后台主菜单接口
后台菜单调用接口:/admin/manages ManageRepository类: 每个路由中注册: 等等: 最后后台菜单返回:
- java子类对象和成员变量的隐写&方法重写
1.子类继承的方法只能操作子类继承和隐藏的成员变量名字类新定义的方法可以操作子类继承和子类新生命的成员变量,但是无法操作子类隐藏的成员变量(需要适用super关键字操作子类隐藏的成员变量.) publ ...
- 性能测试四十一:sql案例之慢sql配置、执行计划和索引
MYSQL 慢查询使用方法MYSQL慢查询介绍分析MySQL语句查询性能的问题时候,可以在MySQL记录中查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”.MYSQL自带的慢查 ...
- 微信录音文件上传到服务器以及amr转化成MP3格式,linux上转换简单方法
微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...
- js FileReader 笔记
以上传图片为例 通过input type='file' 上传完成图片后,获取图片 $('#input').files[0] var reader = new FileReader(); read ...
- Python模拟人猜数过程(折半查找)
import random# (0,1000)随机产生一个数key = random.randint(1,1000)# 用来统计猜的次数count = 0 # 定义一个折半查找的函数def BinSe ...
- 微信小程序--代码构成---JSON 配置
在上一章中,我们通过开发者工具快速创建了一个 QuickStart 项目.你可以留意到这个项目里边生成了不同类型的文件: .json 后缀的 JSON 配置文件 .wxml 后缀的 WXML 模板文件 ...
- social psychology 10th David G. Myers
Social psychology is a science that studies the influences of our situations, with special attention ...
- SQL查询数据并插入新表
SQL查询数据并插入新表 --如果接受数据导入的表已经存在 insert into 表 select * from tablename --如果导入数据并生成表 select * into 表 fro ...