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

题目链接

这道题目用dfs直接暴力枚举搜索所有情况然后判断输出最小步数即可。

AC代码:

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const double eps = 1e-5;
const double e = 2.718281828459;

int ans = INF;
bool suc_flag = 0;
char input_maze[5][5];
bool maze[5][5];

bool suc_judge() {
	for (int i = 1; i < 5; ++i) {
		for (int j = 1; j < 5; ++j) {
			if (maze[i][j] != maze[1][1]) {
				return 0;
			}
		}
	}
	return 1;
}

void change(int x, int y) {
	for (int i = -1; i <= 1; ++i) {
		for (int j = -1; j <= 1; ++j) {
			if (abs(i) != abs(j) || (i == 0 && j == 0)) {
				int nx = x + i, ny = y + j;
				if (nx >= 0 && nx <= 4 && ny >= 0 && ny <= 4) {
					maze[nx][ny] = !maze[nx][ny];
				}
			}
		}
	}
}

void dfs(int x, int y, int step) {
	if (suc_judge()) {
		if (step < ans) {
			ans = step;
		}
		suc_flag = 1;
		return;
	}
	if (x > 4 || y > 4) {
		return;
	}
	change(x, y);
	if (y == 4) {
		dfs(x + 1, 1, step + 1);
		change(x, y);
		dfs(x + 1, 1, step); 
	}
	else {
		dfs(x, y + 1, step + 1);
		change(x, y);
		dfs(x, y + 1, step);
	}
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
	for (int i = 1; i < 5; ++i) {
		for (int j = 1; j < 5; ++j) {
			cin >> input_maze[i][j];
			maze[i][j] = input_maze[i][j] == 'b' ? 1 : 0;
		}
	}
	dfs(1, 1, 0);
	if (suc_flag) {
		cout << ans;
	}
	else {
		cout << "Impossible";
	}
    return 0;
}