Flip Game poj1753
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 32961 | Accepted: 14407 |
Description
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
dfs枚举
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int Map[][],a[]={,,,,-},b[]={,,,-,};
int flg,step; int judge(int Map[][])
{
int i,j;
for(int i=;i<=;i++)
for(j=;j<=;j++)
{
if(Map[i][j]!=Map[][])
return ;
}
return ;
} int flip(int r,int c)
{
for(int i=;i<;i++)
{
if(Map[r+a[i]][c+b[i]]==)
Map[r+a[i]][c+b[i]]=;
else
Map[r+a[i]][c+b[i]]=;
}
} void dfs(int r,int c,int deep)
{
int i,j;
if(deep==step)
{
flg=judge(Map);
return;
}
if(flg== || r>)
{
return;
}
flip(r,c);
if(c<)
dfs(r,c+,deep+);
else
dfs(r+,,deep+);
flip(r,c);
if(c<)
dfs(r,c+,deep);
else
dfs(r+,,deep);
return;
} int main()
{
int i,j;
char k;
memset(Map,,sizeof(Map));
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
scanf("%c",&k);
if(k=='b')
Map[i][j]=;
}
getchar();
}
for(step=;step<=;step++)
{
dfs(,,);
if(flg)
break;
}
if(flg)
printf("%d\n",step);
else
printf("Impossible\n");
return ;
}
Flip Game poj1753的更多相关文章
- dfs关于按钮问题(flip游戏POJ1753)以及和bfs的区别+板子
DFS深度搜索:之前一直和bfs的用法搞不太清楚:写了题才能慢慢参透吧,看了别的博客的代码,感觉能更好理解dfs在图中的应用: 这个题目的意思是一个人去救另一个人,找出最短的寻找路径: #includ ...
- poj1753 Flip Game(BFS+位压缩)
题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
- poj1753,Flip Game,ArrayDeque<Node>
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30449 Accepted: 13232 Descr ...
- POJ1753 Flip Game(bfs、枚举)
链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...
- poj1753 Flip Game
题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...
- POJ1753——Flip Game
Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...
- POJ-1753 Flip Game---二进制枚举子集
题目链接: https://vjudge.net/problem/POJ-1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白-> ...
- POJ-1753 Flip Game (BFS+状态压缩)
Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...
- POJ1753 Flip Game(位运算+暴力枚举)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
随机推荐
- linux下奇怪的“重名”文件
是这样的,文件创建是通过远程命令来进行的. 就是在表单中输入命令,然后使用php的system来执行. 表单使用的是多行文本输入框. 可能某次使用 类似touch这种命令创建文件的时候多按了一次回车, ...
- dbo与db_owner区别
dbo 是具有在数据库中执行所有活动的暗示性权限的用户.将固定服务器角色 sysadmin 的任何成员都映射到每个数据库内称为 dbo 的一个特殊用户上.另外,由固定服务器角色 sysadmin 的任 ...
- SQL的主键和外键约束(转)
SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...
- HGE引擎之hgeSprite
一.hgeSprite类 hgeSprite是一个精灵实体的HGE帮助类. 1.构造函数 创建和初始化一个hgeSprite对象. hgeSprite(HTEXTURE tex, float x, f ...
- Eclipse启动认定SDK地址,出现Error executing aapt
我把安卓的SDK安装在D盘,但是新建项目之后,提示的错误是 就是认定在F盘了,可是那个时候我只是在F盘启动了第一次,之后就拷贝到了D盘的, 是不是第一次启动就会注册了什么的呢? 解决方案: 更改你的e ...
- HAL中通过JNI调用java方法【转】
转载请注明本文出处:http://www.cnblogs.com/xl19862005 作者:Xandy 由于工作的需要,最近一直在研究HAL.JNI.Java方法之间互调的问题,并做了如下一些记录和 ...
- python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作
转自:http://blog.chinaunix.net/uid-200142-id-3992553.html 有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总 ...
- 将txt文件数据转成bin文件.
之前用牛逼的绘图以及分析bmp的像素文件的方法, 整理出汉字编码从: 0x4E00到0x9FA5, (维基上说是9FD5, 完了, 回头再更新吧.) https://en.wikipedia.org/ ...
- Delphi中CoInitialize之探究
CoInitialize(LPVOID),它将以特定参数调用CoInitializeEx,为当前单元初始化COM库,并标记协同模式为单线程模式.参数必须为NULL.这是关于OLE和COM的问题. Co ...
- oracle初次使用连接不上
问题描述: win10下,cmd运行 输入sqlplus报一下错误 SP2-1503: 无法初始化 Oracle 调用界面 SP2-0152: ORACLE 不能正常工作 解决办法 cmd右键--以管 ...