zoukankan      html  css  js  c++  java
  • [POJ1753] 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
    

    Source

    Northeastern Europe 2000

    题解

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<bitset>
    #include<vector>
    using namespace std;
    
    const int maxw=0xffff;
    int change[4][4],First;
    struct node
    {
    	int w,n;
    }q[100000];
    bool flag[maxw];
    
    bool Can(int x,int y)//判断是否越界
    {
    	if(x<0||y<0||x>=4||y>=4) return 0;
    	return 1;
    }
    
    void Init()//预处理变化每一个所引起的变化
    {
    	for(int i=0;i<4;++i)
    		for(int j=0;j<4;++j)
    		{
    			change[i][j]^=1<<((i<<2)+j);
    			if(Can(i-1,j)) change[i][j]^=1<<(((i-1)<<2)+j);
    			if(Can(i+1,j)) change[i][j]^=1<<(((i+1)<<2)+j);
    			if(Can(i,j-1)) change[i][j]^=1<<((i<<2)+j-1);
    			if(Can(i,j+1)) change[i][j]^=1<<((i<<2)+j+1);
    		}
    }
    
    void Read()//读入
    {
    	string s;
    	for(int i=0;i<4;++i)
    	{
    		getline(cin,s);
    		for(int j=0;j<4;++j)
    			if(s[j]=='b') First^=1<<(i*4+j);
    	}
    }
    
    void Bfs()//状态压缩BFS
    {
    	int l,r;
    	l=r=1,q[1]=(node){First,0};
    	flag[First]=1;
    	int YH;
    	while(l<=r)
    	{
    		if(q[l].w==0||q[l].w==maxw)
    		{
    			printf("%d
    ",q[l].n);
    			return;
    		}
    		for(int i=0;i<4;++i)
    			for(int j=0;j<4;++j)
    			{
    				YH=(q[l].w^change[i][j]);
    				if(!flag[YH])
    					flag[YH]=1,++r,q[r]=(node){YH,q[l].n+1};
    			}
    		++l;
    	}
    	puts("Impossible");
    }
    
    int main()
    {
    	Init(),Read(),Bfs();
    	return 0;
    }
    

    本文作者:OItby @ https://www.cnblogs.com/hihocoder/

    未经允许,请勿转载。

  • 相关阅读:
    Danny_Su的devexpress 9.3.3的注册插件在vs2010RC中无法使用的解决办法
    EntitySpaces2009中的关系
    EntitySpaces2009的开发文档地址
    EntitySpaces2009支持事务
    EntitySpaces2009中连接Access的连接设置
    基于matlab的视频测速处理
    图像拼接与融合
    某图像分析系统
    黑子数与开盘指数相关性分析
    另类的图像处理
  • 原文地址:https://www.cnblogs.com/hihocoder/p/11482136.html
Copyright © 2011-2022 走看看