POJ 1753 状压(开关问题)

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 38306 Accepted: 16664

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

Source


题目大意:

就是翻格子,然后每翻一个上下左右都会被翻一次。


思路:

简单的状态压缩题目(开关问题)。

假设,我们要把所有的题目都给翻成白色,那么从第二行开始,如果每次上面这一行是黑色,那么就统计一下黑色周围四个的被翻的次数,然后再用if考虑一下颜色,看看是否flip就好了


#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      

using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;

int atlas1[10][10];//1是白色
int atlas2[10][10];//1是黑色
int flip1[10][10];
int flip2[10][10];
int dx[] = {0 ,0, 1, -1};
int dy[] = {1, -1, 0, 0};

void init(){
    char ch[1][10];
    for (int i = 0; i < 4; i++){
        scanf("%s", ch[0]);
        for (int j = 0; j < 4; j++){
            if (ch[0][j] == 'b'){
                atlas1[i][j] = 1;
                atlas2[i][j] = 0;
            }
            else {
                atlas1[i][j] = 0;
                atlas2[i][j] = 1;
            }
        }
    }
}

int work1(){
    for (int i = 1; i < 4; i++){
        for (int j = 0; j < 4;j ++){
            int cnt = flip1[i - 1][j];
            for (int k = 0; k < 4; k++){
                int x = i - 1 + dx[k];
                int y = j + dy[k];
                if (x < 0 || x >= 4 || y < 0 || y >= 4) continue;
                cnt += flip1[x][y];
            }
            cnt = cnt % 2;
            if (cnt == 1 && atlas1[i - 1][j] == 0 || (cnt == 0 && atlas1[i - 1][j] == 1)){
                flip1[i][j] = 1;
            }
        }
    }
    for (int j = 0; j < 4; j++){
        int cnt = flip1[3][j];
        for (int k = 0; k < 4; k++){
            int x = 3 + dx[k];
            int y = j + dy[k];
            if (x < 0 || x >= 4 || y < 0 || y >= 4) continue;
            cnt += flip1[x][y];
        }
        cnt %= 2;
        if (cnt == 1 && atlas1[3][j] == 0 || (cnt == 0 && atlas1[3][j] == 1)){
            return -1;
        }
    }
    int cnt = 0;
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            if (flip1[i][j]){
                cnt += flip1[i][j];
            }
        }
    }
    return cnt;
}

int work2(){
    for (int i = 1; i < 4; i++){
        for (int j = 0; j < 4;j ++){
            int cnt = flip2[i - 1][j];
            for (int k = 0; k < 4; k++){
                int x = i - 1 + dx[k];
                int y = j + dy[k];
                if (x < 0 || x >= 4 || y < 0 || y >= 4) continue;
                cnt += flip2[x][y];
            }
            cnt = cnt % 2;
            if (cnt == 1 && atlas2[i - 1][j] == 0 || (cnt == 0 && atlas2[i - 1][j] == 1)){
                flip2[i][j] = 1;
            }
        }
    }
    for (int j = 0; j < 4; j++){
        int cnt = flip2[3][j];
        for (int k = 0; k < 4; k++){
            int x = 3 + dx[k];
            int y = j + dy[k];
            if (x < 0 || x >= 4 || y < 0 || y >= 4) continue;
            cnt += flip2[x][y];
        }
        cnt %= 2;
        if (cnt == 1 && atlas2[3][j] == 0 || (cnt == 0 && atlas2[3][j] == 1)){
            return -1;
        }
    }
    int cnt = 0;
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            if (flip2[i][j]){
                cnt += flip2[i][j];
            }
        }
    }
    return cnt;
}

void solve(){
    int res1 = inf;
    int res2 = inf;
    for (int i = 0; i < 1 << 4; i++){
        memset(flip1, 0, sizeof(flip1));
        memset(flip2, 0, sizeof(flip2));
        for (int j = 0; j < 4; j++){
            if ((i >> j) & 1) {
                flip1[0][j] = 1;
                flip2[0][j] = 1;
            }
        }
        int tmp = work1();
        if (tmp < res1 && tmp >= 0) res1 = tmp;
        tmp = work2();
        if (tmp < res2 && tmp >= 0) res2 = tmp;
    }
    if (res1 == inf && res2 == inf){
        printf("Impossible\n");
        return ;
    }
    printf("%d\n", min(res1, res2));
}

int main(){
    init();
    solve();
    return 0;
}
     
     
    
    
   
   




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值