poj 1753 Flip Game

来源:互联网 发布:淘宝左侧客服模板代码 编辑:程序博客网 时间:2024/06/09 16:40
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39949 Accepted: 17341

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

提示

题意:
这是一个翻转游戏,在4*4的格子内,有黑白两种棋子,遵循以下规则:
1.选择4*4格子中的一个棋子。
2.翻转其中一个棋子,使其变色,如果该棋子的上下左右存在棋子的话,同样也要变色。
求在这盘棋中寻找最少翻转棋子次数使得整盘棋都是一种颜色。
思路:
实际上翻转同一个棋子,0和偶数次数是一样的情况,1和奇数次数也是一样的,那么问题就转化到哪几个棋子需要翻转,用dfs进行枚举就得出答案。

示例程序

Source CodeProblem: 1753Code Length: 1306BMemory: 392KTime: 157MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>int bug;int judge(char a[4][5],int deep){    int i,i1;    for(i=0;4>i;i++)    {        for(i1=0;4>i1;i1++)        {            if(a[i][i1]!=a[0][0])            {                return 0;            }        }    }    if(bug>deep)    {        bug=deep;    }    return 1;}char change(char c){    if(c=='b')    {        c='w';    }    else    {        c='b';    }    return c;}void flip(int i,char a[4][5]){    int x=i/4,y=i%4;    a[x][y]=change(a[x][y]);    if(x+1<4)    {        a[x+1][y]=change(a[x+1][y]);    }    if(x-1>=0)    {        a[x-1][y]=change(a[x-1][y]);    }    if(y+1<4)    {        a[x][y+1]=change(a[x][y+1]);    }    if(y-1>=0)    {        a[x][y-1]=change(a[x][y-1]);    }}void dfs(int i,int deep,char a[4][5]){    int i1;    char b[4][5];    if(judge(a,deep)==1)//符合就回去,不符合接着找    {        return;    }    if(i==16)//16个都翻了,也就翻不动了    {        return;    }    for(i1=0;i1<4;i1++)    {        strcpy(b[i1],a[i1]);    }    dfs(i+1,deep,b);    flip(i,b);//翻棋子的函数    dfs(i+1,deep+1,b);}int main(){    int i;    char a[4][5];    bug=17;//最大翻转16个棋子,这里进行初始化    for(i=0;4>i;i++)    {        scanf("%s",a[i]);    }    dfs(0,0,a);    if(bug!=17)    {        printf("%d",bug);    }    else    {        printf("Impossible");    }    return 0;}


0 0
原创粉丝点击