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 嗯,这个题我觉得很好啊,是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经典---也可以枚举)的更多相关文章

  1. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  2. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  3. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  4. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  5. POJ 3279 Fliptile(DFS+反转)

    题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子, ...

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

  7. POJ - 3279 Fliptile (枚举)

    http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...

  8. POJ 3279 Fliptile(反转 +二进制枚举)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13631   Accepted: 5027 Descrip ...

  9. POJ 3279 Fliptile (dfs+二进制)

    Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...

随机推荐

  1. Vue1.0到2.0变化

    一.生命周期 二.代码片段 在vue1.0中可以在template编写时出现: <template> <div>第一行</div> <div>第二行&l ...

  2. spring springMvc spring-boot spring-cloud分别是什么

    本文来源于:克己习礼成仁   的<spring springMvc spring-boot spring-cloud分别是什么> 什么是spring 关于spring的定义无论是从官方还是 ...

  3. Confluence 6 临时目录(安装目录)

    temp 目录是由 Java 运行时进行配置的,同时一些 Confluence 的组件将会写入历史文件或者锁定文件到这个目录中. 临时目录位于安装目录下的 /temp 目录中. 希望修改这个目录的位置 ...

  4. Confluence 6 PostgreSQL 创建数据库和数据库用户

    一旦你成功的安装了 PostgreSQL 数据库: 创建一个数据库用户,例如 confluenceuser. 你的新用户必须能够  创建数据库对象(create database objects) 和 ...

  5. extra过滤

    extra extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None) 有些 ...

  6. mysql where和having的区别

    简单描述:需要查询一个数量count,于是做分组查询后,发现有的数据没有过滤掉,于是就想加上过滤条件,就在group by后边写了where ,发现不好使,直接就报错了,查了一下,where只能写在g ...

  7. Niagara物联网框架机制二(笔记)

    一.Niagara框架 1.一个Niagara 系统中有四种典型的Programs,这些程序间的关系及其网络通讯关系可通过下面的通讯图表解释 2. Niagara  Programs station ...

  8. fdisk命令

    fdisk -l命令详解 Disk /dev/sda: 53.7 GB, 53687091200 bytes 块设备名称为/dev/sda,此设备的大小为53.7GB,这个数字不是特别精确,我系统是5 ...

  9. 为什么访问json接口出现文件下载

    在IE9,10,11下,当服务器端返回数据格式为json,且明确设置Content-Type为”application/json;charset=utf-8“时,会提示文件下载.如图所示: 解决办法是 ...

  10. selenium+python-autoit文件上传

    前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...