码迷,mamicode.com
首页 > 其他好文 > 详细

Flip Game (poj 1753)枚举+二进制

时间:2014-09-14 12:55:37      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:poj 1753 枚举

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31031   Accepted: 13500

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

bubuko.com,布布扣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

Source

题意:有一个4*4的方格,每个方格中放一粒棋子,这个棋子一面是白色,一面是黑色。游戏规则为每次任选16颗中的一颗,把选中的这颗以及它四周的棋子一并反过来,当所有的棋子都是同一个颜色朝上时,游戏就完成了。现在给定一个初始状态,要求输出能够完成游戏所需翻转的最小次数,如果初始状态已经达到要求输出0。如果不可能完成游戏,输出Impossible。
思路:因为方格只有4*4,直接枚举所有状态,用队列实现:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

struct node
{
    int x;
    int step;
};

queue<node>Q;
int visit[70000];  //标记状态,是否出现过

int main()
{
    int id;
    char ch;
    id=0;
    memset(visit,0,sizeof(visit));
    for (int i=0;i<4;i++)
    {
        for (int j=0;j<4;j++)
        {
            scanf("%c",&ch);
            if (ch=='b')
                id=id^(1<<(4*i+j));
        }
        getchar();
    }
    if (id==0||id==65535)
    {
        printf("0\n");
        return 0;
    }
    while (!Q.empty())
        Q.pop();
    node st,now;
    st.x=id;
    visit[id]=1;
    st.step=0;
    Q.push(st);
    while (!Q.empty())
    {
        st=Q.front();
        Q.pop();
        if (st.x==0||st.x==65535)
        {
            printf("%d\n",st.step);
            return 0;
        }
        for (int i=0;i<4;i++)
            for (int j=0;j<4;j++)
        {
            now.x=st.x^(1<<(4*i+j));
            if (i>0)
                now.x=now.x^(1<<(4*(i-1)+j));//上
            if (i<3)
                now.x=now.x^(1<<(4*(i+1)+j));//下
            if (j>0)
                now.x=now.x^(1<<(4*i+j-1));//左
            if (j<3)
                now.x=now.x^(1<<(4*i+j+1));//右
            if (!visit[now.x])
            {
                now.step=st.step+1;
                Q.push(now);
                visit[now.x]=1;
            }
        }
    }
    printf("Impossible\n");
    return 0;
}
/*
bwwb
bbwb
bwwb
bwww
*/


Flip Game (poj 1753)枚举+二进制

标签:poj 1753 枚举

原文地址:http://blog.csdn.net/u014422052/article/details/39268123

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!