Flip Game POJ - 1753 (C枚举)

该博客讨论了一种4x4棋盘翻转游戏,玩家需翻转棋子以达到所有棋子颜色统一的目标。每回合可翻转3到5个相邻棋子,博主提供了一个C++程序来寻找达成目标所需的最小翻转次数。如果无法达到目标状态,则输出'Impossible'。

题目

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

题目概括

输入4行,每行4个字母,“b”表示黑色面朝上,“w”表示白色面朝上。

每一轮你翻转3到5个棋子,从而将其上侧的颜色从黑色变为白色,反之亦然。根据以下规则,每轮选择要翻转的棋子:

从16个中任选一个。

将所选的棋子以及所有相邻的棋子翻转到所选棋子的左侧、右侧、顶部和底部(如果有)。

游戏目标为将场上所有16颗子都为黑或白色。

将实现游戏目标所需的最小轮数输出。如果目标最初实现了,那么输出0。如果不可能实现目标,那么输出“Impossible”。

代码

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
using namespace std;
int main() 
{
	char b;
	int i,j,a[4][4],c[4][4],now[4][4],temp=16,p;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			scanf("%c", &b);
			if (b == 'b') {
				a[i][j] = 1;//将输入的b或w分别转换成1、0存入a中
			}
			else if (b == 'w') {
				a[i][j] = 0; 
			}
		}getchar();//取出每一行输入结束的回车,非常重要!
	}
	for(c[0][0]=0;c[0][0]<2;c[0][0]++)//枚举每一种情况
	for (c[0][1] = 0; c[0][1] < 2; c[0][1]++)
	for (c[0][2] = 0; c[0][2] < 2; c[0][2]++)
	for (c[0][3] = 0; c[0][3] < 2; c[0][3]++)
	for (c[1][0] = 0; c[1][0] < 2; c[1][0]++)
	for (c[1][1] = 0; c[1][1] < 2; c[1][1]++)
	for (c[1][2] = 0; c[1][2] < 2; c[1][2]++)
	for (c[1][3] = 0; c[1][3] < 2; c[1][3]++)
	for (c[2][0] = 0; c[2][0] < 2; c[2][0]++)
	for (c[2][1] = 0; c[2][1] < 2; c[2][1]++)
	for (c[2][2] = 0; c[2][2] < 2; c[2][2]++)
	for (c[2][3] = 0; c[2][3] < 2; c[2][3]++)
	for (c[3][0] = 0; c[3][0] < 2; c[3][0]++)
	for (c[3][1] = 0; c[3][1] < 2; c[3][1]++)
	for (c[3][2] = 0; c[3][2] < 2; c[3][2]++)
	for (c[3][3] = 0; c[3][3] < 2; c[3][3]++)
	{
		now[0][0] = (a[0][0]+c[0][0] + c[0][1] + c[1][0]) % 2;//now[0][0]表示现在[0][0]的颜色
		now[1][0] = (a[1][0] +c[1][0] + c[0][0] + c[1][1] + c[2][0]) % 2;
		now[2][0] = (a[2][0] + c[2][0] + c[1][0] + c[2][1] + c[3][0]) % 2;
		now[3][0] = (a[3][0] + c[3][0] + c[2][0] + c[3][1]) % 2;
		now[0][1] = (a[0][1] + c[0][0] + c[0][1] + c[0][2] + c[1][1]) % 2;
		now[1][1] = (a[1][1] + c[1][0] + c[1][1] + c[1][2] + c[2][1]+c[0][1]) % 2;
		now[2][1] = (a[2][1] + c[2][0] + c[2][1] + c[2][2] + c[3][1] + c[1][1]) % 2;
		now[3][1] = (a[3][1] + c[3][0] + c[3][1] + c[3][2] + c[2][1]) % 2;
		now[0][2] = (a[0][2] + c[0][1] + c[0][2] + c[0][3] + c[1][2]) % 2;
		now[1][2] = (a[1][2] + c[1][1] + c[1][2] + c[1][3] + c[2][2] + c[0][2]) % 2;
		now[2][2] = (a[2][2] + c[2][1] + c[2][2] + c[2][3] + c[3][2] + c[1][2]) % 2;
		now[3][2] = (a[3][2] + c[3][1] + c[3][2] + c[3][3] + c[2][2]) % 2;
		now[0][3] = (a[0][3] + c[0][2] + c[0][3] + c[1][3]) % 2;
		now[1][3] = (a[1][3] + c[1][2] + c[1][3] + c[2][3] + c[0][3]) % 2;
		now[2][3] = (a[2][3] + c[2][2] + c[2][3] + c[3][3] + c[1][3]) % 2;
		now[3][3] = (a[3][3] + c[3][2] + c[3][3] + c[2][3]) % 2;
		if ((now[0][0] + now[0][1] + now[0][2] + now[0][3] + now[1][0] + now[1][1] + now[1][2] + now[1][3] + now[2][0] 
			+ now[2][1] + now[2][2] + now[2][3] + now[3][0] + now[3][1] + now[3][2] + now[3][3]) % 16 == 0)//如果和为0或者16则为通关
		{
			p = c[0][0] + c[0][1] + c[0][2] + c[0][3] + c[1][0] + c[1][1] + c[1][2] + c[1][3] + c[2][0] + c[2][1] + c[2][2] + c[2][3] + c[3][0] + c[3][1] + c[3][2] + c[3][3];
			if (p < temp)temp = p;//把成功通过的最小记录保存到temp
		}
	}
	if(temp==16)printf("Impossible");
	else printf("%d", temp);
}

思路

每次翻面都将改变颜色,连续翻两次相当于没翻,于是就把输入的w和b转换成0,1。后续进行枚举,每个棋子不会翻面超过一次,每次枚举都将判断是否均为黑面或者白面,当now的和0或者16时,说明均为黑面或白面。将所得此方法翻面次数与历史最小翻面次数(temp)进行比较,如果temp==16每个棋子都翻面,说明temp没被更改,无解。如果更改过temp的值,则输出最小翻面次数(包括0)。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值