poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推
题目链接:http://poj.org/problem?id=1753
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46724 | Accepted: 20002 |
Description
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
(without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
方法一:
1.对于每一个格子,它都有翻或不翻两种选择,所以枚举从第一个格子开始一直到最后一格,所以总共有2^16种情况(像二叉树)。然而这种方式必须要搜索到最后一个才能作出判断。
2.对于整盘棋,.每次枚举翻一个,第一次有16种选择,第二次有15种选择,所以:16*15*14……。当搜索到符合全白或全黑则直接返回,这种方法较快。
技巧:由于每个棋只有两种情况,则可以讲棋盘压缩成二进制以记录状态。(推导:当单个的情况数为n种时,可以将其状态用n进制记录)。
方法二:
1.枚举第一行的所有状态。
2.如果把所有棋翻为黑色:从第二行开始,如果当前格子的头上格子为白色,那么需要翻转当前格子。因为在这一行之内,只有当前格子能够使头上的格子变为黑色,然后一直递推到最后一行。如果把所有棋翻为白色,做法亦如此。
3.分析:方法二相对于方法一是有明显的优势的:方法二从第二行开始,每翻转一个棋都是有针对性的、有目的性的;而方法一则是枚举所有情况,盲目地翻转,如:假如要把所有棋翻为黑色,而当前格子为黑色,却还是硬要翻转该格子,那显然是行不通的。所以方法二相对来说更“智能”。
4.注意:s&(1<<i)的值要么等于0,要么等于(1<<i);而不是0或1。应注意!!
5.此题的加强版,必须使用方法二:POJ3279:http://www.cnblogs.com/DOLFAMINGO/p/7538582.html
DFS(方法一):
#include<cstdio>//dfs直接对棋盘进行操作
#include<cstring>
#define MIN(a,b) (a<b?a:b)
#define init_min 2000000000
int a[],min; int get()
{ //获取状态的二进制数表达
int sum = ;
for(int i = ; i<; i++)
sum += a[i]<<i;
return sum;
} void flip(int loc)
{
a[loc] = !a[loc];
if(loc->=) a[loc-] = !a[loc-];
if(loc+<) a[loc+] = !a[loc+];
if(loc%!=) a[loc-] = !a[loc-];
if(loc%!=) a[loc+] = !a[loc+];
} void dfs(int loc,int step)
{
if(loc==)
{ //当搜索到最后一个格子时,开始判断
int status = get();
if(status== || status==(<<)-)
min = MIN(min,step);
} else
{
dfs(loc+,step);
flip(loc);//翻转
dfs(loc+,step+);
flip(loc);//再翻转,使其状态复原
}
} int main()
{
char s[];
for(int i = ; i<; i++)
{
scanf("%s",s);
for(int j = ; j<; j++)
{
if(s[j]=='b') a[i*+j] = ;
else a[i*+j] = ;
}
}
min = init_min;
dfs(,);
if(min!=init_min)
printf("%d\n",min);
else
puts("Impossible");
return ;
}
BFS(方法一):
#include<cstdio>//由于bfs需要记录状态,而直接用数组入队列会增加时间(stl的原因),所以用二进制数记录,并对其操作
#include<cstring>
#include<queue> using namespace std; int a[],ss,vis[];//ss为初始状态,vis用来标记访问过的状态
struct node
{ //status为棋牌状态,step为步数
int status, step;
}; void calcul(node *now, node *next,int i)
{
if(now->status&(<<i)) next->status -= (<<i);
else next->status += (<<i);
} void turn (node *now, node *next,int i)
{ //翻转时直接对二进制数进行操作,即01
calcul(now, next,i);
if(i->) calcul(now, next,i-);
if(i+<) calcul(now, next,i+);
if(i%!=) calcul(now, next,i-);
if(i%!=) calcul(now, next,i+);
} int bfs()
{
node now, next;
queue<node>q;
now.status = ss, now.step = ; q.push(now);
vis[now.status] = ;
memset(vis,,sizeof(vis));
while(!q.empty())
{
now = q.front(); q.pop();
if(now.status== || now.status==0xffff) return now.step;
for(int i = ; i<; i++)
{ /*之前在这里加了个是否重复翻转的判断,但时间增加了一倍
细想下面的判重已经包含了这个功能*/
next.status = now.status;
turn(&now,&next,i);//翻转
if(vis[next.status]) continue;//判重
next.step = now.step + ;
vis[next.status] = ;
if(next.status== || next.status==0xffff) return next.step;
q.push(next);
}
}
return -;
} int main()
{
char s[];
ss = ;
for(int i = ; i<; i++)
{
scanf("%s",s);
for(int j = ; j<; j++)
{
if(s[j]=='b') { a[i*+j] = ; ss += <<(i*+j);}
else a[i*+j] = ;
}
}
int ans = bfs();
if(ans!=-)
printf("%d\n",ans);
else
puts("Impossible");
return ;
}
方法二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; int M[MAXN][MAXN], subM[MAXN][MAXN];
int op[MAXN][MAXN];
int n, m; void press(int x, int y)
{
op[x][y] = ;
subM[x][y] = !subM[x][y];
if(x->=) subM[x-][y] = !subM[x-][y];
if(x+<) subM[x+][y] = !subM[x+][y];
if(y->=) subM[x][y-] = !subM[x][y-];
if(y+<) subM[x][y+] = !subM[x][y+];
} bool all()
{
int b1 = , b2 = ;
for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
if(subM[i][j]==) b1 = ;
if(subM[i][j]==) b2 = ;
}
return b1||b2;
} void solve(int s, int type, int &ans)
{
ms(op, );
memcpy(subM, M, sizeof(subM)); int cnt = ;
for(int i = ; i<; i++)
if( (s&(<<i))==(type<<i) ) press(, i), cnt++;
//注意:s&(1<<i)的值要么等于0,要么等于(1<<i);而不是0或1。应注意!! for(int i = ; i<; i++)
for(int j = ; j<; j++)
if(subM[i-][j]==type) press(i, j), cnt++; if(all())
ans = min(ans, cnt);
} int main()
{
char s[];
for(int i = ; i<; i++)
{
scanf("%s",s);
for(int j = ; j<; j++)
{
if(s[j]=='b') M[i][j] = ;
else M[i][j] = ;
}
} int ans = INF;
for(int s = ; s<(<<); s++)
{
solve(s, , ans); //尝试把棋全部翻成0
solve(s, , ans); //尝试把棋全部翻成1
}
if(ans!=INF)
printf("%d\n", ans);
else
puts("Impossible");
}
poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推的更多相关文章
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- EOJ Problem #3249 状态压缩+循环周期+反向递推
限量供应 Time limit per test: 4.0 seconds Time limit all tests: 4.0 seconds Memory limit: 256 megabytes ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- poj1753 Flip Game(BFS+位压缩)
题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
- POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成
昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
随机推荐
- [转]Windows10内置Linux子系统初体验
Windows10内置Linux子系统初体验 https://www.jianshu.com/p/bc38ed12da1d
- 【共享单车】—— React后台管理系统开发手记:主页面架构设计
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- iOS 应用内跳转到系统设置
在iOS5下面版本号使用下面方法:[IOS5.1+之后不能使用此方法.iOS8的跳转方法已找到见下方,iOS7的正在摸索,欢迎大家给出观点意见] 通过URL Scheme的方式打开内置的Setting ...
- 请求php文件的整个流程
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- Shell脚本之:for
与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...
- python判断字符串类型
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False.(重点,这是字母数字一起判断的!!) s.isalpha() 所有字符都是字母,为真返回 Tur ...
- Robot Framework使用Phantomjs进行无界面UI自动化测试
Robot Framework 是一款关键字驱动的验收自动化测试框架,现在在国内使用的越来越广泛了.一种通用的Web UI自动化测试解决方案是Robot Framework+Selenium2Libr ...
- 2个YUV视频拼接技术
http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...
- FTP匿名登录或弱口令漏洞及服务加固
漏洞描述 FTP 弱口令或匿名登录漏洞,一般指使用 FTP 的用户启用了匿名登录功能,或系统口令的长度太短.复杂度不够.仅包含数字.或仅包含字母等,容易被黑客攻击,发生恶意文件上传或更严重的入侵行为. ...
- Hive报错:Failed with exception Unable to rename
之前也安装过hive,操作过无数,也没发现什么错误,今天因为之前安装的hadoop不能用了,不知道为什么,老是提示node 0,所以重新安装了hadoop和hive.安装完测试hive创建表也没发现什 ...