zoukankan      html  css  js  c++  java
  • POJ 1753

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 26203   Accepted: 11308

    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

    //508K	16MS
    //广度搜索
    #include <iostream>
    #include <queue>
    using namespace std;
    
    static const int GRID_NUM = 16 ; //小格子的个数
    static const int MAX_STATES = 1 << GRID_NUM ; //最多的状态个数
    int states[MAX_STATES] ; //是否已经被访问过,-1表示没有访问过,否则表示经过多少次翻转操作访问到的
    
    bool isComplete(int state)
    {
    	return (state == 0 || state == ((1 << GRID_NUM) - 1)) ;
    }
    
    int flipState(int state, int i)
    {
    	state ^= (1 << i) ;
    	if (i - 4 >= 0)
    		state ^= (1 << (i - 4)) ;
    	if (i + 4 < GRID_NUM)
    		state ^= (1 << (i + 4)) ;
    
    	if ((i & ((1<<2) - 1)) != 0) //i % 4 != 0表示不是一行中最左端的那个
    		state ^= (1 << (i - 1)) ;
    	if((i & ((1<<2) - 1)) != 3)//i % 4 != 3表示不是一行中最右边的那个
    		state ^= (1 << (i + 1)) ;
    
    	return state ;
    }
    
    int buildState()
    {
    	int ret = 0 ;
    	char ch ;
    	for (int i = 0; i < GRID_NUM ; ++ i)
    	{
    		cin >> ch;
    		if (ch == 'b')
    			ret += (1 << i) ;
    	}
    	return ret ;
    }
    
    int minFlip(int startStates)
    {
    	int tmpState = startStates ;
    	memset(states, 0, sizeof(states)) ;
    	if (isComplete(tmpState)) 
    		return states[tmpState] ;
    
    	queue<int> statesQueue ;
    	statesQueue.push(tmpState) ;
    
    	while(!statesQueue.empty()) //广度搜索
    	{
    		tmpState = statesQueue.front() ;
    		statesQueue.pop() ;
    		for (int i = 0; i < GRID_NUM; ++ i)
    		{
    			int curState = flipState(tmpState, i) ;
    			if (states[curState] == 0)
    			{
    				states[curState] = states[tmpState] + 1 ; //翻转操作的次数增加一
    				if (isComplete(curState))
    					return states[curState] ;
    				statesQueue.push(curState) ;
    			}
    		}
    	}
    
    	return -1 ; //表示没有找到解
    }
    
    
    int main (int argc, char** argv)
    {
    	int startState = buildState() ;
    	int result = minFlip(startState) ;
    	if (result != -1)
    		cout << result << endl;
    	else
    		cout << "Impossible" << endl;
    	return 0 ;
    }



  • 相关阅读:
    JN_0041:在新版的edge浏览器中 将 url 地址 设置为应用,并在桌面上显示快捷方式
    H50074:base标签 指定资源地址
    MayaLearn0004: 层 大纲视图 特殊复制
    MayaLearn0003: 工具架 快捷菜单 枢轴 对齐
    MayaLearn0000: 快捷键命令
    MayaLearn0002: 软件基本操作工具 基本物体的创建 工作区菜单
    MayaLearn0001: 初识maya
    Ajax0006: ajax 读取 本地 js 配置文件
    JN_0040:如何下载视频流视频文件
    H50073:div 循环添加点击事件,swiper循环添加点击事件
  • 原文地址:https://www.cnblogs.com/dancingrain/p/3405188.html
Copyright © 2011-2022 走看看