POJ1753Flip Game(DFS + 枚举)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37050 | Accepted: 16122 |
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 没点思路-_-
转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299076400
提示:翻转棋,可以建模为多叉树
本题难点有两个,一个就是不要以全黑(或全白)作为目标进行搜索,而是要把全黑(或全白)作为“根”,去搜索树叶,看看是否有 输入的棋盘状态。
另一个难点需要一点数学功底,就是要知道 树 的最大高度,这是“状态不存在”的判断标准
提示:其实每格棋子最多只可以翻转一次(实际是奇数次,但这没意义),只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,由此就可以确定这个棋盘最多只能走16步,最多只能有翻出2^16种状态
其实也想过dfs,找不到终止状态,对,就是16就ok了,因为如果每一个状态都翻转了一遍,那么你在翻转一个牌,那么就和他之前没翻转是一样的,所以没必要了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool chess[][];
int step,flag;
int r[] = {-,,,};
int c[] = {,,-,};
int judge_all()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
if(chess[i][j] != chess[][])
return false;
}
}
return true;
}
void flip(int row, int col)
{
chess[row][col] = !chess[row][col];
for(int i = ; i < ; i++)
{
int fx = row + r[i];
int fy = col + c[i];
if(fx > && fx <= && fy > && fy <= )
chess[fx][fy] = !chess[fx][fy];
}
}
void dfs(int row, int col, int deep)
{
if(deep == step) // 到达了翻转的指定次数,就判断是否都一样,然后返回
{
flag = judge_all();
return;
}
if(flag || row > )
return;
flip(row, col);
if(col < )
{
dfs(row, col + , deep + ); //如果这一行还可以翻转,就判断下一个
}
else
{
dfs(row + , , deep + );
} flip(row, col); //回溯,把原来的转过来 if(col < )
{
dfs(row, col + , deep); //回溯时要判断上一步是否已经结束,因此传的是deep,判断当前状态,如果没结束,就按row,col+1变化
}
else
{
dfs(row + , , deep);
}
return;
}
int main()
{
char s[];
while(scanf("%s", s) != EOF)
{
memset(chess, false, sizeof(chess));
for(int i = ; i < ; i++)
{
if(s[i] == 'b')
{
chess[][i + ] = true;
}
}
for(int i = ; i <= ; i++)
{
scanf("%s", s);
for(int j = ; j < ; j++)
{
if(s[j] == 'b')
chess[i][j + ] = true;
}
}
flag = ;
for(step = ; step <= ; step++)
{
dfs(,,);
if(flag)
break;
}
if(flag)
printf("%d\n", step);
else
printf("Impossible\n");
} return ;
}
POJ1753Flip Game(DFS + 枚举)的更多相关文章
- POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- 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 ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- The Pilots Brothers' refrigerator DFS+枚举
Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...
随机推荐
- 头像上传功能实现,PC端的需要做兼容
暂时实现的效果: http://sandbox.runjs.cn/show/v2vkds3j <form action=""> <img id="vie ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 【Windows phone 8】欢迎引导页面02
[目标]前一篇文章已经实现了图片的切换,这里需要限制pivot的循环滚动. [思路]通过手势事件,对第一张,最后一张图片处加以限制 [前台] 在pivot处加上 <toolkit:Gesture ...
- Socket Programming in C#--Conclusion
Conclusion And that's all there is to it! Here is how our client looks like Here is how our server l ...
- 思科产品选型pdf
以前做工程时候想起了设备选型时候用过的一份文档. 有个小伙伴今天问起思科设备选型,恰好google到了这份文档 https://www.cisco.com/web/CN/products/pdf/04 ...
- Sublime 将 Tab 转为空格
最近在使用 vue-cli 搭建项目,但每次用 Hbuilder 编写 vue 文件的时候,如果存在<script>部分就会报错,错误信息大意是说空格有问题.仔细研究了之后才知道,这是因为 ...
- PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
擦,一身盗汗.一向的设计信息都在设计图里!竟然坏了,坏了!!!!! 惊.怒.悲 固然可以经由过程数据库当前状况反向工程.然则那么注解.我写的提示这些器材都邑消散. 比来的备份是10天前,恢复也会有必然 ...
- 安卓界面篇(一) 自定义一个topbar
步骤一: 先在values 里 新建一个attrs.xml 来设置我们的属性值: <?xml version="1.0" encoding="utf-8" ...
- c语言自加自减三道题
int x , y,z; x = 0; y = z = -1; x += -z ---y; printf("x=%d\n",x) x = 2 为什么? x + = -z - - ...
- [CareerCup] 8.9 An In-memory File System 内存文件系统
8.9 Explain the data structures and algorithms that you would use to design an in-memory file system ...