基础算法 第八周 Flip Game

---------------以下是个人感受-------------
其实这道题是放在广搜的单元里,但是无论如何我都觉得用“熄灯问题”的解法更快更省内存。


---------------以下是题目----------
描述

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).
基础算法 第八周 Flip Game_第1张图片

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.

输入

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

输出

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).

基础算法 第八周 Flip Game_第2张图片

--------------以下是本人解法-------------

#include
#include
#include
using namespace std;

int Puzzle[6][6] = { 0 }, FlipBlack[6][6] = { 0 }, FlipWhite[6][6] = {0};
int MinStep = 10000;

bool AllBlack(int row)
{
	for(int i=row;i<5;i++)
		for (int j = 1; j< 5; j++)
			if ((Puzzle[i][j] + FlipWhite[i][j] + FlipWhite[i][j - 1] + FlipWhite[i][j + 1] + FlipWhite[i - 1][j]) % 2 == 0)
				return false;
	return true;
}
bool AllWhite(int row)
{
	for (int i = row; i<5; i++)
		for (int j = 1; j < 5; j++)
			if ((Puzzle[i][j] + FlipBlack[i][j] + FlipBlack[i][j - 1] + FlipBlack[i][j + 1] + FlipBlack[i - 1][j]) % 2 == 1)
				return false;
	return true;
}

void fun(int StepForBlk,int StepForWt)
{
	for (int i = 1; i < 4; i++)
		for (int j = 1; j < 5; j++)
		{
			FlipBlack[i + 1][j] = (Puzzle[i][j] + FlipBlack[i][j] + FlipBlack[i][j - 1] + FlipBlack[i][j + 1] + FlipBlack[i - 1][j]) % 2;
			if (FlipBlack[i + 1][j] == 1) StepForBlk++;
			FlipWhite[i + 1][j] = (Puzzle[i][j] + FlipWhite[i][j] + FlipWhite[i][j - 1] + FlipWhite[i][j + 1] + FlipWhite[i - 1][j] + 1) % 2;
			if (FlipWhite[i + 1][j] == 1) StepForWt++;
		}
	if (AllBlack(4))
		if (StepForWt < MinStep)
			MinStep = StepForWt;
	if (AllWhite(4))
		if (StepForBlk < MinStep)
			MinStep = StepForBlk;
}

int main()
{
	for (int i = 1; i < 5; i++)
		for(int j=1;j<5;j++)
		{
			char tmp;
			cin >> tmp;
			if (tmp == 'b') Puzzle[i][j]=1;
		}
	if (AllBlack(1) || AllWhite(1)) cout << "0" << endl;
	else
	{
		for (int i = 0; i < 16; i++)
		{
			bitset<4>firstRow(i);
			for (int j = 1; j < 5; j++)
			{
				FlipBlack[1][j] = firstRow[j-1];
				FlipWhite[1][j] = firstRow[j-1];
			}
			int Count = firstRow.count();
			fun(Count, Count);
			memset(FlipBlack, 0, sizeof(FlipBlack));
			memset(FlipWhite, 0, sizeof(FlipWhite));
		}
		if (MinStep == 10000) cout << "Impossible" << endl;
		else cout << MinStep << endl;
	}
	return 0;
}



你可能感兴趣的:(学习,C++)