poj1753--Flip Game

来源:互联网 发布:中国新闻周刊 知乎 编辑:程序博客网 时间:2024/05/19 00:48
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 29079 Accepted: 12579

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

bwwbbbwbbwwbbwww

Sample Output

4

Source

Northeastern Europe 2000
黑白棋翻转问题,没翻动一块棋子,会带动它的上下左右同时翻转(如果存在),问最快多少步可以成为全黑或是全白,如果翻转不成输出Impossible,

在这期间每块棋子只有翻动第一次是有意义的,在翻转就会恢复原来的样子,所以通过16块棋子翻转还是不翻转,来判断能不能得到最总的结果,用bfs,每次都新的棋子翻转或是不翻加入top,从第一块开始,一直到第十六块,最终会得到结果
#include <stdio.h>struct node{    int a[16] ;    int t ;    int n ;}p[1000000];int main(){    int i , j , a[16] , min_t = 999999;    char s[4][4] ;    for(i = 0 ; i < 4 ; i++)        scanf("%s", s[i]);    for(i = 0 ; i < 4 ; i++)        for(j = 0 ; j < 4 ; j++)            if(s[i][j] == 'b')                a[4*i+j] = 1 ;            else                a[4*i+j] = 0 ;    int low = 0 , top = 1 ;    p[0].t = 0 ;    p[0].n = -1 ;    for(i = 0 ; i < 16 ; i++)        p[0].a[i] = a[i] ;    while(low < top)    {        node k ;        k = p[low++] ;        int num = 0 ;        for(i = 0 ; i < 16 ; i++)            num += k.a[i];        if( (num == 0 || num == 16) && k.t < min_t )            min_t = k.t ;        if(k.n == 15)            continue ;        p[top] = k ;        p[top].n++ ;        top++ ;        p[top] = k ;        p[top].n++ ;        j = p[top].n ;        p[top].a[j] = 1 - p[top].a[j] ;        if(j >= 4)            p[top].a[j-4] = 1 - p[top].a[j-4];        if(j <= 11)            p[top].a[j+4] = 1 - p[top].a[j+4] ;        if(j % 4 > 0)            p[top].a[j-1] = 1 - p[top].a[j-1] ;        if(j % 4 < 3)            p[top].a[j+1] = 1 - p[top].a[j+1] ;        p[top].t++ ;        top++ ;    }    if(min_t == 999999)        printf("Impossible\n");    else        printf("%d\n", min_t);    return 0;}


0 0