poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each 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 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
The input consists of lines with characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write . If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
Source
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12 int vis[(<<)];
struct Node{
int val;
int t; }st;
int mp[]={
,,,,,,,,,,,,,,,
};
void bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
vis[st.val]=;
while(!q.empty()){
t1=q.front();
q.pop();
if(t1.val== || t1.val==((<<)-)){
printf("%d\n",t1.t);
return ;
}
for(int i=;i<;i++){
t2=t1;
t2.val^=mp[i];
if(vis[t2.val]) continue;
vis[t2.val]=;
t2.t++;
q.push(t2);
}
}
printf("Impossible\n");
}
int main()
{
char s[];
int ans=;
int num=;
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b'){
ans+=(<<num);
}
num--;
}
}
st.val=ans;
st.t=;
memset(vis,,sizeof(vis));
bfs(); return ;
}
思路:仔细考虑,可以发现,一个位置的棋子,要么保持原状不变,要么改变一次。因为改变偶数次和不改变的效果是一样的,改变奇数次和改变1次的效果是一样的。也就是说,达到最后颜色都一样的状态,某个位置的棋子,要么改变一次,要么一次也不改变。所以求最少经过多少步的改变,可以转化为最少需要改变多少个棋子。因为数据范围小,总共16个棋子,所以我们可以枚举。
我们可以判断改变0个,改变1个,改变2个,,,改变16个。改变x个,就是从16个元素中选x个的过程,可以用dfs实现。如果一直到最后改变16个都不行,则输出“Impossible”即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int mp[][];
int newMp[][];
int newRecord[N];
int dirx[]={,,-,};
int diry[]={-,,,};
int flag;
int vis[N];
void judge(int cnt){
for(int i=;i<;i++){
for(int j=;j<;j++){
newMp[i][j]=mp[i][j];
}
}
for(int i=;i<cnt;i++){
int x=newRecord[i]/;
int y=newRecord[i]-x*;
newMp[x][y]=!newMp[x][y];
for(int j=;j<;j++){
int newX=x+dirx[j];
int newY=y+diry[j];
if(newX< || newX>= || newY< || newY>=) continue;
newMp[newX][newY]=!newMp[newX][newY];
}
}
int ans=;
for(int i=;i<;i++){
for(int j=;j<;j++){
if(newMp[i][j]){
ans++;
}
}
}
if(ans== || ans==){
flag=;
}
}
void dfs(int st,int num,int cnt){
if(num==cnt){
judge(cnt);
return;
} for(int i=st;i<;i++){
if(!vis[i]){
vis[i]=;
newRecord[num]=i;
dfs(i,num+,cnt);
if(flag)
return;
vis[i]=;
}
} }
int main()
{
char s[];
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b'){
mp[i][j]=;
}
else{
mp[i][j]=;
}
}
}
flag=;
for(int i=;i<=;i++){//改变多少个可以达到目的
memset(vis,,sizeof(vis)); dfs(,,i);
if(flag){
printf("%d\n",i);
break;
}
}
if(flag==){
printf("Impossible\n");
} return ;
}
poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)的更多相关文章
- POJ 1753 Flip Game (状态压缩 bfs+位运算)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
随机推荐
- awk笔记1
grep: 文本过滤器 grep 'pattern' input_file ... sed:流编辑器 awk: 报告生成器 格式化以后,显示 AWK a.k.a. Aho, Kernigh ...
- 【转】Alsa音频编程【精华】
一.前序 这里了解一下各个参数的含义以及一些基本概念. 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. 样本长度(sample):样本是记录音频数据最基本的单位,计算机对每 ...
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- hdu 5344 MZL's xor(数学之异或)
Problem Description MZL loves xor very much.Now he gets an array A.The length of A ≤i,j≤n) The xor ...
- pyqt信号事件相关网址说明及python相关
pyqt在线文档: http://www.rzcucc.com/search/pyqt.sourceforge.net/Docs/PyQt4/-qdatetime-2.html PyQT信号槽_学习笔 ...
- IOS 创建一张有颜色的UIImage
#import <UIKit/UIKit.h> @interface UIImage (ImageWithColor) + (UIImage *)imageWithColor:(UICol ...
- mysql常用操作命令
本章内容:(引用原文:http://www.cnblogs.com/suoning/p/5769141.html) 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网远程连接法 查看\创建\使 ...
- linux hash_map
在linux下的hash_map hash_map本身以前本身不属于标准库,是后来引入的.有两种可能:一种可能它被放在了stdext名空间里,那么你就要使用using namespace stdext ...
- [Hapi.js] Serving static files
hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...
- jstl数字保留两位小数
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><fmt:fo ...