• poj 1753 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
    



    #include <stdio.h> #include <string.h> #include <math.h> char map[6][6],copy[6][6],str[20]; const int INF=666; int ans; int flag[6][6]; bool Check(char copy[][6]){//检查所有棋子是不是都是一样的颜色 char ch=copy[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(copy[i][j]!=ch) return false; return true; } void Change(int i,int j){//棋子颜色的翻转 copy[i][j]=(char)('b'+'w'-copy[i][j]); if(i!=0) copy[i-1][j]=(char)('b'+'w'-copy[i-1][j]); if(j!=0) copy[i][j-1]=(char)('b'+'w'-copy[i][j-1]); if(i!=3) copy[i+1][j]=(char)('b'+'w'-copy[i+1][j]); if(j!=3) copy[i][j+1]=(char)('b'+'w'-copy[i][j+1]); } void BFS(){//暴力搜索加位运算 if(Check(map))//若是本身就已经是满足的情况,就不必翻转 ans=0; else for(int t=1;t<65536;t++){//枚举所有的翻转情况,并且分析 int num=t; int step=0; for(int coi=0;coi<4;coi++) for(int coj=0;coj<4;coj++) copy[coi][coj]=map[coi][coj];//复制棋子图 for(int i=0;i<4;i++) for(int j=0;j<4;j++){ flag[i][j]=num%2;//利用位运算进行分析 num/=2; if(flag[i][j]==1){//1要变,0保持原状 Change(i,j); step++; //记录变化的次数 } } if(Check(copy))//当期棋子图满足要求 ans=ans<step?ans:step;//及时更新最小的变化次数 } } int main(){ for(int i=0;i<4;i++) gets(map[i]); ans=INF; BFS();//主要搜索函数 if(INF!=ans)//若是没有一种情况满足,ans为初值INF printf("%d ",ans); else puts("Impossible"); return 0; }
  • 相关阅读:
    Mime Types
    对MySQL DELETE语法的详细解析
    创建MySQL存储过程示例
    Python安装
    python学习之matplotlib实战
    python学习之numpy实战
    SQLserver建表规则
    机器学习之BP神经网络
    机器学习之决策树算法
    Spring学习总结
  • 原文地址:https://www.cnblogs.com/huaixiaohai2015/p/5168698.html
Copyright © 2020-2023  润新知