poj 1753Flip Game---枚举

来源:互联网 发布:美篇是什么软件 编辑:程序博客网 时间:2024/05/16 17:11

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

看了别人的,才知道要这样做啊

#include<stdio.h>#include<string.h>#include<stdlib.h>bool map[4][4];///存储初始的地图bool tp[4][4];///翻牌后的地图char ls[4][4];bool flag[4][4];int k,key;void dfs(int n, int m)///深搜{    if(n == k)///翻了k张牌    {        for(int i = 0; i < 4; i++)        {            for(int j = 0; j < 4; j++)                tp[i][j] = map[i][j];        }        for(int i = 0; i < 4; i++)///翻完牌后的情况        {            for(int j  = 0; j < 4; j++)            {                if(flag[i][j])                {                     tp[i][j] = !tp[i][j];                    if(i-1>= 0)                        tp[i-1][j] = !tp[i-1][j];                    if(i+1 < 4)                        tp[i+1][j] = !tp[i+1][j];                    if(j-1 >= 0)                        tp[i][j-1] = !tp[i][j-1];                    if(j+1 < 4)                        tp[i][j+1] = !tp[i][j+1];                }            }        }        bool mm = tp[0][0];        key = 1;        for(int i = 0; i < 4; i++)///看是否完成        {                for(int j = 0; j < 4; j++)                {                        if(tp[i][j] != mm)                        {                            key = 0;                             break;                        }                }                if(key == 0)                   break;        }        return ;    }    for(int i = m+1; i < 16; i++)///枚举翻的牌,用flag标记    {        int x = i/4;        int y = i%4;        flag[x][y] = 1;        dfs(n+1,i);        if(key)            return ;        flag[x][y] = 0;    }}int main(){    while(~scanf("%s",ls[0]))///为了多组输入    {        for(int i = 1; i < 4; i++)            scanf("%s",ls[i]);        for(int  i = 0; i < 4; i++)///把英文的变为一个0,1的,可以用位运算        {            for(int j = 0; j < 4; j++)            {                if(ls[i][j] == 'w')                    map[i][j] = 1;                else                    map[i][j] = 0;            }        }        key = 0;///重要的控制变量        for(k = 0; k <= 16; k++)///枚举1步2步还是        {             memset(flag,0,sizeof(flag));///清空标记            dfs(0,-1);///开始翻牌            if(key)                break;        }        if(key)            printf("%d\n",k);        else            printf("Impossible\n");    }    return 0;}

另外,把云飞巨 的代码沾上,我是看他的思路

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;char s[5][5];bool flag[5][5];int k;bool bh[5][5];bool mark;bool tmp[5][5];void dfs_search(int cnt,int cont){    if(cnt==k)    {    for(int i=0;i<4;i++){for(int j=0;j<4;j++){tmp[i][j]=bh[i][j];}}        for(int i=0; i<4; i++)        {            for(int j=0; j<4; j++)            {                if(flag[i][j])                   {                   tmp[i][j]=!tmp[i][j];                   if(i-1>=0)tmp[i-1][j]=!tmp[i-1][j];if(i+1<4)tmp[i+1][j]=!tmp[i+1][j];if(j-1>=0)tmp[i][j-1]=!tmp[i][j-1];if(j+1<4)tmp[i][j+1]=!tmp[i][j+1];                   }            }        }        bool x=tmp[0][0];        mark=1;        for(int i=0; i<4; i++)        {            for(int j=0; j<4; j++)            {                if(x!=tmp[i][j])                {                    mark=0;                    break;                }            }            if(mark==0)                break;        }        return ;    }    for(int i=cont+1; i<16; i++)    {        int x=i/4;        int y=i%4;        flag[x][y]=1;        dfs_search(cnt+1,i);        if(mark)            return;        flag[x][y]=0;    }}int main(){    while(~scanf("%s",s[0]))    {        for(int i=1; i<4; i++)        {            scanf("%s",s[i]);        }        mark=0;        for(int i=0; i<4; i++)        {            for(int j=0; j<4; j++)                if(s[i][j]=='w')                    bh[i][j]=1;                else                    bh[i][j]=0;        }        mark=0;        for(k=0; k<=16; k++)        {            memset(flag,0,sizeof(flag));            dfs_search(0,-1);            if(mark)                break;        }        if(mark)            printf("%d\n",k);        else            printf("Impossible\n");    }    return 0;}


0 0
原创粉丝点击