POJ_3279_(dfs)(状态)
---恢复内容开始---
| 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
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
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)(状态)的更多相关文章
- POJ2743Mobile Computing[DFS 状态压缩]
Mobile Computing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 666 Accepted: 224 ...
- uva10160(dfs+状态压缩)
题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...
- 2101 可达性统计(拓扑排序/dfs+状态压缩)
[题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...
- HDU 4921 Map DFS+状态压缩+乘法计数
算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...
- POJ 1632 Vase collection【状态压缩+搜索】
题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS Memory Limit: 10000K Total ...
- hadoop单机
Hadoop安装教程——单机模式 博客分类: 大数据 Hadoop是MapReduce的开源实现,网上有很多相关的文章,但是很多不全,有的有点乱,本人Ubuntu小白,Hadoop初学者,根据别人的资 ...
- hadoop1.2.1伪分布模式配置
1.修改core-site.xml,配置hdfs <configuration> <property> <name>fs.default.name</name ...
- Ubuntu环境下手动配置Hadoop1.2.1
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- 安装Hadoop系列 — 安装Hadoop
安装步骤如下: 1)下载hadoop:hadoop-1.0.3 http://archive.apache.org/dist/hadoop/core/hadoop-1.0.3/ 2)解压文 ...
随机推荐
- 安装 openCV 2.4.10
近期试验了一下 ubuntu 12.06 (x86) 安装.openCV 安装脚本 最好的文章是 https://help.ubuntu.com/community/OpenCV. 它提供一个脚本( ...
- unity3d杂记
由于公司用unity3d开发客户端部分,今天去参加了下unity3d成都开发者大会.在这里简单记录一下会议里关于unity3d的内容. 说到unity3d,第一次知道的时候也是大概3年前的事情.这几年 ...
- BZOJ 3007 解救小云公主 二分答案+对偶图
题目大意:给定一个矩形和矩形内的一些点.求一条左下角到右上角的路径.使全部点到这条路径的最小距离最大 最小距离最大.果断二分答案 如今问题转化成了给定矩形中的一些圆形障碍物求左下角和右上角是否连通 然 ...
- 在SAE搭建Python+Django+MySQL(基于Windows)
为了与时俱进,工作闲余開始研究Python,刚一接触就被Python这"优雅"的语法吸引住.后来接触到了Django.尽管还没有太深入的研究.但对这样的新概念的WEB开发非常感兴趣 ...
- 【翻译自mos文章】将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边
将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边 參考原文: How To Extract Datapump File From ASM Diskgroup To Local Files ...
- Linux gadget驱动分析2------设备识别过程
设备连上主机之后,设备驱动做了的事. 设备连上host的port之后,主机端会有一套策略发送请求获取device的一系列描述符.进行枚举过程.找到适合该device的驱动. 这样就可以与device进 ...
- YTU 2715: 函数---判断某年某月某日是这一年中的第几天
2715: 函数---判断某年某月某日是这一年中的第几天 时间限制: 1 Sec 内存限制: 128 MB 提交: 380 解决: 155 题目描述 在主程序(main)中输入某年某月某日,例如2 ...
- 【转】Android HTML5 Video视频标签自动播放与自动全屏问题解决
为了解决 HTML5Video视频标签自动播放与全屏问题,在网上找了很多相关资料,网上也很多关于此问题解决方法,但几乎都不能解决问题,特别对各大视频网站传回来的html5网页视频自动播放与全屏问题,我 ...
- [BZOJ 1698] 荷叶池塘
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1698 [算法] 最短路 [代码] #include<bits/stdc++.h ...
- 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
最近在新项目中突然出现了 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...