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

题目链接:

http://poj.org/problem?id=3279

题目大意:

给你一个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<cstdio>
#include<algorithm>
#include<cstring>
#include<queue> using namespace std; int tile[][];
int ttile[][];
int oper[][]; int main()
{
int m,n;
scanf("%d%d",&m,&n);
for(int i=;i<m;i++)
for(int j=;j<n;j++)
scanf("%d",tile[i]+j); int fflag=false;
for(int i=;i<(<<n);i++)//枚举第一行的翻转
{
memcpy(ttile,tile,sizeof(tile)); for(int j=;j<n;j++)//处理第一行的翻转
{
int op;
if((i&(<<j))>)
op=oper[][j]=;
else
op=oper[][j]=;
ttile[][j]^=op;
if(j->=)
ttile[][j-]^=op;
if(j+<n)
ttile[][j+]^=op;
if(m>)
ttile[][j]^=op;
} for(int j=;j<m;j++)//处理2..m行的翻转
{
for(int k=;k<n;k++)
{
int op;
if(ttile[j-][k]==)
op=oper[j][k]=;
else
op=oper[j][k]=;
ttile[j][k]^=op;
ttile[j-][k]^=op;
if(k->=)
ttile[j][k-]^=op;
if(k+<n)
ttile[j][k+]^=op;
if(j+<m)
ttile[j+][k]^=op;
}
} int flag=true;
for(int i=;i<n;i++)//判断最后一行是否符合条件
{
if(ttile[m-][i]==)
{
flag=false;
break;
}
} if(flag)
{
fflag=true;
break;
}
} if(fflag)
{
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
if(j==n-)
printf("%d\n",oper[i][j]);
else
printf("%d ",oper[i][j]);
}
}
else
printf("IMPOSSIBLE\n");
return ;
}

poj 3279 Fliptile (简单搜索)的更多相关文章

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

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

  2. poj 3279 Fliptile(二进制搜索)

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

  3. POJ 3279 Fliptile(翻格子)

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

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

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

  5. POJ 3279(Fliptile)题解

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

  6. (简单) POJ 3279 Fliptile,集合枚举。

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

  7. POJ 3279 Fliptile (二进制+搜索)

    [题目链接]click here~~ [题目大意]: 农夫约翰知道聪明的牛产奶多. 于是为了提高牛的智商他准备了例如以下游戏. 有一个M×N 的格子,每一个格子能够翻转正反面,它们一面是黑色,还有一面 ...

  8. 【POJ 3279 Fliptile】开关问题,模拟

    题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...

  9. POJ 3279 Fliptile[二进制状压DP]

    题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...

随机推荐

  1. vue项目中使用百度统计

    统计有多少人访问了自己的网站(wap端pc web端都适用),或者更细的统计网站每个页面的访问量,可以使用百度统计 百度统计传送门 按提示注册登录即可 登录后-->管理-->新增网站,配置 ...

  2. 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa

    docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...

  3. .NET Core 3.0之深入源码理解HealthCheck(一)

    写在前面 我们的系统可能因为正在部署.服务异常终止或者其他问题导致系统处于非健康状态,这个时候我们需要知道系统的健康状况,而健康检查可以帮助我们快速确定系统是否处于正常状态.一般情况下,我们会提供公开 ...

  4. Xftp工具连接 报错: 无法与 “ 目标IP ” 连接

    这里是OpenSSH升级导致 查看sshd_config [root@sdw1 glibc]# vim /etc/ssh/sshd_config 查找后进行修改 [root@sdw1 glibc]# ...

  5. Node_exporter一键安装部署脚本(Shell)

    #!/bin/bash # # rhel7. 安装node_exporter 用于监控数据采集 # Usage: # sh addNode.sh #Logs: /var/log/messages #H ...

  6. Tensorflow搭建CNN实现验证码识别

    完整代码:GitHub 我的简书:Awesome_Tang的简书 整个项目代码分为三部分: Generrate_Captcha: 生成验证码图片(训练集,验证集和测试集): 读取图片数据和标签(标签即 ...

  7. pringBoot-MongoDB 索引冲突分析及解决【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  8. NumPy 学习 第三篇:矢量化和广播

    矢量化 矢量化是指用数组表达式替换显式的for循环.在Python中循环数组或其他跟数组类似的数据结构时,使用循环会涉及很多开销.NumPy中的矢量化操作把内部循环委托给高度优化的C和Fortran函 ...

  9. SpringCache自定义过期时间及自动刷新

    背景前提 阅读说明(十分重要) 对于Cache和SpringCache原理不太清楚的朋友,可以看我之前写的文章:Springboot中的缓存Cache和CacheManager原理介绍 能关注Spri ...

  10. 思科CISCO ASA 5521 防火墙 Ipsec 配置详解

    版本信息: Cisco Adaptive Security Appliance Software Version 9.9(2) Firepower Extensible Operating Syste ...