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. swift 学习- 22 -- 嵌套类型

    // 枚举 常备用于为特定的类 或 结构体实现某些功能, 类似的, 枚举可以方便的定义工具类 或 结构体, 从而为某个复杂的类型所使用, 为了实现这种功能, Swift 允许你定义 嵌套类型, 可以在 ...

  2. .NET Windows API库(Cjwdev.WindowsApi)版本2.2发布

    https://blog.cjwdev.co.uk/2011/06/12/net-windows-api-library-cjwdev-windowsapi-vesion-2-2-released/# ...

  3. windows+mysql集群搭建-三分钟搞定集群

    注:本文来源:  陈晓婵   <  windows+mysql集群搭建-三分钟搞定集群   > 一:mysql集群搭建教程-基础篇 计算机一级考试系统要用集群,目标是把集群搭建起来,保证一 ...

  4. Confluence 6 使用电子邮件可见

    Confluence 提供了 3 个电子邮件策略,这些策略 Confluence 管理员可以通过管理员控制台(Administration Console)进行配置: 公开(Public):电子邮件地 ...

  5. 设置 Confluence 6 日志

    Confluence 使用的是 Apache's log4j 日志服务.能够允许管理员通过编辑配置文件来控制日志的表现和日志输出文件.在系统中有 6 个日志输出级别,请参考 log4j logging ...

  6. spring-boot与spring-data-JPA的简单整合

    如何在boot中轻松使用JPA <!--首先引入JPA依赖--><dependency> <groupId>org.springframework.boot< ...

  7. 10进制 VS 2进制

    10进制 VS 2进制 时间限制: 1 Sec  内存限制: 32 MB 题目描述 样例输出 623 #include<stdio.h> #include<string.h> ...

  8. 80端口被占用 导致apach无法启动问题

    1.查找是哪个程序占用了80端口 netstat -ano 列出所有进程 观察 “本地地址” 列 找到对应的PID 我这里是4 简单的办法,打开任务管理器,查看PID是4的 是哪个进程. 发现是Sys ...

  9. 饮冰三年-人工智能-linux-05 Linux进程

    1:top 命令,查看cpu使用情况.(由于top是实时刷新,占用内存比较大) P:按照cpu使用率降序排列 M:按照内存使用率降序排列 2:free 命令,查看内存使用情况 free -m 以M为单 ...

  10. linux基础练习题(2)

    Linux命令作业(关卡二) 练习题1 理解操作系统的作用,以及各种操作系统的不同 要求: 为什么要有OS?没有OS能行吗?原因是什么? Linux内核指的是什么? Linux主要应用在哪些地方? 使 ...