http://poj.org/problem?id=1753

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39180   Accepted: 17033

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 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:
  1. Choose any one of the 16 pieces.
  2. 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 4 lines with 4 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 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】的更多相关文章

  1. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  2. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. 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 ...

  4. hdu 3681 Prison Break(状态压缩+bfs)

    Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...

  5. 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】

    Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...

  6. 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 ...

  7. POJ - 1324 Holedox Moving (状态压缩+BFS/A*)

    题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...

  8. POJ 3411 Paid Roads (状态压缩+BFS)

    题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...

  9. 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)

    首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...

  10. [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)

    快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...

随机推荐

  1. Redis 存储机制

    Redis存储机制分成两种Snapshot和AOF.无论是那种机制,Redis都是将数据存储在内存中. Snapshot工作原理: 是将数据先存储在内存,然后当数据累计达到某些设定的伐值的时候,就会触 ...

  2. s3cmd 安装使用指南

    https://wangyan.org/blog/s3cmd-how-to-use.html s3cmd 安装使用指南 s3cmd 是一款 Amazon S3 命令行工具.它不仅能上传.下载.同步,还 ...

  3. Tomcat部署项目后有括号的处理方法

    常见的问题,收录整理了一下,方便查找. 如下3个地方都修改为一致即可解决. 1,右键项目名 --> properties --> 输入web project settings --> ...

  4. 【Oracle】实现Oracle数据库对象的一键升级

    引言     公司内部的项目比较倾向于将业务逻辑放在oracle存储过程中实现,所以每次项目升级都涉及到很多的oracle表,存储过程等数据库对象的升级.然而采取的升级方式是比较"原始&qu ...

  5. Python网络编程2018-01-26更新

    前言:使用python3.x写的socket编程,本人wechat:YWNlODAyMzU5MTEzMTQ=. 如果内容有错,请指出来. ssh服务端 # 1.接收一个连接实例 # 2.接收数据 # ...

  6. spring MVC HandlerInterceptorAdapter

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  7. C语言中time函数获取系统时间

    可以通过time()函数来获得计算机系统当前的日历时间(Calendar Time),处理日期时间的函数都是以本函数的返回值为基础进行运算.其原型为: time_t time(time_t * t); ...

  8. python与mongodb

    一.mongodb的原理介绍: 特点: 为了理解以上特点,我们从一个真实的场景出发,介绍mongodb的原理:参考视频:https://www.youtube.com/watch?v=4SxHNmk5 ...

  9. SQL夯实基础(一):inner join、outer join和cross join的区别

    一.数据构建 先建表,再说话 create database Test use Test create table A ( AID ,) primary key, name ), age int ) ...

  10. [独孤九剑]Oracle知识点梳理(二)数据库的连接

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...