POJ1753 Flip Game(bfs、枚举)

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

Flip Game

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

题目里是4*4的格子,可以用二进制的0、1表示每个格子的黑白,第i个格子为黑即第i位的二进制数为1。这样16个格子需要16位二进制,即一共有2^16-1个状态。从初始状态开始枚举每一种状态改变16个格子的颜色所能导致的状态。出现16位全1或者全0就输出Impossible。代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define MAXX (1<<16)
bool flag[MAXX];
int step[MAXX];
queue<int> que;
void input()
{
    int state=0;
    char ch;
    int i;
    for(i=1;i<MAXX;i<<=1)
    {
        scanf("%c",&ch);
        if(ch==‘\n‘)
            scanf("%c",&ch);
        if(ch==‘b‘)
            state+=i;
    }
    que.push(state);
    memset(flag,false,sizeof(flag));
    memset(step,0,sizeof(step));
    flag[state]=true;
    //printf("%d ",state);
}
void change(int n,int state)
{
    int temp=state;
    if(n-4>=0)
        state^=(1<<(n-4));
    if(n+4<16)
        state^=(1<<(n+4));
    if(n!=0&&n!=4&&n!=8&&n!=12)
        state^=(1<<(n-1));
    if(n!=3&&n!=7&&n!=11&&n!=15)
        state^=(1<<(n+1));
    state^=(1<<n);
    if(!flag[state])
    {
        flag[state]=true;
        que.push(state);
        step[state]=step[temp]+1;
    }
}
void getall()
{
    int state;
    int i;
    while(!que.empty())
    {
        state=que.front();
        if(state==0||state==MAXX-1)
        {
            printf("%d\n",step[state]);
            return;
        }
        que.pop();
        for(i=0;i<16;i++)
        {
            change(i,state);
        }
    }
    printf("Impossible\n");
}
int main()
{
    input();
    getall();
    return 0;
}

时间: 2024-10-12 07:04:42

POJ1753 Flip Game(bfs、枚举)的相关文章

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 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 o

poj1753 Flip Game DFS,枚举

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34437   Accepted: 15058 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

POJ1753——Flip Game

Flip Game 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 r

poj1753解题报告(枚举、组合数)

POJ 1753,题目链接http://poj.org/problem?id=1753 题意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使4*4的正方形变为纯白或者纯黑? 思路: 1. 每一个位置只有两种颜色,翻偶数次等于没有翻,所以只有翻基数次对棋盘有影响,即只用考虑一个位置最多翻一次. 2. 一共有16个位置,所以最多翻16次.那么可能翻0次就成功.或者翻1

POJ 1753 Flip Game (DFS + 枚举)

题目:http://poj.org/problem?id=1753 这个题在开始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每个棋子翻一遍,然后顺利的过了,所以也就没有深究. 省赛前一次做PC2遇到了几乎一模一样的题,只不过是把棋盘的界限由4X4改为了5X5,然后一直跑不出结果来,但是当时崔老湿那个队过了,在最后总结的时候,崔老湿就说和这个题一样,不过要枚举第一行进行优化. 我以为就是恢复第一行然后第二行以此类推,不过手推一下结果是6不是4,就知道这个有问题. 问了崔老湿,问了+

poj1753 Flip Game(BFS+位压缩)

题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子意味着将该格子及其上下左右格子(如果存在的话)的黑面朝上变成白面朝上,反之亦然,游戏的目标是格子全部黑面朝上或者全部白面朝上.输入棋盘的初始状态,求最少经过多少轮可以达到游戏的目标. 思路 求最少轮数,我会想到使用bfs来解决(dfs也可以解决),但使用bfs求解,如果每个状态都直接存储下当前棋盘的

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

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 f

poj 1753 Flip Game (bfs + 枚举)

链接:poj 1753 题意:这是翻棋游戏,给定4*4棋盘,棋子一面为黑色(用b表示),另一面为白色(用w表示),问至少要几步可以将棋子翻为全黑或者全白,如不能达到目的,输出"Impossible " 翻转规则:可以选定16个棋子中的任意一个,将其本身以及上下左右相邻的翻转过来 分析:其实每格棋子最多只可以翻转一次(实际是奇数次,但与翻转一次状态一样),只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,与最初状态未翻转一样,由此就可以

[POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)

题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 *