pku暑期訓練3filp game

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:
Choose any one of the 16 pieces.
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.
輸入
The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.
輸出
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).
樣例輸入
bwwb
bbwb
bwwb
bwww
樣例輸出
4

http://bailian.openjudge.cn/practice/1753/
枚舉所有狀態,直至找到目標狀態,而且由於只需要輸出該種狀態所在樹的深度

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>

using namespace std;

typedef long long LL;
const int INF = 1E9;
const int maxn = 5 + 5;
const int dx[] = {1,0,-1,0,0};
const int dy[] = {0,-1,0,1,0};

int ditu[maxn][maxn];
int step;
bool flag;
struct node
{
    void init()
    {
        memset(ditu,0,sizeof(ditu));
        for(int i =0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                char temp;
                cin>>temp;
                if(temp=='b')
                {
                    ditu[i][j] = 1;
                }
            }
        }
    }
    bool panduan()
    {
        for(int i =0;i<4;i++)
        {
            for(int j =0;j<4;j++)
            {
                if(ditu[i][j]!=ditu[0][0])
                {
                    return false;
                }
            }
        }
        return true;
    }
    void fz(int row,int col)
    {
        for(int i =0;i<5;i++)
        {
            int xx = dx[i] + row;
            int yy = dy[i] + col;
            if(xx>=0&&xx<4&&yy>=0&&yy<4)
            {
                ditu[xx][yy] = !ditu[xx][yy];
            }
        }
    }
    void dfs(int row, int col ,int deep)
    {
        if(deep == step)
        {
            flag = panduan();
            return ;
        }
        if(row==4||flag) return;
        fz(row,col);
        if(col<3)
        {
            dfs(row,col+1,deep+1);
        }
        else
        {
            dfs(row+1,0,deep+1);
        }
        fz(row,col);
        if(col<3)
        {
            dfs(row,col+1,deep);
        }
        else
        {
            dfs(row+1,0,deep);
        }
    }
    void solve()
    {
        init();
        for(step = 0;step<=16;step++)
        {
            dfs(0,0,0);
            if(flag) break;
        }
        if(flag) cout<<step<<endl;
        else cout<<"Impossible"<<endl;
    }
};
node jiejue;
int main()
{
    jiejue.solve();
    return 0 ;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章