BIT2014級軟件學院程序設計-03 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: Choose any one of the 16 pieces. 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

 

NOTICE: Sorry, there may be something wrong while using getchar() or scanf("%c", &c) to read the extra '\n' after each line.

To avoid it, you should use gets() orscanf("%s", s) to read line by line, or usechar c; scanf("%1s", &c) or scanf("\n%c", &c) to read char by char.

可用BFS或者DFS暴力,注意記錄狀態可用將地圖轉換爲二進制。也可直接用高斯消元。

DFS代碼如下

#include<stdio.h>
int map[6][6] = { 0 };
char shit[5][5];
int flag=1,step;
int dirx[5] = { -1,1,0,0,0 };
int diry[5] = { 0,0,0,1,-1 };
void operate(int x, int y)
{
	int i;
	for (i = 0;i <= 4;i++)
		map[x + dirx[i]][y + diry[i]] = !map[x + dirx[i]][y + diry[i]];
	return;
}




int check()
{
	int i, j;
	for (i = 1;i <= 4;i++)
		for (j = 1;j <= 4;j++)
		{
			if (map[i][j] != map[1][1])
				return 0;
		}

	return 1;
}
void dfs(int x, int y, int k)
{

	if (k == step)
	{
		flag = check();
		return;
	}
 if ( y == 5 || flag)
	 return;
	operate(x, y);
	if (x<4) dfs(x + 1, y, k + 1);
	else dfs(x, y + 1, k + 1);
	operate(x, y);
	if (x<4) dfs(x + 1, y, k );
	else dfs(1, y + 1, k );
	return;

}
int main()
{

	char wtf;
	int i, j, ans;
 for (i = 1;i <= 4;i++)
		scanf("%s",shit[i]);
	for (i = 1;i <= 4;i++)
		for (j = 0;j < 4;j++)
		{
			
			if ('w' == shit[i][j])
				map[i][j+1] = 0;
				else
				map[i][j+1] = 1;
		}

	for (step = 0;step <= 8;step++)
	{
		dfs(1, 1, 0);
		if (flag)
		break;
	}
	if (flag)
		printf("%d\n", step);
	else printf("Impossible\n");

}
//bwwb bbwb bwwb bwww



發佈了31 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章