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 ...
随机推荐
- 修改pytorch官方实例适用于自己的二分类迁移学习项目
本demo从pytorch官方的迁移学习示例修改而来,增加了以下功能: 根据AUC来迭代最优参数: 五折交叉验证: 输出验证集错误分类图片: 输出分类报告并保存AUC结果图片. import os i ...
- 初学Splunk
splunk简介 https://www.splunk.com/zh-hans_cn/download.html splunk 简体中文版手册 http://docs.splunk.com/Docum ...
- Ajax之eval()函数
Ajax之eval()函数 <!DOCTYPE html> <html> <head lang="en"> <meta charset=& ...
- isolate-user-vlan隔离用户vlan的配置
lab1 根据项目需求搭建好拓扑图: 首先,配置sw2,在E0/4/0接口上创建vlan20,并将该vlan接口配置成带有ip地址的类以太接口 其次,在E0/4/1接口上加入vlan2,同理,E0/4 ...
- node.js常用的fs文件系统
fs文件系统模块对于系统文件及目录进行一些读写操作. 模块中的方法均有异步和同步版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync(). 异步的 ...
- yii 自带RBAC
common:中加 'authManager' => [ 'class' => 'yii\rbac\DbManager', 'itemTable' => 'auth_item', ' ...
- thinkphp-PHP实现pdf导出功能
Thinkphp框架引用tcpdf插件,插件下载地址:待续... 代码编写前先引入tcpdf整个文件夹到项目目录的ThinkPHP文件夹下 如:/ThinkPHP/Library/Vendor/tcp ...
- 我的机器学习之路--anaconda环境搭载
网上许多教程比较晦涩难懂,本教程按照笔者(新手)自己的视角记录,希望给大家一些帮助 1.安装anaconda 目前比较推荐的机器学习环境为anaconda. Anaconda指的是一个开源的Pytho ...
- 分享一个根据具体的日期判断星座的PHP函数
其实原理很简单,也就是把所有的星座月份日期范围存储到一个数组中,然后根据日期判断属于哪个范围,这样就得到是哪个星座了. 下面的这个函数写的比较精炼,可以参考一下 function constellat ...
- linux文件操作篇 (一)文件属性与权限
文件的属性和权限是linux中 目录 和 文件 的两个基本特性. #属性: . 所有者属性 . 访问权限属性 -rwxrwxr-x #第一个字符是文件类别 -表示 普通文件 d 表示目录 b 表示 ...