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 ...
随机推荐
- ETM and PTM
ETM:embedded Trace Macrocell PTM:Program Flow Trace Macrocell ETM-A7 macrocell提供Cortex-A7 MPcore的ins ...
- c++ 容器(list学习总结)
list是一个线性双向链表结构,它的数据由若干个节点构成,每一个节点都包括一个信息块(即实际存储的数据).一个前驱指针和一个后驱指针.它无需分配指定的内存大小且可以任意伸缩,这是因为它存储在非连续的内 ...
- js表单提交一种方式
在一个html和php混编的文件中,用到js提交表单的方法: 定义表单id为form1,提交按钮type为submit, var data_info = document.getElementById ...
- sql语句中where和having的区别
WHERE语句在GROUPBY语句之前:SQL会在分组之前计算WHERE语句. HAVING语句在GROUPBY语句之后:SQL会在分组之后计算HAVING语句.
- 锋利的JQuery(五)
jQuery与Ajax: load: load(url) $("#resText").load("test.html") 加载所有元素 load(url ...
- selenium webdriver设置超时
webdriver类中有三个和时间相关的方法: 1.pageLoadTimeout 2.setScriptTimeout 3.implicitlyWait pageLoadTimeout from s ...
- gerrit docker运行失败 chown: /var/gerrit/review_site: Permission denied 【已解决】
Docker Volume 之权限管理(转) - jackluo - 博客园 http://www.cnblogs.com/jackluo/p/5783116.html 为什么在公司电脑没有问题,但在 ...
- Java的深度克隆和浅度克隆
说到克隆,其实是个比较简单的概念,跟现实生活正的克隆一样,复制一个一模一样的对象出来.clone()这个方法是从Object继承下来的,一个对象要实现克隆,需要实现一个叫做Cloneable的接口,这 ...
- 如何获取各大平台的播放地址(获得优酷的m3u8播放地址)为例
1.打开safari 2.在顶部(黑色小苹果)旁边,点击safari. 3.打开里面的 偏好设置. 4.在高级 中 找到 “在菜单栏中显示开发菜单” 并打勾. 5.关闭窗口后,可以发现safari 顶 ...
- Symfony电子商务
http://zhilihe.com/content/symfony%E7%94%B5%E5%AD%90%E5%95%86%E5%8A%A1%E9%A1%B9%E7%9B%AE%E6%80%BB%E7 ...