题意转载自https://www.cnblogs.com/blumia/p/poj3279.html

题目属性:DFS

相关题目:poj3276

题目原文:
【desc】
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".
【In】
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 
【Out】
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location. 
【SampIn】
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
【SampOut】
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

【一句话题意】

给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑色所需翻转的是哪些棋子(这句话好长...)。

【题目分析】

盯着奇怪的题目看了半天发现和奶牛没什么卵关系..奶牛智商高了产奶高是什么鬼说法...

这题刚开始被放到搜索的分类下了..然而这和搜索有什么关系..没经验所以想了各种和搜索沾边的方法,结果没想出解法,直到看了网上的题解,压根不是搜索啊有木有。

然后这题网上给的分类是简单题..简单题..蒟蒻跪了..

好了开始说解法。首先根据题目,每次操作都会影响到周围的“棋子”,而要使得每个1都被反转为0,那么我们就应当每次都反转1下方的棋子以改变1为0.

那么,当我们处理过1到n-1行的时候,前n-1行就都已经是0了,最后我们只需要检查最后一行是不是全部为0就可以检查这次的出操作是否正确了。如果正确且最小,那就存起来。最后输出,万事大吉。

当然,因为我们要改变第x行的1为0需要反转的是x+1行的位置。而这个整个规则是我们验证一组操作是否能达到目的所用的,那么我们需要在验证前先确定操作(没操作怎么验证..)。

于是根据规则内容可知,只需要能确认第一行的翻转情况,就能够推出下面所有的翻转情况并验证是否正确。于是需要做的就是枚举第一行的情况了。

【算法流程】

#include<stdio.h>
#include<string.h>
int n,m;
int count,min;
int s[20][20],s1[20][20];
int lg[20][20];
int cg(int a,int b)
{
int i,j;
s[a][b]=!s[a][b];
if(a-1>0)
s[a-1][b]=!s[a-1][b];
if(a+1<=n)
s[a+1][b]=!s[a+1][b];
if(b-1>0)
s[a][b-1]=!s[a][b-1];
if(b+1<=m)
s[a][b+1]=!s[a][b+1];
}
int do1(int a)
{
count=0;
int k=1;
int i,j;
memcpy(s,s1,sizeof(s));
memset(lg,0,sizeof(lg));
for(j=1;j<=m;j++)
{
if((a&(1<<(m-j)))>0) //记得a&1<<(m-j)要打括号,不然会是a&(1<<(m-j)>0)
{
cg(1,j),count++,lg[1][j]=1;
//printf("w%d %d\n",a,a&(1<<(m-j)));
}
}
for(i=1;i<n;i++)
for(j=1;j<=m;j++)
{
if(s[i][j]==1)
cg(i+1,j),count++,lg[i+1][j]=1;
}
for(j=1;j<=m;j++)
if(s[n][j]==1)
k=0;
//if(k==1)
//printf("%d %d\n",count,a);
return k;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
min=n*m+1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&s1[i][j]);
k=-1;
for(i=0;i<(1<<m);i++)//第一行翻动情况
{
if(do1(i)==1&&min>count)
{
min=count;
k=i;
//printf("%d\n",min);
}
}
if(k==-1)
printf("IMPOSSIBLE\n");
else
{
do1(k);
//printf("%d",k);
for(i=1;i<=n;i++)
{
j=1;
printf("%d",lg[i][j]);
for(j=2;j<=m;j++)
printf(" %d",lg[i][j]);
printf("\n");
}
}
}
return 0;
}

poj3279(dfs+二进制枚举思路)的更多相关文章

  1. UVa 818 切断圆环链(dfs+二进制枚举)

    https://vjudge.net/problem/UVA-818 题意:有n个圆环,其中有一些已经扣在了一起.现在需要打开尽量少的圆环,使得所有圆环可以组成一条链,例如,有5个圆环,1-2,2-3 ...

  2. poj-3279 poj-1753(二进制枚举)

    题目链接:http://poj.org/problem?id=3279 题目大意: 有一个m*n的棋盘(1 ≤ M ≤ 15; 1 ≤ N ≤ 15),每个格子有两面分别是0或1,每次可以对一个格子做 ...

  3. Good Bye 2015B(模拟或者二进制枚举)

    B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

  5. UVA1354-Mobile Computing(二进制枚举子集)

    Problem UVA1354-Mobile Computing Accept:267  Submit:2232 Time Limit: 3000 mSec  Problem Description ...

  6. HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...

  7. 51nod 1363 最小公倍数的和 欧拉函数+二进制枚举

    1363 最小公倍数之和 题目来源: SPOJ 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 给出一个n,求1-n这n个数,同n的最小公倍数的和.例如:n = 6,1,2,3 ...

  8. CUGBACM_Summer_Tranning1 二进制枚举+模拟+离散化

    整体感觉:这个组队赛收获还挺多的.自从期末考试以后已经有一个多月没有 做过组队赛了吧,可是这暑假第一次组队赛就找回了曾经的感觉.还挺不错的!继续努力!! 改进的地方:这次组队赛開始的时候题目比較难读懂 ...

  9. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

随机推荐

  1. Exception:public class feign.codec.EncodeException feign.codec.EncodeException: 'Content-Type' cannot contain wildcard type '*'

    一.异常出现的场景  Spring Cloud 服务A通过feign调用服务B;之前是好好的,但今天突然就不好了,抛以下异常===> 出现原因补充,Spring Boot默认的JSON方式 Ja ...

  2. 2018-02-16 GetSameTypeQuestion

    '目前存在的BUG '图片补丁存在多个URL Private Declare Function URLDownloadToFile Lib "urlmon" Alias " ...

  3. 文件上传MultipartBody使用方法

    最近有使用一个文件上传的功能,需要在请求中添加文件,一起传给服务器 Okhttp提供了这个文件添加然后上传的功能 下面给出核心的代码,然后分析一下 //多个文件上传,Filelist private ...

  4. DAG最长路问题 hdu-1224

    用DFS+记忆化写了一下,拓扑排序+DP的我还没弄明白.据说Codeforces 721C就是这类题目,因为有费用限制,DFS不太好写,有时间把DP法想明白来. #include <iostre ...

  5. hammer.js方法总结(只做了一个简单的demo)

    html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  6. re正则表达式方法

    目录 1.python正则匹配 1.1 re.search 正则表达式 1.2 re.match() 正则表达式 1.3 re.match与re.search的区别 1.4 检索和替换 1.5 正则表 ...

  7. Hongcow Buys a Deck of Cards CodeForces - 744C (状压)

    大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...

  8. 『Re』正则表达式模块_常用方法记录

    『Re』知识工程作业_主体识别 一个比较完备的正则表达式介绍 几个基础函数 re.compile(pattern, flags=0) 将正则表达式模式编译成一个正则表达式对象,它可以用于匹配使用它的m ...

  9. Shiro中Realm

    6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系   即用户-角色 ...

  10. 内存池、进程池、线程池介绍及线程池C++实现

    本文转载于:https://blog.csdn.net/ywcpig/article/details/52557080 内存池 平常我们使用new.malloc在堆区申请一块内存,但由于每次申请的内存 ...