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 ...
随机推荐
- Python-面向对像及其他
其他相关 1.isinstance(obj,cls) 检查是否obj是类cls的对象 # 针对变量 n = 123 s = "123" print isinstan ...
- 对window的认识
首先要明确: 不管是全局的函数还是全局的变量,都是属于window的,例如: a = 12; //全局变量 alert(a) === alert(window.a) function show(){ ...
- windows系统命令服务安装卸载
安装: sc create PDW.CHM.WebAPI binPath= "%~dp0PDW.CHM.WebAPI.exe" start= autosc start PDW.CH ...
- C语言 百炼成钢1
//题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> ...
- PHP基础13:数组排序
<?php //1.对数组进行升序排序 - sort() $cars=array("VOLVO","BMW","BYD"); $fri ...
- C#获取枚举描述代码
public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...
- iOS开发工具(上班需要必备的软件)
1 源代码管理工具 SVN:SVN可以使用的客户端软件有Cornerstone,SmartSVN,svnX,乌龟SVN,莲花版svn等等 或者git(sourcetree) 2 有道词典 3 Foxm ...
- html 空格-有趣的试验
首先,先给大家看一组demo <input /> <input type="submit" /> 展示效果: 为什么会出现空格呢?input不是行内元素吗? ...
- WINDOWS8.1安装ORACLE客户端及配置
1.官方网站下载: instantclient-basic-win32-11.2.0.1.0.zip instantclient-sqlplus-win32-11.2.0.1.0.zip instan ...
- Git.Framework 框架随手记-- 分享一个"比较垃圾"的项目
本文主要分享一个Git.Framework 开发的一个项目的部分源码,此项目代码"比较垃圾",所以请各位码农,码畜,码神,码圣勿喷!发此文只为记录工作问题以及分享问题! 一. 项目 ...