Flip Game(POJ 1753)

来源:互联网 发布:项目数据分析报告范文 编辑:程序博客网 时间:2024/04/30 05:15

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;}
0 0
原创粉丝点击