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)解压文 ...
随机推荐
- Hive之单独部署机器
环境说明 CentOS7,hadoop-2.6.5,hive-1.2.2,MariaDB-5.5.60,jdk-1.8 假设hive机已经安装好了MariaDB(已启动且已创建好hive账号,对hiv ...
- Mybatis在Spring环境下的启动顺序
主要看三个类: mybatis-spring-1.2.2.jar包 -> org.mybatis.spring.SqlSessionFactoryBean mybatis-3.2.6.jar包 ...
- putty SSH出现乱码
解决方法如下: 打开PuTTY主程序 选择window-〉Appearance-〉Font settings-〉点击Change.按钮,字体中选择"新宋体". 选择window-〉 ...
- 一个经典的消费者和生产者的实现(linux )
#include <stdio.h> #include <pthread.h> #define BUFFER_SIZE 16 // 缓冲区数量 struct ...
- CSS垂直居中完美有用实例
<!DOCTYPE HTML> <html> <head> <meta meta chars ...
- 桌面系统集成WEB认证系统方案
最近做的一个项目,有WEB版.WPF版.手机版.领导想集成集团的一个现成的认证系统,姑且称这个认证系统名为 W4认证系统. W4认证系统有如下特点: 1.现成的 2.是个单点登录系统 3.不支持oAu ...
- BNUOJ 13098 约瑟夫环问题
C. Josephus Problem 题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=7095#problem/C 题目描述 The hist ...
- new (C# Reference)
https://msdn.microsoft.com/en-us/library/51y09td4.aspx In C#, the new keyword can be used as an oper ...
- Head First 设计模式 —— 单例模式(Singleton)
单例模式简要定义:单例模式确保一个类只有一个实例,并提供一个全局访问点. 1. 如何保证一个类只有一个实例,且这个实例易于被访问? lazy evaluation:在用到的时候才创建对象. 全局变量: ...
- Luogu3674小清新人渣的本愿
https://zybuluo.com/ysner/note/1109536 题面 给你一个序列a,长度为n,有m次操作,每次询问一个区间 是否可以选出两个数它们的差为x 是否可以选出两个数它们的和为 ...