Flip Game(POJ 1753)

Emma1997發表於2016-12-01

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

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

這道題是深搜列舉加回溯。具體思路:由於把一個棋子翻兩次結果是一樣的,所以最多的深搜層數為16,這就是回溯條件。知道了最大深度之後,就可以用暴力搜尋列舉0是深度是否可行,如果不可行,就把層數加一層看可不可行,如果深度加為16都不可行的話,那麼就不可行。每一次一層列舉的時候都從第一個棋子翻到最後一個棋子,依次列舉。

思考:
1、要看出列舉的最大深度,也就是判斷哪兩種情況下列舉結果是一樣的
2、如果此路不通,一定要把這條路走的時候做的操作消除,然後繼續列舉
如dfs (row, col + 1, deep)這樣的同層節點。
3、這道題還可以用位運算做

#include <iostream>
#include <cstring>

using namespace std;

bool field[4][4];
bool flag;
int step;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

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

void flip(int row, int col)
{
    field[row][col] = !field[row][col];
    for (int i = 0; i < 4; i++) {
        int tempx = dir[i][0] + row;
        int tempy = dir[i][1] + col;
        if (tempx >= 0 && tempx < 4 && tempy >= 0 && tempy < 4) field[tempx][tempy] = !field[tempx][tempy];
    }
}

void dfs(int row, int col, int deep)
{
    if (deep == step) {
        if (OK ()) {
            flag = true;
        }
        return;
    }

    if (flag || row == 4) return;

    flip (row, col);
    if (col < 3)
        dfs (row, col + 1, deep + 1);
    else
        dfs (row + 1, 0, deep + 1);
    if (flag) return;

    flip (row, col);
    if (col < 3) {
        dfs (row, col + 1, deep);
    } else {
        dfs (row + 1, 0, deep);
    }
    return;
}

int main()
{
    memset (field, true, sizeof(field));
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            char c;
            cin >> c;
            if (c == 'b') field[i][j] = false;//bΪfalse, wΪtrue
        }
    }

    flag = false;
    for (int i = 0; i <= 16; i++) {
        step = i;
        dfs (0, 0, 0);
        if (flag) break;
    }

    if (flag) cout << step;
    else cout << "Impossible";
    return 0;
}

相關文章