poj--1753--Flip Game(dfs好题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 37201 | Accepted: 16201 |
Description
round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
- 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
goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
一个4*4的矩阵,每个格子要么是黑色,要么是白的,目标是将全部的格子颜色统一,每次可以翻转一个格子,黑变白,白变黑,翻转的同时该格子的周围上下左右也会变颜色(有的话),问最少的步数,由题意易知每个格子最多翻转1次,最多的步数也就是16,状态就是有2的16次方种,枚举每一个可能的步数,每个格子可以翻转或者不翻转,
在达到枚举的步数时,判断一下颜色是否相同,总的思路就是一行一行枚举,一个一个尝试
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
bool map[6][6],flag;
int step;
int dx[5]={0,0,0,1,-1};
int dy[5]={1,-1,0,0,0};
bool is()
{
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
if(map[i][j]!=map[1][1])
return false;
}
return true;
}
void flip(int r,int c)
{
for(int i=0;i<5;i++)
{
int x=r+dx[i];
int y=c+dy[i];
map[x][y]=!map[x][y];
}
}
void dfs(int r,int c,int s)
{
if(s==step)
{
flag=is();
return ;
}
if(flag||r==5) return ;//一行一行进行枚举
flip(r,c);//如果r,c进行翻转,步数加一
if(c<4)
dfs(r,c+1,s+1);
else
dfs(r+1,1,s+1);
flip(r,c);//如果r,c处不翻转,就枚举下一个,flip要再用一次
if(c<4)
dfs(r,c+1,s);
else
dfs(r+1,1,s);
}
int main()
{
char c;
memset(map,false,sizeof(map));
flag=false;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
{
cin>>c;
if(c=='b')
map[i][j]=true;
}
for(step=0;step<=16;step++)
{
dfs(1,1,0);
if(flag) break;
}
if(flag) cout<<step<<endl;
else cout<<"Impossible"<<endl;
return 0;
}
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 (dfs)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28805 Accepted: 12461 Descr ...
- 枚举 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 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- 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 ...
随机推荐
- x264
x264 x264 h264 1. x264调用主要过程 x264_param_default():设置参数集结构体x264_param_t的缺省值. x264_picture_alloc():为 ...
- JS高级——浏览器的线程
基本概念 1.js的执行过程是单线程的模式,也就是同步进行,只有前面的代码执行完了才会往下面执行 2.但是执行js代码也只是浏览器的线程之一所负责的事情,这个线程被称为js引擎,浏览器还具有其他线程: ...
- html——快捷键
webstorm ctrl+c 复制 ctrl+v 粘贴 ctrl+x 剪切一行或删除一行 ctrl+d 复制本行到下一行 ctrl+z 退回之前操作 ctrl+shift+z 已经做好的退回之后的下 ...
- JS——行内式注册事件
html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...
- CSS平滑过渡动画:transition
<html> <head> <link href="http://cdn.bootcss.com/twitter-bootstrap/3.0.2/css/boo ...
- https Full Handshake
1)加密套件交互: 2)密码交换: 3)身份认证: Full Handshake Initially, client and server "agree upon" null en ...
- EF入门
1.(安装EF)右键项目
- loader__demo_css
环境 node + yarn + webpack4.0 + webpack-cli + style-loader css-loader 文件结构 │ package.json │ webpack.co ...
- Centos7下mysql的主从配置
最近,有朋友业务并发量比较大,让我帮他配置个主从,来缓解数据库的压力.下面就是我配置的,有需要的朋友可以借鉴下. 首先,我得到2台服务器: 172.18.2.142(主) 172.18.2.141(从 ...
- Lua之尾调函数的用法
Lua之尾调函数的用法 --当函数的最后返回结果调用另一个函数,称之为尾调函数 function f(x) return g(x) end --由于“尾调用”不会耗费栈空间,所以一个程序可以拥有无数嵌 ...