POJ1753 - Flip Game

Flip Game

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 64189 Accepted: 26478

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

POJ1753 - Flip Game

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

Source

Northeastern Europe 2000

Approach:

Since I found this question under the tag of "permutation", it should be solvable with simple nested loops.

I tried to go through 2^16 possible ways.

However, my codes didn't work as intended...

Another approach is DFS, but I forgot the right way to implement DFS(tut)

After a hard time, I read some hints from other blogs.

To write a DFS, we need few important elements.

Firstly, we need a ending situation. When some condition is met(when bool check() returns true), we can terminate the searching process.

Also, we need to update while searching and change it to the previous status after searching.

such as:

flip(this block);
search(next block);
flip(this block);  // flip this block again so it can go back to its original state
search(next block);
// Therefore, we can include all possible sitautions: we flip this block, or not flip this block

We have to keep in mind that this can be impossible.

If our variable ans is never updated and remains the value of INF, then we can know that this situation is impossible and we can output "Impossible".

I will say, this problem is a bit tough for me.(maybe that's because I skipped training for half a month already?)

Solution:

(Implemented in C++, used DFS)

// POJ1753 - Flip Game
// zqsml on 3 Mar 2021
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int INF = 0x3f3f3f3f;

int ans = INF;
char board[5][5];
int grid[4][4];
int dx[] = {1 , -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool check(){
	for(int i = 0; i < 4; i ++ )
		for(int j = 0; j < 4; j ++ )
			if(grid[i][j] != grid[0][0])
				return false;
	return true;
}

void flip(int row, int col){
	grid[row][col] = !grid[row][col];
	for(int i = 0; i < 4; i ++ ){
		int nx = row + dx[i];
		int ny = col + dy[i];
		if(nx >= 0 && nx < 4 && ny >= 0 && ny < 4)
			grid[nx][ny] = !grid[nx][ny];
	}
}

void dfs(int row, int col, int count){
	if(check()){
		ans = min(ans, count);
		return ;
	}
	if(count > ans) return;

	int x = row * 4 + col;

	if(x == 16) return;

	x ++ ;

	int nx = x / 4;
	int ny = x % 4;

	flip(row, col);
	dfs(nx, ny, count + 1);

	flip(row, col);
	dfs(nx, ny, count);
}

int main(){
	for(int i = 0; i < 4; i ++ ) scanf("%s", board[i]);

	for(int i = 0; i < 4; i ++ )
		for(int j = 0; j < 4; j ++ )
			if(board[i][j] == 'b') grid[i][j] = 1;

	dfs(0, 0, 0);

	if(ans == INF) puts("Impossible");
	else printf("%d\n", ans);
	
	return 0;
}
上一篇:Java NIO学习笔记二(Buffer的flip()方法详解)


下一篇:【opencv-python】 cv2.flip(...)图像翻转