Flip Game / POJ 1753

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

题意

一个4*4黑白棋盘,每次可以将一个格子和其周围的4个格子反转成相反的颜色,问:至少反转多少次,整个棋盘一种颜色。

题解

//方法一
dfs 暴力枚举
枚举每个格子翻或不翻,如果最后同色,看下操作次数,更新答案

//方法二⭐️
二进制枚举 & 状态压缩DP

代码

DFS

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5;

char mp[N][N];
int ret = N * N;
int dir[2][5] = {{0, 1, -1, 0, 0}, {0, 0, 0, 1, -1}};//5种方向

bool part(int x, int y){//判定有没有越界
    if(x >= 0 && x <= 3 && y >= 0 && y <= 3){
        return true;
    }
    return false;
}

bool judge(){
    char example = mp[0][0];
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(example != mp[i][j]){
                return false;
            }
        }
    }
    return true;
}

void change(int x, int y){
    for(int i = 0; i < 5; i++){//5个方向枚举一遍
        int dx = x + dir[0][i];
        int dy = y + dir[1][i];
        if(part(dx, dy)){//不越界 就翻转
            mp[dx][dy] = (mp[dx][dy] == 'b') ? 'w' : 'b';
        }
    }
    return ;
}

void dfs(int x, int y, int step){//行 列 步骤数
    if(judge()){//判定一下 是否都是统一字符
        ret = min(ret, step);//选最小的
        return ;//可以 就不需要继续枚举了
    }
    if(y >= 4){//如果列>=4 需要换行
        x++;
        y = 0;
    }
    if(x >= 4){//不存在第4行 最多到第3行(0~3)
        return ;
    }
    for(int i = 0; i < 2; i++){//2种选择 翻或不翻
        if(i){//翻转
            change(x, y);
            dfs(x, y+1, step+1);//下一个格子 步骤数+1
            change(x, y);//回溯一步(再翻转一遍即可回到原来状态)
        }
        else{//不反转
            dfs(x, y+1, step);
        }
        
    }
    return ;
}

int main(){
    for(int i = 0; i < 4; i++){
        scanf("%s", mp[i]);
    }
    dfs(0, 0, 0);//从第0行第0列开始 没有反转步骤为0
    if(ret != N * N){
        printf("%d\n", ret);
    }else{
        printf("Impossible\n");
    }
    return 0;
}
PS
所有下标都从0开始,从0到3

第一次的时候,有一次以为是1到4
第45和49行写成了
    if(y > 4){
        x++;
        y = 0;
    }
    if(x > 4){
        return ;
    }
结果就超时了

状压

#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5;

char mp[N][N];
int ret = N * N;

bool judge(){//判断是否都为同一字符
    char example = mp[0][0];
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(example != mp[i][j]){
                return false;
            }
        }
    }
    return true;
}

void change(int x, int y){
    //^ 相同为0 不同为1,1^1=0, 0^1=1, 可以起到翻转效果
    mp[x][y] ^= 1;
    if(x-1 >= 0)    mp[x-1][y] ^= 1;
    if(x+1 < 4) mp[x+1][y] ^= 1;
    if(y-1 >= 0)    mp[x][y-1] ^= 1;
    if(y+1 < 4) mp[x][y+1] ^= 1;
    return ;
}

int main(){
    for(int i = 0; i < 4; i++){
        scanf("%s", mp[i]);
    }
    for(int i = 0; i < 4; i++){//字符转换成数字表示
        for(int j = 0; j < 4; j++){
            if(mp[i][j] == 'b'){
                mp[i][j] = 1;
            }else{
                mp[i][j] = 0;
            }
        }
    }
    for(int mask = 0; mask < (1 << 16); mask++){//二进制枚举所有状态
        int used = 0;
        for(int i = 0; i < 16; i++){//枚举所有的点
            if(mask & (1 << i)){//如果第i为是 1
            //x = i / 4, y = i % 4 稍以举例即可明白
                change(i / 4, i % 4);//翻转
                used++;//步骤+1
            }
        }
        if(judge()){//判断一下
            ret = min(ret, used);//选择最小的
        }
        for(int i = 0; i < 16; i++){//回溯一步
            if(mask & (1 << i)){
                change(i / 4, i % 4);
            }
        }
    }
    if(ret != N * N){
        printf("%d\n", ret);
    }else{
        printf("Impossible\n");
    }
    return 0;
}
PS

错误代码(change()):

void change(int x, int y){
    for(int i = 0; i < 5; i++){//5个方向枚举一遍
        int dx = x + dir[0][i];
        int dy = y + dir[1][i];
        if(part(dx, dy)){//不越界 就翻转
            mp[dx][dy] = (mp[dx][dy] == 'b') ? 'w' : 'b';
        }
    }
    return ;
}
要用位运算,提高运行效率
如果还是直接用字符来运行,就会超时
上面的错误代码,就是因为直接用字符而导致的超时
posted @ 2020-02-29 11:25  LT-Y  阅读(146)  评论(0)    收藏  举报