POJ 1753-Flip Game(列舉&&DFS)

kewlgrl發表於2016-04-26

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 38472   Accepted: 16721

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

Source

Northeastern Europe 2000

題目意思:
有一個4*4的棋盤上有黑b白w兩種顏色的棋子,翻轉一個即改變它的顏色的時候而且其上下左右顏色都會變化。
求最少需要翻動多少個棋子就可以使棋盤上所有的棋子顏色相同。

解題思路:
用1表示白色w,0表示黑色b來簡化;
有4*4=16個位置可以翻轉,遍歷搜尋一遍,求出最小的翻動次數輸出即可。



/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:filp game.cpp
* 作    者:單昕昕
* 完成日期:2016年4月26日
* 版 本 號:v1.0
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int p[4][4];//儲存棋盤
int Min=0x7fffffff;
int check()//檢查棋子是否同色
{
    int i,j;
    for(i=0; i<4; ++i)
        for(j=0; j<4; ++j)
            if(p[i][j]!=p[0][0])
                return -1;
    return 1;
}
void change(int i,int j)//改變自己和上下左右的顏色
{
    p[i][j]=!p[i][j];
    if(i-1>=0)
        p[i-1][j]=!p[i-1][j];
    if(i+1<4)
        p[i+1][j]=!p[i+1][j];
    if(j-1>=0)
        p[i][j-1]=!p[i][j-1];
    if(j+1<4)
        p[i][j+1]=!p[i][j+1];
}
void dfs(int d,int step)//列舉深搜
{
    int x,y;
    if(d==16)
    {
        if(check()==1)
            if(step<Min)
                Min=step;
        return;
    }
    else
    {
        x=d/4;//行
        y=d%4;//列
        dfs(d+1,step);
        change(x,y);//不行就翻回來
        dfs(d+1,step+1);
        change(x,y);
    }
}
int main()
{
    int i,j;
    char t;
    memset(p,0,sizeof(p));
    for(i=0; i<4; ++i)
        for(j=0; j<4; ++j)
        {
            cin>>t;
            if(t=='w')
                p[i][j]=1;//1表示白色w,0表示黑色b
        }
    dfs(0,0);
    if(Min==0x7fffffff)//0x7fffffff是int的最大值
        cout<<"Impossible"<<endl;
    else
        cout<<Min<<endl;
    return 0;
}


相關文章