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

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. 函数式语言(functional language)定义、函数式语言的种类以及为什么函数式语言会流行起来的学习笔记

    一.什么是函数式语言?       函数式语言一类程序设计语言,是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据结构的能力,可以把某一数据 ...

  2. 22、Java并发性和多线程-Java中的读/写锁

    以下内容转自http://ifeve.com/read-write-locks/: 相比Java中的锁(Locks in Java)里Lock实现,读写锁更复杂一些.假设你的程序中涉及到对一些共享资源 ...

  3. eclipse的Java项目修改后要不要重启tomcat问题

    tomcat服务器重新部署工程或者修改了项目的代码就必须重启tomcat吗? 答: omcat服务器重新部署工程或者修改了项目的代码就必须重启tomcat吗?有没有不重启的方法,或者其他高效点的,让服 ...

  4. [JavaEE] JTA, Java Transaction API, Repository for DB opreations

    Mainly two things: 1. For all the creating and deleting opreations for the DB, we want to use 'REQUI ...

  5. Office EXCEL如何批量把以文本形式存储的数字转换为数字

    如果"以文本形式存储的数字"不多,则点击右边的感叹号,转换为数字即可.但是如果有几万个单元格就不能这样做了.   先把他旁边的一列填充为1(选中该列,然后按Ctrl+F查找,按列查 ...

  6. Android开发系列(二十四):Notification的功能与使用方法

    关于消息的提示有两种:一种是Toast,一种就是Notification.前者维持的时间比較短暂.后者维持的时间比較长. 并且我们寻常手机的应用比方网易.贴吧等等都有非常多的推送消息,就是用Notif ...

  7. window.setTimeout() 和window.setInterval() 的差别

    setTimeout 和setInterval的功能都是经过某一个时间段后发生某件指定的事件或者方法. 如window.setTimeout("sleep()",5000);指的是 ...

  8. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  9. 极客标签互动课程系列 - Javascript生成SVG动画素描特效

    课程描写叙述:在这个课程中,我们将介绍SVG.而且介绍怎样使用javascript来控制SVG生成素描动画效果 课程地址:http://www.gbtags.com/gb/gbliblist/21.h ...

  10. hdu5305(2015多校2)--Friends(状压,深搜)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...