poj 1753 Flip Game

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

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 36419   Accepted: 15866

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

 
题意:输入4*4的字符,包含b,w,每次选择一个位置,改变这个位置上下左右包括自身的值,问最少需要改变多少次。
 
分析: 暴力枚举一遍。
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[6][6];
char str[10][10];
int b[5][2]= {1,0,0,1,-1,0,0,-1,0,0};
int Min;

int pai(int ans)
{
    int t=a[1][1];
    int flag = 0;
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            if(a[i][j]!=t)
            {
                flag = 1;
                i = 5;
                break;
            }
        }
    }
    if(flag == 0)
    {
        if(ans < Min)
            Min = ans;
        return 1;
    }
    return 0;
}

void DFS(int x, int y, int ans)
{
    if(y>4)
    {
        x+=1;
        y-=4;
    }
    if(x>4||y>4)
    {
        int f = pai(ans);
        return ;
    }
    DFS(x, y+1, ans);
    for(int k=0; k<5; k++)
    {
        a[x+b[k][0]][y+b[k][1]]^=1;
    }
    DFS(x, y+1, ans+1);
    for(int k=0; k<5; k++)
    {
        a[x+b[k][0]][y+b[k][1]]^=1;
    }
}

int main()
{
    char str[10][10];
    memset(a,-1,sizeof(a));
    for(int i=1; i<=4; i++)
    {
        scanf("%s", str[i]+1);
    }
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            if(str[i][j] == 'b')
                a[i][j] = 1;
            else
                a[i][j] = 0;
        }
    }
    Min = 20;
    if(pai(0))
        Min = 0;
    else
        DFS(1,1,0);
    if(Min==20)
        printf("Impossible
");
    else
        printf("%d
",Min);

    return 0;
}
原文地址:https://www.cnblogs.com/mengzhong/p/5036390.html