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. c、rust、golang、swift性能比较

    mac 计算速度视觉判断是(由好到差):c > rust > swift > golang 内存开销在mac上是(由好到差):c > rust > golang > ...

  2. nyoj-451-光棍节的快乐(错排公式)

     题目链接 /* Name:nyoj-451-光棍节的快乐 Copyright: Author: Date: 2018/4/25 16:44:47 Description:D(n)=(n-1)*(D( ...

  3. UVALive 3708 Graveyard(思维题)

    将原有的每个雕塑的坐标位置,映射在一个总长为n+m的数轴上,设第一个点的坐标为0,(新的等分点必然有至少有一个和原来n等分的等分点重合,因为等分点可以等距的绕圆周旋转,总可以转到有至少一个重合的,不妨 ...

  4. Linux 获取随机密码

    /****************************************************************************** * Linux 获取随机密码 * 说明: ...

  5. Java实现LSH(Locality Sensitive Hash )

    在对大批量数据进行图像处理的时候,比如说我提取SIFT特征,数据集为10W张图片,一个SIFT特征点是128维,一张图片提取出500个特征点,这样我们在处理的时候就是对5000万个128维的数据进行处 ...

  6. BZOJ3832 Rally

    传送门(权限) 题目大意 给定一个有向无环图,可以删去一个点和所有与它相连的边,使得图的其余部分最长路径最小,求这个位置和最小的最长路径长度. 题解 对于每一条边$u\rightarrow v$,设$ ...

  7. shell 实现闰年的判断

    #!/bin/shecho "please input the year"read year let "n1=$year % 4"let "n2=$y ...

  8. JavaScript6 新语法 let 有什么优势

    最近看国外的前端代码时,发现ES6的新特性已经相当普及,尤其是 let,应用非常普遍 虽然 let 的用法与 var 相同,但不管是语法语义上,还是性能上,都提升了很多,下面就从这两方面对比一下 语法 ...

  9. 详解Top命令 输出命令行选项及代码

    Linux中的top命令显示系统上正在运行的进程.它是系统管理员最重要的工具之一.被广泛用于监视服务器的负载.在本篇中,我们会探索top命令的细节.top命令是一个交互命令.在运行top的时候还可以运 ...

  10. git之reset

    有时候,一直用公司电脑开发的时候,顺顺利利.回到家,用自己笔记本,想改改代码,git pull后,发现一堆的unmerged或者其他冲突等问题. 明明只是git pull 一下,怎么会这么多问题. 这 ...