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

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. JVM内存分布和垃圾回收

    内存区域划分   程序计数器(Program counter Register) 描述  程序计数器(Program Counter Register)是一块较小的内存空间.它可以看作是当前线程执行的 ...

  2. 关于jQuery的append()和prepend()方法的小技巧

    最近工作上有个需求是要求一个自动向上滚动的列表,表有很多行,但只显示一行,每次滚动一行.很简单的一个功能,代码如下 <div class="scroll-area"> ...

  3. excel 补全全部空格

    首先全选列或者选中某列,按F5键.再按"定位条件„"button,选择空值,这样就把全部列的空格选中了,然后直接输入"你想要替换的值",再按Ctrl + 回车

  4. Python3标准库(一) 内置函数

    Python中内置了很多的函数,而学习标准库我们需要事先熟悉这些内置函数,所以在这里一个个进行介绍.有些函数比较简单,只给予文字描述,而有些函数会给出示例代码帮助理解. 1.abs(x):返回一个整数 ...

  5. NSDate 格式化 及 互转

    /* NSDateFormatter的作用 1.NSString -> NSDate 2.NSDate -> NSString */ void fmt_date_to_string(); ...

  6. HDU 4249 A Famous Equation(数位DP)

    题目链接:点击打开链接 思路:用d[i][a][b][c][is]表示当前到了第i位, 三个数的i位各自是a,b,c, 是否有进位 , 的方法数. 细节參见代码: #include<cstdio ...

  7. 删除windows服务 cygwin sshd

    $ cygrunsrv -R sshd     删除ssd服务

  8. LINKs: Xamarin.Forms + Prism

    LINK 1 - How to use Prism with Xamarin.Forms http://brianlagunas.com/first-look-at-the-prism-for-xam ...

  9. Ubuntu下用命令行快速打开各类型文件(转)

    nautilus /media/pm/文档/book/system/必读nautilus /media/pm/文档/book/android/ndk 内核/framerwork/android wai ...

  10. visual studio推荐的插件

    https://marketplace.visualstudio.com/items?itemName=EricLebetsamer.BootstrapSnippetPack https://mark ...