码迷,mamicode.com
首页 > 其他好文 > 详细

Flip Game

时间:2015-08-13 18:03:24      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

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


题解:枚举翻转次数再暴力搜索。其实是简单题。


#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char map[5][5];
int d[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int ans;

bool judge()  //全部同色 
{
	for(int i = 0;i < 4;i++)
	{
		for(int j = 0;j < 4;j++)
		{
			if(map[i][j] != map[0][0])
			{
				return false;
			}
		}
	}
	
	return true;
}

void flip(int n)   //翻转 
{
	int x;
	int y;
	if(n % 4 == 0)   //把n看做是格子是第几个,就是把格子看成第1-16个(编号), 
	{                //此时可以得到坐标和编号的关系 
		x = n / 4 - 1;
	}
	else
	{
		x = n / 4;
	}
	y = n - 4 * x - 1;
	map[x][y] = (map[x][y] == 'b' ? 'w' : 'b');
	for(int i = 0;i < 4;i++)
	{
		int xx = x + d[i][0];
		int yy = y + d[i][1];
		if(xx >= 0 && xx < 4 && yy >= 0 && yy < 4)
		{
			map[xx][yy] = (map[xx][yy] == 'b' ? 'w' : 'b');
		}
	}
}

bool dfs(int t,int s,int n)
{
	if(n > 16)   //只能翻转16个 
	{
		return false;
	}
	if(s == t)  //翻转次数到了 
	{
		if(judge())  //全部颜色一致?? 
		{
			return true;
		}
		return false;
	}
	flip(n + 1);  //翻转 
	if(dfs(t,s + 1,n + 1))  //表示翻转 
	{   
		return true;
	}
	flip(n + 1);      //翻转回去,下面表示不翻转该点 
	if(dfs(t,s,n + 1))
	{
		return true;
	}
	return false;
}

int main()
{
	while(scanf("%s",map[0]) != EOF)
	{
		for(int i = 1;i < 4;i++)
		{
			scanf("%s",map[i]);
		}
		bool flag = true;
		for(int i = 0;i < 4;i++)
		{
			for(int j = 0;j < 4;j++)
			{
				if(map[0][0] != map[i][j])
				{
					flag = false;
					break;
				}
			}
		}
		if(flag)
		{
			printf("0\n");
			continue;
		}
		flag = true;
		for(int i = 1;i <= 16;i++)
		{
			if(dfs(i,0,0))  //i表示翻转次数,0表示翻转了几次,0表示翻转的是第几个 
			{
				printf("%d\n",i);
				break;
			}
			if(i == 16)
			{
				flag = false;
			}
		}
		if(!flag)
		{
			printf("Impossible\n");
		}
		
	}
	
	
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Flip Game

标签:

原文地址:http://blog.csdn.net/wang2534499/article/details/47615805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!