poj 1753 Flip Game (dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28805 | Accepted: 12461 |
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
//164K 125MS C++ 1211B 2014-04-26 11:02:12
/* 题意:
问最少翻几步可以使棋盘棋子一样,不可能就输出Impossible 搜索枚举:
枚举全部状态,每个位置的棋子有翻或不翻两种状态,枚举全部状态。
注意一个棋子翻两次则和没翻一样,所以一种有2^16种情况,用dfs枚举全部状态。 */
#include<stdio.h>
#include<string.h>
int g[][];
int flag;
int judge(int tg[][]) //判断
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(g[i][j]!=g[][]) return ;
return ;
}
void flip(int i,int j) //翻棋
{
g[i][j]^=;
g[i-][j]^=;
g[i+][j]^=;
g[i][j-]^=;
g[i][j+]^=;
}
void dfs(int x,int y,int cnt,int n)
{
if(cnt==n){
flag=judge(g);
return;
}
if(flag || y>) return;
flip(x,y);
if(x<) dfs(x+,y,cnt+,n);
else dfs(,y+,cnt+,n);
flip(x,y);
if(x<) dfs(x+,y,cnt,n);
else dfs(,y+,cnt,n);
}
int main(void)
{
char c[];
while(scanf("%s",c)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<;i++) g[][i+]=c[i]=='b'?:;
for(int i=;i<;i++){
scanf("%s",c);
for(int j=;j<;j++)
g[i+][j+]=c[j]=='b'?:;
}
flag=;
int cnt=-;
for(int i=;i<;i++){
dfs(,,,i);
if(flag){
cnt=i;break;
}
}
if(cnt==-) puts("Impossible");
else printf("%d\n",cnt);
}
return ;
}
/* bwwb
bbwb
bwwb
bwww bwbw
bwww
wwwb
wwwb bwww
wwww
wwww
wwww */
poj 1753 Flip Game (dfs)的更多相关文章
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- OpenJudge/Poj 1753 Flip Game
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
随机推荐
- ajax在同一页面中同控制器不同方法中调用数据并异步刷新的实例
我在实习以来都有做一些笔记,之前做的笔记都在简书里,现在我提前把公司给我的任务做好了,坐在电脑前又不好玩别的,那么我就整理下我之前的笔记吧!(此项目是thinkphp5开发的) 先上效果图 这是整体页 ...
- PHP队列之理论篇
定义: 特殊的线性表. 特点: 1.先进先出:连结性. 2.作为一种特殊性的表,主要是在表前端进行删除操作,我们称删除的端为对头(front):只能在表的后端进行插入操作,我们称之为称插入 ...
- php成绩排序
$arr = ['12','12','23']; $arr = $arr; $arr1=$arr; rsort($arr1); $c=[]; foreach ( $arr as $k=>$v){ ...
- 20180803UnionPay银联支付
LNMP环境下开发的银联支付(测试环境) 1.准备条件 a.银联支持API:https://open.unionpay.com/ajweb/help/api b.选择开发软件包 图示: c.我选择: ...
- mysql 5.7 配置初始化及修改 ROOT 用户密码
1.修改配置文件 my.ini 放在 mysql\bin [mysqld] basedir=C:\Mysql datadir=C:\Mysql\data port=3306 # server_id = ...
- ZooKeeper异常:Error connecting service It is probably not running
ZooKeeper安装后使用以下命令可以启动成功 bin/zkServer.sh start 但是使用下面命令查看启动状态,则报错误: bin/zkServer.sh status Error con ...
- EXKMP学习笔记QAQ
因为一本通少了一些算法,所以我就自行补充了一些东西上去. EXKMP也就是扩展KMP,是一种特别毒瘤的东西 EXKMP确实很难,我理解他的时间与AC机的时间差不多,而且还很难记,因此一学会就马上写博客 ...
- 006---hashlib模块
hashlib模块 HASH 一般翻译成散列,也可以叫哈希. 把任意长度的输入通过散列算法变换成固定的长度. 该转换是一种压缩映射 MD5 输入任意长度的信息,经过处理.输出为128位的信息(数字指纹 ...
- JAVA 泛型之类型擦除
★ 泛型是 JDK 1.5 版本引进的概念,之前是没有泛型的概念的,但泛型代码能够很好地和之前版本的代码很好地兼容. CollectionTest.java ---编译成CollectionTest. ...
- rhel6.4 根目录扩容
状况:根目录容量不足 解决:扩容根目录 ====================================================== 解决步骤: 1. 将新的磁盘加入服务器 2. 使用 ...