zoukankan      html  css  js  c++  java
  • Flip Game-poj 1753

    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
     1 /*
     2  * flipgame.cpp
     3  *
     4  *  Created on: 2015年2月23日
     5  *      Author: xk
     6  */
     7 #include<iostream>
     8 using namespace std;
     9 int main(){
    10     char input[16];
    11     int mm[16];
    12     int cc[16];
    13     int num=16;
    14     int in=0,res=0;
    15     for(int i=0;i<num;i++){
    16         cin>>input[i];
    17         mm[i]=1;
    18     }
    19     for(int i=0;i<num;i++){
    20         in*=2;
    21             if(input[i]=='b')
    22                 in+=1;
    23             mm[i]=mm[i]<<(num-i-1);
    24         }
    25     for(int j=0;j<num;j++){
    26         int temp=j;
    27         cc[temp]=mm[temp];
    28         if(temp/4!=0)
    29             cc[temp]+=mm[temp-4];
    30         if(temp/4!=3)
    31             cc[temp]+=mm[temp+4];
    32         if(temp%4!=0)
    33             cc[temp]+=mm[temp-1];
    34         if(temp%4!=3)
    35             cc[temp]+=mm[temp+1];
    36     }
    37     int *com=new int[num];
    38     for(int i=0;i<num;i++){
    39         com[i]=0;
    40     }
    41     int rr=0,flag=0;
    42     if(in!=0&&in!=(1<<16)-1){
    43     for(int i=0;i<num;i++){
    44             for(int j=0;j<i+1;j++){
    45                 com[j]=j;
    46             }
    47             com[i]=com[i]-1;
    48             do{
    49                 res=in;
    50                 int cha=0;
    51                 for(int j=i;j>=0;j--){
    52                     if(com[j]!=num-(i-j)-1){
    53                         cha=j;
    54                         break;
    55                     }
    56                 }
    57                 com[cha]+=1;
    58                 for(int j=cha+1;j<i+1;j++){
    59                     com[j]=com[cha]+j-cha;
    60                 }
    61                 for(int j=0;j<i+1;j++){
    62                     int temp=com[j];
    63                     res=res^cc[temp];
    64                 }
    65                 if(res==0||res==(1<<16)-1){
    66                     flag=1;
    67                     rr=i+1;
    68                     break;
    69                 }
    70                 if(com[0]==num-i-1&&com[i]==num-1){
    71                     break;
    72                 }
    73             }while(1);
    74             if(flag==1)
    75                 break;
    76     }
    77     }else{
    78         flag=1;
    79     }
    80     if(flag==1){
    81         cout<<rr<<endl;
    82     }else{
    83         cout<<"Impossible"<<endl;
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    ZedBoard学习(6)System Generator实现串口通信(一行HDL代码都不用写)
    ZedBoard学习(1)Ubutun下进行串口通信
    Zedboard学习(7)PS下第一个裸奔程序
    激光雷达(一)数据采集C++
    win7/win8下安装Oracle1出错10g,提示“程序异常终止,发生未知错误”解决方法
    XML文件的加密与解密
    三层中最重要的SqlHelper类
    创建桌面快捷方式的语法
    秋招总结 艾尔夏尔
    thoughtworks二面准备 (三) 艾尔夏尔
  • 原文地址:https://www.cnblogs.com/sdxk/p/4298238.html
Copyright © 2011-2022 走看看