poj1753--Flip Game(推导)

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29136   Accepted: 12600

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

poj1753--Flip Game(推导)_第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
遍历第一行,得到2^4种情况。对每一种情况推导的全黑或全白需要的步骤,例:如果要推导全白的情况,a[1][2]为黑,那么只能改变a[1+4][2]这一枚棋子,达到改变a[1][2]的目的,以这样的规则推导出最后的结果

#include <stdio.h>
int min_temp ;
void f(int a[],int n)
{
    a[n] = 1 - a[n] ;
    if(n >= 4)
        a[n-4] = 1 - a[n-4] ;
    if(n <= 11)
        a[n+4] = 1 - a[n+4] ;
    if(n%4!=0)
        a[n-1] = 1 - a[n-1] ;
    if(n%4!=3)
        a[n+1] = 1 - a[n+1] ;
}
void dfs(int s[],int n,int temp)
{
    int i , a[16] ;
    for(i = 0 ; i < 16 ; i++)
        a[i] = s[i] ;
    if(n == 4)
    {
        int i , j , k = temp;
        for(i = 0 ; i < 12 ; i++)
        {
            if(a[i])
            {
                f(a,i+4);
                k++ ;
            }
        }
        int num = 0 ;
        for(i = 12 ; i < 16 ; i++)
            num += a[i] ;
        if(num == 0 && min_temp > k)
            min_temp = k ;
        for(i = 0 ; i < 16 ; i++)
            a[i] = s[i] ;
        k = temp ;
        for(i = 0 ; i < 12 ; i++)
            if(a[i] == 0)
            {
                f(a,i+4);
                k++ ;
            }
        num = 0 ;
        for(i = 12 ; i < 16 ; i++)
            num += a[i] ;
        if(num == 4 && min_temp > k)
            min_temp = k ;
        return ;
    }
    dfs(a,n+1,temp) ;
    a[n] = 1 - a[n] ;
    if(n != 0)
        a[n-1] = 1 - a[n-1] ;
    if(n != 3)
        a[n+1] = 1 - a[n+1] ;
    a[n+4] = 1 - a[n+4] ;
    dfs(a,n+1,temp+1) ;
}
int main()
{
    int i , j , a[16];
    char str[4][4] ;
    min_temp = 9999 ;
    for(i = 0 ; i < 4 ; i++)
        scanf("%s", str[i]);
    for(i = 0 ; i < 4 ; i++)
        for(j = 0 ; j < 4 ; j++)
            if(str[i][j] == 'b')
                a[4*i+j] = 1 ;
            else
                a[4*i+j] = 0 ;
    dfs(a,0,0);
    if(min_temp == 9999)
        printf("Impossible\n");
    else
        printf("%d\n", min_temp);
    return 0;
}


你可能感兴趣的:(poj1753--Flip Game(推导))