现在的位置: 首页 > 综合 > 正文

POJ训练计划1753_Flip Game(枚举+BFS)

2015年01月16日 ⁄ 综合 ⁄ 共 2960字 ⁄ 字号 评论关闭

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
解体报告
POJ计划第一题。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#define inf 65536
using namespace std;
int num;
struct node
{
    int n,step;
};
int maxx=inf;
int vis[65555];
int bfs()
{
    int i;
    queue<node >Q;
    memset(vis,0,sizeof(vis));
    node now,next;
    now.n=num;
    now.step=0;
    vis[num]=1;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.front();
        Q.pop();
        next.n=now.n;
        for(i=15; i>=0; i--)
        {
            next.n=now.n;
            if(i==15)
            {
                next.n^=(int)pow(2,15);
                next.n^=(int)pow(2,14);
                next.n^=(int)pow(2,11);
            }
            else if(i==12)
            {
                next.n^=(int)pow(2,12);
                next.n^=(int)pow(2,13);
                next.n^=(int)pow(2,8);
            }
            else if(i==0)
            {
                next.n^=(int)pow(2,0);
                next.n^=(int)pow(2,1);
                next.n^=(int)pow(2,4);
            }
            else if(i==3)
            {
                next.n^=(int)pow(2,3);
                next.n^=(int)pow(2,2);
                next.n^=(int)pow(2,7);
            }
            else if(i==1||i==2)
            {
                next.n^=(int)pow(2,i+4);
                next.n^=(int)pow(2,i);
                next.n^=(int)pow(2,i-1);
                next.n^=(int)pow(2,i+1);
            }
            else if(i==13||i==14)
            {
                next.n^=(int)pow(2,i-4);
                next.n^=(int)pow(2,i);
                next.n^=(int)pow(2,i-1);
                next.n^=(int)pow(2,i+1);
            }
            else if(i==4||i==8)
            {
                next.n^=(int)pow(2,i+1);
                next.n^=(int)pow(2,i);
                next.n^=(int)pow(2,i-4);
                next.n^=(int)pow(2,i+4);
            }
            else if(i==7||i==11)
            {
                next.n^=(int)pow(2,i-1);
                next.n^=(int)pow(2,i);
                next.n^=(int)pow(2,i-4);
                next.n^=(int)pow(2,i+4);
            }
            else
            {
                next.n^=(int)pow(2,i+1);
                next.n^=(int)pow(2,i);
                next.n^=(int)pow(2,i-1);
                next.n^=(int)pow(2,i-4);
                next.n^=(int)pow(2,i+4);
            }
            if(next.n==0||next.n==65535)
            {
                if(now.step+1<maxx)
                    maxx=now.step+1;
            }
            if(!vis[next.n])
            {
                next.step=now.step+1;
                vis[next.n]=1;
                Q.push(next);
            }
        }
    }
    if(maxx==inf)
        cout<<"Impossible"<<endl;
    else cout<<maxx;
}
int main()
{
    int i,j,k=15;
    char ch[4];
    for(i=0; i<4; i++)
    {
        cin>>ch;
        for(j=0; j<4; j++)
        {
            if(ch[j]=='w')
                num+=(int)pow(2,k--);
            else k--;
        }
    }
    if(num==0||num==65535)
    {
        cout<<"0"<<endl;
        return 0;
    }
    bfs();
    return 0;
}


抱歉!评论已关闭.