poj1753――Flip Game

原题:

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

bwwb bbwb bwwb bwww

Sample Output

4 

 

题意与分析:

有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使4*4的正方形变为纯白或者纯黑?

枚举所有的可能 来确定最小值

原码:

#include<iostream> #include<string.h> using namespace std; int map[6][6]; int initMap[6][6]; int ope[6][6]; void opeInit(int i){//初始化操作矩阵第一行     int temp=i;         for(int j=1;j<=4;j++){             ope[1][j]=temp%2;             temp/=2;         }         for(int j=1;j<=4;j++)             map[1][j]=(map[1][j]+ope[1][j]+ope[1][j-1]+ope[1][j+1])%2;         return ; }  int calOpe(int i,int black){     //拷贝棋盘,初始化         for(int k=1;k<=4;k++)             for(int j=1;j<=4;j++)                 map[k][j]=initMap[k][j];          memset(ope,0,sizeof(ope));         opeInit(i);//第一行采取枚举,后几行根据上一行棋盘情况          for(int j=2;j<=4;j++){             //完成对ope矩阵的填充             for(int k=1;k<=4;k++){             if(map[j-1][k]==black) //                 ope[j][k]=1;             else                 ope[j][k]=0;             }             //修改当前棋盘的状态             for(int k=1;k<=4;k++){                 map[j][k]=(map[j][k]+ope[j][k]+ope[j][k-1]+ope[j][k+1]+ope[j-1][k])%2;             }         }          //检验是否符合要求         bool isOk=true;         for(int i=1;i<=4;i++){             if(map[4][i]==black){//                 isOk=false;                 break;             }         }         int sum=0;         if(isOk){             for(int i=1;i<=4;i++)                 for(int k=1;k<=4;k++){                     if(ope[i][k]==1)                         sum++;                 }                 return sum;         }         return 20; } int smaller(int a,int b){     if(a<b)return a;     else return b; }   int main(){     char cinchar;     int min=20;     //棋盘输入操作         for(int i=1;i<=4;i++)             for(int j=1;j<=4;j++){                 cin>>cinchar;                 if(cinchar=='b')initMap[i][j]=1;                 else initMap[i][j]=0;             }     for(int i=0;i<=15;i++){//枚举16次选择最大值         int blackNum=calOpe(i,1);         int whiteNum=calOpe(i,0);         int smallest=smaller(blackNum,whiteNum);             if(min>smallest)min=smallest;         }     if(min==20)cout<<"Impossible"<<endl;     else cout<<min<<endl;     return 0; }  


 

 

你可能感兴趣的:(java,开发技术)