POJ 1753 Flip Game 枚举

本文介绍了一种通过枚举方法解决Flipgame问题的算法。该方法适用于4x4棋盘,目标是在最少的轮次内将所有棋子翻转至同一面。文章详细解释了实现过程,并提供了一份通过验证的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25810 Accepted: 11149

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
这道题看网上很多报告都写了BFS的代码,但是,作为ACM新手,我还是用了一下枚举的方法,简单来说,开始的时候先直接判断当前是否为纯色,如果是,直接输出0,如果不是,进行从1到16循环,套用全组合模板求出16取i个数的所有可能,在每次全组合得到结果的位置带入翻转函数对棋盘进行翻转,并且判断棋盘翻转后是否变为纯色,如果是,跳出全组合函数,如果不是,继续循环,直到全组合所有可能都循环结束的时候依然无法满足纯色,跳出全组合函数返回-1,之后,如果全组合函数返回非-1的值,那么就输出当前循环的的i,并且结束程序,如果返回-1,那么继续循环,当程序循环到16时依然返回-1,那么就是无法满足,输出“Impossible”,程序结束,另外还要说,如果一个棋子被翻转超过两次,是没有意义的,所以其实最多也就循环到16次,而且每个点至多可以使用一次。
 
下面是AC的代码:
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
char a[4][4],b[4][4];
int x[16],y[16];
struct p
{
	int x;
	int y;
}pa[20];
int judge()
{
	int i,j;
	char c;
	c=b[0][0];
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			if(b[i][j]!=c)
				return 0;
	return 1;
}
void change(int x,int y)
{
	if(b[x][y]=='w')
		b[x][y]='b';
	else
		b[x][y]='w';
	if(b[x-1][y]=='w'&&x-1>=0)
		b[x-1][y]='b';
	else if(x-1>=0)
		b[x-1][y]='w';
	if(b[x+1][y]=='w'&&x+1<=3)
		b[x+1][y]='b';
	else if(x+1<=3)
		b[x+1][y]='w';
	if(b[x][y-1]=='w'&&y-1>=0)
		b[x][y-1]='b';
	else if(y-1>=0)
		b[x][y-1]='w';
	if(b[x][y+1]=='w'&&y+1<=3)
		b[x][y+1]='b';
	else if(y+1<=3)
		b[x][y+1]='w';
}
int createfab(int m,int n) //全组合
{
     int i,j,lcount,wa[20];
     for(i=1;i<=n;i++)     
		 wa[i]=i;
     wa[n+1]=m+1;
	 for(i=1;i<=n;i++)
		 change(pa[wa[i]].x,pa[wa[i]].y);
	 if(judge()==1)
		 return n;
	 for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			b[i][j]=a[i][j];
     lcount=1;
     while(wa[1]<m-n+1) {
          for(i=n;i>0;i--) {
               if(wa[i]<wa[i+1]-1) {
                    wa[i]++;
                    for(j=i;j<n;j++)     
						wa[j+1]=wa[j]+1;
						 for(i=1;i<=n;i++)
		                     change(pa[wa[i]].x,pa[wa[i]].y);
	                     if(judge()==1)
		                     return n;
						 for(i=0;i<4;i++)
		                     for(j=0;j<4;j++)
			                     b[i][j]=a[i][j];
                    lcount++;
                    break;
               }
          }
     }
	 return -1;
}
int main()
{
	int i,j,k;
	k=1;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			pa[k].x=i;
			pa[k].y=j;
			k++;
		}
	for(i=0;i<4;i++)
	{
		scanf("%s",a[i]);
	}
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			b[i][j]=a[i][j];
	if(judge()==1)
	{
		printf("0\n");
		return 0;
	}
	for(i=1;i<=16;i++)
	{
		if(createfab(16,i)!=-1)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	printf("Impossible\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值