poj1753-Flip Game 【状态压缩+bfs】
http://poj.org/problem?id=1753
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39180 | Accepted: 17033 |
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
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 0. If it's impossible to
achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
思路:
1.首先需要理解一点,每个位置最多翻一次,翻两次等于没有翻,而且,最终结果只与翻的位置有关,与翻的次序无关。故而,最多翻16次,若翻了16次还没有达到目的,那就无解。
2.求最少步数,则bfs,效率最高。
3.采取位运算压缩状态,把16个位置的状态用bit存储,即16位,每次翻转后的状态存入que队列。
4.优化:16步长检查;相同状态值检查。
程序:
#include <iostream>
#include <fstream>
using namespace std; #define n 16
#define N (1 << 16) struct Node
{
int count;
bool b;
}node[N];
int value;
int que[N + ]; void Init ()
{
char c;
while (~scanf("%c", &c))
{
if (c == 'b')
{
value <<= ;
value |= ;
}
else if (c == 'w')
{
value <<= ;
}
}
}
inline bool Check (int i, int j)
{
return i >= && i < && j >= && j < ;
}
void Bfs ()
{
int front = , rear = , ans = n;
if (value == || value == 0x0ffff)
{
puts("");
return;
}
memset(node, , (sizeof(node) << ));
que[] = value;
node[value].b = true;
while (front != rear)
{
int tmp = que[front++];
int ttmp;
if (node[tmp].count == n)
{
break;
}
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
ttmp = tmp;
int tttmp = (i << ) + j;
ttmp ^= ( << tttmp);
if (Check(i - , j))
{
ttmp ^= ( << (tttmp - ));
}
if (Check(i + , j))
{
ttmp ^= ( << (tttmp + ));
}
if (Check(i, j - ))
{
ttmp ^= ( << (tttmp - ));
}
if (Check(i, j + ))
{
ttmp ^= ( << (tttmp + ));
}
if (!node[ttmp].b)
{
node[ttmp].b = true;
node[ttmp].count = node[tmp].count + ;
que[rear++] = ttmp;
}
if (ttmp == || ttmp == 0x0ffff)
{
printf("%d\n", node[ttmp].count);
return;
}
}
}
}
puts("Impossible");
} int main ()
{
//freopen("D:\\input.in","r",stdin);
Init();
Bfs();
return ;
}
poj1753-Flip Game 【状态压缩+bfs】的更多相关文章
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 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 ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...
- HDU 5025 Saving Tang Monk 【状态压缩BFS】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...
- POJ - 1324 Holedox Moving (状态压缩+BFS/A*)
题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)
首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...
- [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)
快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...
随机推荐
- Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
The NuGet packages for ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 are now live o ...
- propertychange 属性说明
propertychange(ie)和input事件 input是标准的浏览器事件,一般应用于input元素,当input的value发生变化就会发生,无论是键盘输入还是鼠标粘贴的改变都能及时监听到变 ...
- LightOJ 1038 概率dp
题意:给一个数n,每次除它的一个因子(等概率),问除到1的次数的期望是多少 题解:概率dp,对于一个数x,y是x的因子个数,因子是a1到ay,E(x)=(E(a1)+1)/y+...+(E(ay)+1 ...
- uva 10453 dp/LCS变形
https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...
- window上脚本到linux上不能用
window上的脚本一定要用dos2unix 文件名处理一下
- 剑指offer--21.链表中倒数第k个结点
定义两个指针,当一个指针指到第K个结点时,第二个指针开始向后移动 -------------- 时间限制:1秒 空间限制:32768K 热度指数:602826 本题知识点: 链表 题目描述 输入一个链 ...
- PhotoShop使用指南(2)——下雨动画效果
第一步: 第二步: 第三步: 第四步:
- Memcache mutex设计模式
Memcache mutex设计模式 转自:https://timyang.net/programming/memcache-mutex/ 场景 Mutex主要用于有大量并发访问并存在cache过期的 ...
- BZOJ- 3142:数列 (数学)
题意:给出N,K,M,P.求有多少长度为K的序列A,满足:(1)首项为正整数:(2)递增数列:(3)相邻两项的差小于等于m:(4)最后一个数小于等于N. 思路:根据差分来算数量. #include&l ...
- Oracle hash分区的秘密
转自:http://www.hellodb.net/2009/12/hash_partition.html 在面试时经常会问一个问题,请列举出hash在数据库内部的应用,hash的原理虽然简单,但是它 ...