POJ 1753 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:

Choose any one of the 16 pieces.
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).

POJ 1753 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).
示例1
输入
bwwb
bbwb
bwwb
bwww
输出
4

题意:
有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白),
当翻动一只棋子时,该棋子上下左右相邻的棋子也会同时翻面。
以b代表黑面,w代表白面,给出一个棋盘状态,
问从该棋盘状态开始,使棋盘变成全黑或全白,至少需要翻转多少步

解题思路
枚举每一种情况;每一种情况都要判断是否满足题目要求;如果有满足要求的,就找出满足要求并且改变点最少的那一个情况;每一种情况都不满足则输出“Impossible”;也可以使用BFS、DFS求解。

#include
#include
#include
using namespace std;
typedef long long ll;
char a[5][5];
bool all = 0;
int mm = 100;
void bian(int n, int m)
{
     
    for(int i = -1; i <= 1; i++)
        for(int j = -1; j <= 1; j++)
        {
     
            if(i * j == 1 || i * j == -1) continue;		//四个方向(上,下,左,右)和本身保留,其他方向都排除
            if(1 <= n + i && n + i <= 4 && 1 <= m + j && m + j <= 4)//判断是否在范围内
            {
     
                if(a[n + i][m + j] == 'b') a[n + i][m + j] = 'w';
                else a[n + i][m + j] = 'b';
            }
        }
}
bool pan()
{
     
    char c = a[1][1];
    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
            if(a[i][j] != c) return false;
    return true;
}
void meiju(int n, int m, int num)
{
     
    for(int i = 1; i <= 4; i++)
    {
     
        for(int j = 1; j <= 4; j++)
        {
     
			if(i < n || (i == n && j <= m)) continue;
			bian(i, j);            //改变
			if(pan() && num < mm)
			{
     
				mm = num;
				all = true;
			}
			meiju(i, j, num + 1);
			bian(i, j);           //还原
		}
	}
}

int main()
{
     
    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
            cin >> a[i][j];
    if(pan())         //判断一开始的字符是否满足要求
    {
     
		mm = 0;
		all = true;
    }
	else meiju(0, 0, 1);
    if(all) cout << mm << endl;//判断是否找到满足要求的情况
    else cout << "Impossible" << endl;
    return 0;
}

你可能感兴趣的:(牛客,枚举,dfs,动态规划,bfs,c++)