zoj 2050 -Flip Game题解

本文介绍了一款名为FlipGame的游戏,该游戏在一个4x4的棋盘上进行,目标是通过翻转棋子使得所有棋子颜色一致。文章详细解析了使用广度优先搜索(BFS)算法解决此问题的方法,并分享了利用位运算实现高效状态记录和转换的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flip Game

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 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).


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input


2

bwbw
wwww
bbwb
bwwb

bwwb
bbwb
bwwb
bwww


Sample Output

Impossible

4

这个题,第一次看的时候完全没有思路,虽然知道是用bfs来做,可是不知道怎么来建模型,实现这个过程,甚至对那个状态的判断都不是很明白,后来想想才明白,

  此题目的是达到全白,或者全黑,从当前第一个棋盘模式开始,搜索,从当前开始,在键盘上任意操作一次就是一个状态,就算作上个节点的邻接点,所以,大致思路就是明白了,具体实现的时候,才感觉到这个题,关键点是,怎么实现标记,以及怎么实现每一步的操作,都是关键点。每个状态都对应一个数字,如果看成二进制的话,可以求出来,但是该值出队列的时候,怎么把它还原以及怎么找它的下一个邻接点是个耐人寻味的事情。

       当然了,开始时候是没有觉悟到用,位运算来操作的,都是觉得别的方法太麻烦,才不得不想出这个做法。

       位运算,其实很简单,只不过是我们平时用的较少罢了。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
bool staus[65536];
char map[4][4];
int bottom[4][4];
int drex[4]={-1,0,1,0};
int drey[4]={0,1,0,-1};
bool Moveable(int x,int y){
     if(x<0||x>3||y<0||y>3)
     return false;
     return true;
     }
typedef struct node{
       int data;
       int floor;
       }node;
       queue<node> test;
int main(){
    int N,i,j,start,posion,case1=0;
    node startnode,temp,nextnode;

    scanf("%d",&N);
    
    while(N--){
     int key=0;
     start=0;
     case1++;
    
     memset(staus,0,sizeof(staus));
     for(i=0;i<4;i++){
         scanf("%s", map[i]);
       for(j=0;j<4;j++){
        
         if(map[i][j]=='w')bottom[i][j]=1;
         else bottom[i][j]=0;
         start+=bottom[i][j]*(int)pow(2.0,(i*4+j)*1.0);
       }
       
     }
     
     startnode.data=start;
     startnode.floor=0;
     staus[start]=true;
     test.push(startnode);
     
     
     while(!test.empty()){
        temp=test.front();
        test.pop();
        
        if(temp.data==65535||temp.data==0){
        printf("%d\n",temp.floor);
        key=1;
        break;                                   
        }                  
      
      
        for(posion=0;posion<16;posion++){
             nextnode.data=temp.data^(1<<posion);
             for(i=0;i<4;i++){
                if(Moveable(posion/4+drex[i],posion%4+drey[i])){
                nextnode.data^=1<<((posion/4+drex[i])*4+posion%4+drey[i]);                                             
                }
             }
             if(!staus[nextnode.data]){
               staus[nextnode.data]=true;
               nextnode.floor=temp.floor+1;
               test.push(nextnode);                          
             }
        }
        
        
     }//while(!test**)
     
     if(!key)
         printf("Impossible\n");
    
     while(!test.empty())
         test.pop();  
       if(N)printf("\n");
    }
   
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值