POJ 1753-Flip Game(枚举&&DFS)

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

POJ 1753-Flip Game(枚举&&DFS)_第1张图片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;
}


你可能感兴趣的:(枚举,poj,game,DFS,flip)