poj1753-Flip Game 【状态压缩+bfs】

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 4×4 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).

《poj1753-Flip Game 【状态压缩+bfs】》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步长检查;相同状态值检查。

程序:

  1 #include <iostream>
  2 #include <fstream>
  3 using namespace std;
  4 
  5 #define n 16
  6 #define N (1 << 16)
  7 
  8 struct Node
  9 {
 10     int count;
 11     bool b;
 12 }node[N];
 13 int value;
 14 int que[N + 1];
 15 
 16 void Init ()
 17 {
 18     char c;
 19     while (~scanf("%c", &c))
 20     {
 21         if (c == 'b')
 22         {
 23             value <<= 1;
 24             value |= 1;
 25         }
 26         else if (c == 'w')
 27         {
 28             value <<= 1;
 29         }
 30     }
 31 }
 32 inline bool Check (int i, int j)
 33 {
 34     return i >= 0 && i < 4 && j >=0 && j < 4;
 35 }
 36 void Bfs ()
 37 {
 38     int front = 0, rear = 1, ans = n;
 39     if (value == 0 || value == 0x0ffff)
 40     {
 41         puts("0");
 42         return;
 43     }
 44     memset(node, 0, (sizeof(node) << 16));
 45     que[0] = value;
 46     node[value].b = true;
 47     while (front != rear)
 48     {
 49         int tmp = que[front++];
 50         int ttmp;
 51         if (node[tmp].count == n)
 52         {
 53             break;
 54         }
 55         for (int i = 0; i < 4; i++)
 56         {
 57             for (int j = 0; j < 4; j++)
 58             {
 59                 ttmp = tmp;
 60                 int tttmp = (i << 2) + j;
 61                 ttmp ^= (1 << tttmp);
 62                 if (Check(i - 1, j))
 63                 {
 64                     ttmp ^= (1 << (tttmp - 4));
 65                 }
 66                 if (Check(i + 1, j))
 67                 {
 68                     ttmp ^= (1 << (tttmp + 4));
 69                 }
 70                 if (Check(i, j - 1))
 71                 {
 72                     ttmp ^= (1 << (tttmp - 1));
 73                 }
 74                 if (Check(i, j + 1))
 75                 {
 76                     ttmp ^= (1 << (tttmp + 1));
 77                 }
 78                 if (!node[ttmp].b)
 79                 {
 80                     node[ttmp].b = true;
 81                     node[ttmp].count = node[tmp].count + 1;
 82                     que[rear++] = ttmp;
 83                 }
 84                 if (ttmp == 0 || ttmp == 0x0ffff)
 85                 {
 86                     printf("%d\n", node[ttmp].count);
 87                     return;
 88                 }
 89             }
 90         }
 91     }
 92     puts("Impossible");
 93 }
 94 
 95 int main ()
 96 {
 97     //freopen("D:\\input.in","r",stdin);
 98     Init();
 99     Bfs();
100     return 0;
101 }

 

    原文作者:BFS
    原文地址: https://www.cnblogs.com/jiu0821/p/5614501.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞