Flip Game(模拟 枚举)

Flip Game

https://ac.nowcoder.com/acm/problem/106350


题目描述

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.

输入描述:

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).
示例1

输入

bwwb
bbwb
bwwb
bwww

输出

4

题意

翻转一个棋子,自己和他四周的棋子都会翻转。问给定棋盘,用最小翻转次数使得棋局全部统一。

思路

首先,每个棋子最多翻转一次,因为翻转两次抵消,翻转三次和一次效果一样。现在就变成了每个棋子是翻还是不翻的问题了,但整个棋局太大了,枚举整个棋局是216 = 65536,好像还是可以接受的。但还会有更加简便的办法,就是只枚举第一行或者第一列,那么这一***定下来了,如果这行或者这列还有需要翻转的,就由下一行或者下一列对应的棋子翻转,依此类推,最后只需要判断最后一行或者最后一列是否符合要求就行了。

代码

//Flip Game(模拟 枚举) 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

char a[4][4] , b[4][4];

bool judge(char x)
{
	for(int i = 0 ; i < 4 ; i++)
	{
		for(int j = 0 ; j < 4 ; j++)
		{
			if(a[i][j] != x)
				return false;
		}
	}
	return true;
}

void change(int x , int y)
{
	int dx[5][2] = {{0,0} , {1,0} , {-1,0} , {0,1} , {0,-1}};
	
	for(int i = 0 ; i < 5 ; i++)
	{
		int go_x = x + dx[i][0];
		int go_y = y + dx[i][1];
		
		if(go_x < 0 || go_x >= 4)
			continue;
		if(go_y < 0 || go_y >= 4)
			continue;
		
		if(a[go_x][go_y] == 'b')
			a[go_x][go_y] = 'w';
		else
			a[go_x][go_y] = 'b';
	}
	
//	cout<<x<<" "<<y<<endl;
//	for(int i = 0 ; i < 4 ; i++)
//	{
//		for(int j = 0 ; j < 4 ; j++)
//			cout<<a[i][j];
//		cout<<endl;
//	}
}

int work(char ch)
{
	int cnt = 0 , min_cnt = 17;
 	for(int x = 0 ; x < 16 ; x++)
 	{	
 		cnt = 0;
		 
		//恢复 
		for(int i = 0 ; i < 4 ; i++)
		{
			for(int j = 0 ; j < 4 ; j++)
				a[i][j] = b[i][j];
		}
		
		//首行 
		for(int i = 0 ; i < 4 ; i++)
		{
			int tag = (x >> i) & 1;
			
//			cout<<tag;
			if(tag == 1)
			{
				cnt++;
				change(0 , i);
			}
		} 
//		cout<<endl;
		
		//依次
		for(int i = 1 ; i < 4 ; i++)
		{
			for(int j = 0 ; j < 4 ; j++)
			{
				if(a[i - 1][j] == ch)
				{
					cnt++;
					change(i , j);	
				}	
			}	
		} 
	 	
	 	if((judge('w') && ch == 'b') || (judge('b') && ch == 'w'))
	 		min_cnt = min(min_cnt , cnt);
	}
	
	return min_cnt;
}

int main()
{
 	for(int i = 0 ; i < 4 ; i++)
 		scanf("%s" , a[i]);
 		
 	for(int i = 0 ; i < 4 ; i++)
	{
		for(int j = 0 ; j < 4 ; j++)
			b[i][j] = a[i][j];
	}
	
//	for(int i = 0 ; i < 4 ; i++)
//	{
//		for(int j = 0 ; j < 4 ; j++)
//			cout<<a[i][j];
//		cout<<endl;
//	}
	
	int ans;
	if(judge('w') || judge('b'))
 		ans = 0;
	
	if(ans)
 		ans = min(work('w') , work('b'));
 	if(ans == 17)
 		printf("Impossible\n");
 	else
 		printf("%d\n" , ans);
 	
	return 0; 
}

入门课第一节习题题解

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 17:16
科大讯飞 算法工程师 28.0k*14.0, 百分之三十是绩效,惯例只发0.9
点赞 评论 收藏
分享
头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
评论
3
收藏
分享
牛客网
牛客企业服务