现在的位置: 首页 > 综合 > 正文

POJ 1753

2018年02月01日 ⁄ 综合 ⁄ 共 2622字 ⁄ 字号 评论关闭
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 ;
}

【上篇】
【下篇】

抱歉!评论已关闭.