A - Flip Game

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

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

题目意思:

给了一个4v4的棋盘,里面有黑子和白子,2种棋子,当在4v4的棋盘上,随便翻一个会导致当前位置与相邻2边与上下2边的棋子颜色改变,每翻一次就相当于所走了一步,题目要求输出最少步数(完成棋盘全为黑子或白子任务),如果无法完成的话,就输出Impossible。

思路分析:

一开始真的被这个题目坑到,觉得这是一个运用搜索算法的题,但却不知道怎么去寻找最少步数(相当于不知道子那个地方开始翻,才能完成最少步数),所以在这个地方考虑了很久,结果发现自己考虑多,直接搜啊,直接从棋盘的每一格开始翻,直到达到任务,记录步数,然后每次完成任务的步数,与前几次进行对比,找到最小步数即可,如果没有步数,说明永远无法完成任务。

代码块:

#include<stdio.h>
char c1[4][4];//记录棋子
int m=0,n=0,count=0,min=17;
void lemon()//记录棋子开始状态
{
	int a,b;
	for(a=0;a<4;a++)
	{
		for(b=0;b<4;b++)
		{
			scanf("%c",&c1[a][b]);
			if(b==3 && a!=3)
			{
				getchar();//这一步如果没有的话,会导致数组越界
			}
			if(c1[a][b]=='b')//记录黑子
			{
				m++;
			}
			else if(c1[a][b]=='w')//记录白字
			n++;
		}
	}
}
void lemon3(int x,int y)//开始翻转棋子
{
	if(x>=4)//如果x>=4说明已经翻完全部,直接退出
	{
		return;
	}
	if(c1[x][y]=='b')//当前位置翻
	{
		c1[x][y]='w';
		n++;
		m--;
	}
	else
	{
	c1[x][y]='b';
	n--;
	m++;
}
	if(x-1>=0)//向上翻
	{
		if(c1[x-1][y]=='b')
		{
			c1[x-1][y]='w';
			n++;
			m--;
			
		}
		else
		{
		c1[x-1][y]='b';
		n--;
		m++;
	}
	}
	if(x+1<=3)//向下翻
	{
		if(c1[x+1][y]=='b')
		{
			c1[x+1][y]='w';
			n++;
			m--;
		}
		else
		{
		c1[x+1][y]='b';
		n--;
		m++;
	}
	}
	if(y-1>=0)//向左翻
	{
		if(c1[x][y-1]=='b')
		{
			c1[x][y-1]='w';
			n++;
			m--;
		}
		else
		{
		c1[x][y-1]='b';
		n--;
		m++;
	}
	}
	if(y+1<=3)//向右翻
	{
		if(c1[x][y+1]=='b')
		{
			c1[x][y+1]='w';
			n++;
			m--;
		}
		else
		{
		c1[x][y+1]='b';
		n--;
		m++;
	}
	}
}
void lemon2(int x,int y,int num)
{
//
	int x1=x+(y+1)/4;
	int y1=(y+1)%4;
	//这2步是为了在4v4方格中 一个一个去翻的改变条件
	if(x>=4)
	{
		if(m==16 || n==16)//如果达到条件,记录步数
		{
		if(num<min)//比较步数
		{
			min=num;
			count=1;
		}
	}
		return;
	}
	//
	lemon3(x,y);
	lemon2(x1,y1,num+1);
	//当下一步不成功,就退回上一步
	lemon3(x,y);
	lemon2(x1,y1,num);
	//
}
void Lemonprintf()
{
	if(!count)//如果没有步数
	{
		printf("Impossible");
	}
	else
	printf("%d",min);
}
int main()
{
	lemon();//记录开始时候棋子状态
	lemon2(0,0,0);//进入搜索
	Lemonprintf();//输出步数
}
发布了49 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xiaosuC/article/details/104077924