poj-Flip Game

来源:互联网 发布:苹果系统mac系统下载 编辑:程序博客网 时间:2024/04/30 01:19

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40250 Accepted: 17477
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:
Choose any one of the 16 pieces.
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

思路:问题是说最少多少次可以把棋子全部翻成一种颜色。
每个棋子翻奇数次才改变颜色,偶数次颜色不变,不难得到,每个棋子要么翻一次,要么不翻(即0次)。
只有16个格子,选择0个格子,1个,两个进行反转,总情况为2^16。
每次翻转后,都要判断翻转后是否为一个颜色。
只要某个过程中出现全为一个颜色,就结束,输出这个过程翻得格子数。相反,如果16个格子都翻了,颜色还不为纯色,则说明不可能,输出impossible.
具体看下面代码:

#include <stdio.h>int map[4][4];bool flag;bool isok(){    int i,j;    for(i=0;i<4;i++)    {        for(j=0;j<4;j++)        {            if(map[i][j]!=map[0][0])                return false;        }    }     return true;}void change(int xi,int yi)//翻棋子(变成相反的颜色){    map[xi][yi]=!map[xi][yi];    if(xi+1<=3) map[xi+1][yi]=!map[xi+1][yi];    if(yi+1<=3) map[xi][yi+1]=!map[xi][yi+1];    if(xi-1>=0) map[xi-1][yi]=!map[xi-1][yi];    if(yi-1>=0) map[xi][yi-1]=!map[xi][yi-1];}bool dfs(int n,int k)//k每次要翻的次数{    if(n==0)//n=0说明本次循环完成    {        if(isok())//判断是否颜色都一样            return true;        else            return false;    }    for(int i=k;i<16;i++)    {        int r=i/4,c=i%4;//r横坐标,c纵坐标         change(r,c);        if(dfs(n-1,i+1))//每次都要进行             return true;        change(r,c);    }     return false;}int main(){    char str[5];    int i,j;    for(i=0;i<4;i++)    {        scanf("%s",str);        for(j=0;j<4;j++)//我采用的是把字符转成数字,比较方便        {            if(str[j]=='b')                map[i][j]=1;//黑色1            else                map[i][j]=0;//白色0        }    }    for(i=0;i<=16;i++)    {        if(dfs(i , 0))//0表示次数         {             flag=true;            break;        }    }    if(!flag)        printf("Impossible\n");    else        printf("%d\n",i);//  for(i=0;i<4;i++)//  {//      for(j=0;j<4;j++)//          printf("%d",map[i][j]);//      printf("\n");//  }    return 0;} 
0 0