zoukankan      html  css  js  c++  java
  • POj 1753--Flip Game(位运算+BFS)

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 30669   Accepted: 13345

    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

    我不得不说位运算是一个强大的东西,之前从来没怎么用过。没想到这么一道本来没有头绪的题,位运算一优化成了一道裸一维的BFS,说这个题目。大意是一个翻牌游戏。牌有黑白两面,当翻到全部的牌都是黑或都是白时,满足状态,输出最小操作步数。每一个位置的牌的都能够翻,但如过翻了当前这张牌。它的相邻的位置的牌都要翻过来。对于全部的状态。一共同拥有2^16种(4*4的图)
    而对于翻牌操作,如果当前状态为s 想要翻第i个位置的牌。仅仅要s^=1<<i;就可以。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    bool vis[70000];
    char ma[20];
    typedef struct node
    {
    	int state,step;
    };
    void bfs(int s)
    {
    	queue <node> Q;
    	node t;t.state=s;t.step=0;
    	vis[s]=1;
    	Q.push(t);
    	while(!Q.empty())
    	{
    		node v=Q.front();Q.pop();
    		if(v.state==0||v.state==65535)
    		{
    			cout<<v.step<<endl;
    			return ;
    		}
    		for(int i=0;i<16;i++)//枚举16个点
    		{
    			int tem=v.state;
    			tem^=1<<i;//自身翻转
    			if(i-4>=0) tem^=1<<(i-4);//上
    			if(i<12) tem^=1<<(i+4);//下
    			if(i%4!=0) tem^=1<<(i-1);//左
    			if((i+1)%4!=0) tem^=1<<(i+1);//右
    			if(!vis[tem])
    			{
    				vis[tem]=1;
    				t.state=tem;
    				t.step=v.step+1;
    				Q.push(t);
    			}
    		}
    	}
    	puts("Impossible");
    }
    int main()
    {
    	int i=0,s=0;
    	memset(vis,0,sizeof(vis));
    	while(i<16)
    	{
    		cin>>ma[i];
    		if(ma[i]=='b')
    			s+=1<<i;
    		i++;
    	}
    	bfs(s);
    	return 0;
    }
    





  • 相关阅读:
    jvm gc 线程
    高分辨率图像建筑物提取数据集制作
    Ubuntu 更改软件源
    后台程序员简单应用前端的bootstrap(小白)
    php--常见算法3
    php--常见算法2
    php--常见算法1
    php三种排序算法
    Django学习之十二:Cache 缓存组件
    Restframe_work 回顾记忆集
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6707269.html
Copyright © 2011-2022 走看看