Flip Game-poj 1753

Flipgame算法解析
本文介绍了一个基于4x4棋盘的游戏——Flipgame的算法实现。玩家通过翻转棋子达到全白或全黑的目标状态,文章详细阐述了游戏规则及算法解决方案,并提供了完整的C++代码示例。
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 }

 

转载于:https://www.cnblogs.com/sdxk/p/4298238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值