首页 > ACM题库 > HDU-杭电 > HDU 4434-Game[解题报告]HOJ
2015
07-16

HDU 4434-Game[解题报告]HOJ

Game

问题描述 :

Alice and Bob are playing game. Firstly, Alice drew a regular polygon with n vertices, then assigned each vertex a number.
Then Bob’s goal is to change all the numbers to non-negative numbers. He can do following operation in each step: choose one vertex i, supposed Ai is the number on vertex i, then add Ai to it’s left and right adjacent vertex, and turn Ai into -Ai.
Now you are to help Bob to find out the minimum step to reach his goal.

输入:

Multiple test cases ended with EOF.
Each test case begins with one integer n, followed a line consist of by n integers representing the numbers on the vertices clockwise.

2<n≤105
|Ai| ≤ 105
|sum{Ai}| ≤ 10

输出:

Multiple test cases ended with EOF.
Each test case begins with one integer n, followed a line consist of by n integers representing the numbers on the vertices clockwise.

2<n≤105
|Ai| ≤ 105
|sum{Ai}| ≤ 10

样例输入:

3
1 -2 3

样例输出:

2
Hint
In the sample, first turn -2 to 2, then the sequences became -1 2 1, then choose -1, the result is 1 1 0, all the numbers are non-negative, so the minimum step would be 2.

Description
Flip game is played on a rectangular 4×4 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).

GameConsider 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
 /*
  * flipgame.cpp
  *
  *  Created on: 2015年2月23日
  *      Author: xk
  */
 #include<iostream>
 using namespace std;
 int main(){
     char input[16];
     int mm[16];
     int cc[16];
     int num=16;
     int in=0,res=0;
     for(int i=0;i<num;i++){
         cin>>input[i];
         mm[i]=1;
     }
     for(int i=0;i<num;i++){
         in*=2;
             if(input[i]=='b')
                 in+=1;
             mm[i]=mm[i]<<(num-i-1);
         }
     for(int j=0;j<num;j++){
         int temp=j;
         cc[temp]=mm[temp];
         if(temp/4!=0)
             cc[temp]+=mm[temp-4];
         if(temp/4!=3)
             cc[temp]+=mm[temp+4];
         if(temp%4!=0)
             cc[temp]+=mm[temp-1];
         if(temp%4!=3)
             cc[temp]+=mm[temp+1];
     }
     int *com=new int[num];
     for(int i=0;i<num;i++){
         com[i]=0;
     }
     int rr=0,flag=0;
     if(in!=0&&in!=(1<<16)-1){
     for(int i=0;i<num;i++){
             for(int j=0;j<i+1;j++){
                 com[j]=j;
             }
             com[i]=com[i]-1;
             do{
                 res=in;
                 int cha=0;
                 for(int j=i;j>=0;j--){
                     if(com[j]!=num-(i-j)-1){
                         cha=j;
                         break;
                     }
                 }
                 com[cha]+=1;
                 for(int j=cha+1;j<i+1;j++){
                     com[j]=com[cha]+j-cha;
                 }
                 for(int j=0;j<i+1;j++){
                     int temp=com[j];
                     res=res^cc[temp];
                 }
                 if(res==0||res==(1<<16)-1){
                     flag=1;
                     rr=i+1;
                     break;
                 }
                 if(com[0]==num-i-1&&com[i]==num-1){
                     break;
                 }
             }while(1);
             if(flag==1)
                 break;
     }
     }else{
         flag=1;
     }
     if(flag==1){
         cout<<rr<<endl;
     }else{
         cout<<"Impossible"<<endl;
     }
     return 0;
 }

 

参考:http://www.cnblogs.com/sdxk/p/4298238.html