---恢复内容开始---

Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8219   Accepted: 3071

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 题意:给定一个只含0和1的矩阵,每次可以翻转一个点以及它周围的点(上下左右)。问最少需要多少步。
一来没什么头绪,看了题解,说,枚举第一行的所有状态,然后往后推后面的行。以为自己肯定搞不出来,结果还搞出来了,开心!
不过这题被别人归类在简单题里。。。永远都是菜鸟。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20
#define INF 99999999 int n,m;
int gra[N][N],tmp[N][N],tmp1[N][N]; void flip(int r,int i)
{
gra[r][i]^=;
if(i->=)
gra[r][i-]^=;
if(i+<m)
gra[r][i+]^=;
if(r->=)
gra[r-][i]^=;
if(r+<m)
gra[r+][i]^=;
} void change(int r,int x)
{
for(int i=; i<m; i++)
{
if((<<(m--i))&x)
flip(r,i);
}
} int cnt,res=INF;
void dfs(int r)
{
if(r==n)
{
int flag=;
for(int i=; i<m; i++)
if(gra[n-][i]==)
{
flag=;
break;
}
if(flag)
{
if(res>cnt)
{
res=cnt;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
tmp[i][j]=tmp1[i][j];
}
}
return;
}
int x=;
memset(tmp1[r],,sizeof(tmp[r]));
for(int i=; i<m; i++)
{
if(gra[r-][i]==)
{
tmp1[r][i]=;
cnt++;
x+=(<<(m--i));
}
}
change(r,x);
dfs(r+);
change(r,x);
} int main()
{ scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
for(int j=; j<m; j++)
scanf("%d",&gra[i][j]);
cnt=;
res=INF;
for(int i=; i<(<<m); i++)
{
cnt=;
memset(tmp1[],,sizeof(tmp[]));
for(int j=; j<m; j++)
{
if((<<j)&i)
{
cnt++;
tmp1[][m--j]++;
}
}
change(,i);
dfs();
change(,i);
}
if(res<INF)
{
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
printf("%d",tmp[i][j]);
if(j==m-)
printf("\n");
else
printf(" ");
}
}
else
printf("IMPOSSIBLE\n");
return ;
}
 

---恢复内容结束---

POJ_3279_(dfs)(状态)的更多相关文章

  1. POJ2743Mobile Computing[DFS 状态压缩]

    Mobile Computing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 666   Accepted: 224   ...

  2. uva10160(dfs+状态压缩)

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

  3. 2101 可达性统计(拓扑排序/dfs+状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...

  4. HDU 4921 Map DFS+状态压缩+乘法计数

    算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...

  5. POJ 1632 Vase collection【状态压缩+搜索】

    题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS   Memory Limit: 10000K Total ...

  6. hadoop单机

    Hadoop安装教程——单机模式 博客分类: 大数据 Hadoop是MapReduce的开源实现,网上有很多相关的文章,但是很多不全,有的有点乱,本人Ubuntu小白,Hadoop初学者,根据别人的资 ...

  7. hadoop1.2.1伪分布模式配置

    1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...

  8. Ubuntu环境下手动配置Hadoop1.2.1

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  9. 安装Hadoop系列 — 安装Hadoop

    安装步骤如下: 1)下载hadoop:hadoop-1.0.3     http://archive.apache.org/dist/hadoop/core/hadoop-1.0.3/   2)解压文 ...

随机推荐

  1. 模拟赛 Problem 3 经营与开发(exploit.cpp/c/pas)

    Problem 3 经营与开发(exploit.cpp/c/pas) [题目描述] 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以“EX”为开头的英语单词. eXpl ...

  2. spring-boot-starter-actuator(健康监控)配置和使用

    在生产环境中,需要实时或定期监控服务的可用性.Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看.相关功能统计等. 集成: <depen ...

  3. Ubuntu 16.04安装NASM汇编IDE-SASM

    在Linux下,尤其是Ubuntu,SASM工具应该是用来开发汇编最好用的IDE,小巧且支持调试.支持的编译器有:NASM, MASM, GAS, FASM. 安装步骤: 下载: http://dow ...

  4. TDSTCPServerTransport 的Filters

    TDSTCPServerTransport 的Filters TDSTCPServerTransport 的 Filter 属性,可以对传递的数据进行加密,压缩,再修改等,有 点注入的概念.默认情况下 ...

  5. MVC模式利用xib文件定制collectionCell

    数据来源于豆瓣网~仅供学习交流~ 本实例练习用到了SDWebImage框架:实现从网络端下载图片的功能 下载地址:https://github.com/rs/SDWebImage 实现效果及框架: x ...

  6. JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)

    Java基础语法(变量.常量.数据类型.运算符与数据类型转换) 1.变量 Java中.用户能够通过指定数据类型和标识符来声明变量.其基本的语法为: DataType identifier; 或 Dat ...

  7. Codeforces 91C Ski Base 加边求欧拉回路数量

    题目链接:点击打开链接 题意: 给出n个点m条无向边的图 開始图里没有边.每次加一条边,然后输出图里欧拉回路的条数. 思路: We will count the number of ski bases ...

  8. hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...

  9. cocos2d-x 多触点监听

    [cpp] view plaincopy //首先到cocos2d-x项目下的ios目录下.找到AppController.mm文件,在函数 didFinishLaunchingWithOptions ...

  10. Qt Quick综合实例之文件查看器

    假设你基于Qt SDK 5.3.1来创建一个Qt Quick App项目,项目模板为你准备的main.qml文档的根元素是ApplicationWindow或Window.这次我们就以Applicat ...