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 ;
}

最新文章

  1. 高仿QQ顶部控件之IOS SegmentView
  2. apt-get update更新源时,出现“Hash Sum mismatch”问题
  3. bpl 包的编写和引用
  4. 富文本常用封装(NSAttributedString浅析)
  5. Java程序员的日常 —— 工作一天的收获
  6. 黑马程序员——Foundation之NSString和NSMutableString
  7. Java之DataInputStream和DataOutputStream-用流操作基本数据类型
  8. Win7 64bit 安装VisualSVN出现报错:Servic &#39;VisualSVN Server&#39; failed to start.解决办法
  9. CSS彻底研究(1)
  10. Android:内存优化的一些总结
  11. 两点补充——CSS3新属性以及弹性布局
  12. JAVA对XML文件的读写(有具体的代码和解析
  13. 51nod 1522 上下序列
  14. StackWalk64
  15. ADC触摸屏
  16. windows快速进入安装目录
  17. 关于https不支持http的解决方案
  18. netstat 常用参数总结
  19. 《java并发编程实战》笔记
  20. 131.leetcode-Palindrome Partitioning

热门文章

  1. 开发一个app需要多少钱
  2. vue项目接口地址的定义
  3. 2018.8.11E-mail
  4. 非root用户 如何将cscope安装到指定目录,vim74安装
  5. UVA - 1632 Alibaba (区间dp+常数优化)
  6. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&amp;组合数学---比较综合的一道题)
  7. BZOJ4605:崂山白花蛇草水
  8. Linux开放80、8080端口或者开放某个端口
  9. CEF源码编译
  10. SpringMVC的环境搭建