POJ1753 Flip Game

题目来源:http://poj.org/problem?id=1753

 

题目大意:给定一个4*4的黑白方阵,每次可使某一点及其上下左右相邻的四个点的颜色反转,问最小操作几次可使整个方阵变为全为黑色或全为白色。

 

状态压缩bfs。由于最多有2^16-1=65535种状态,只需使用一个vis数组来判重即可。

 

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>

#define ll long long
#define ull unsigned long long
#define BUG cout<<"*************************"<<endl
using namespace std;

const ll mod = 998244353;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10000;
const double eps = 1e-8;

int a[5][5];
bool vis[2000000];

int change() {
    int pos = 0;
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            if (a[i][j] == 1) {
                int c = (i - 1) * 4 + j;
                pos = (pos | (1 << (c - 1)));
            }
        }
    }
    return pos;
}

void back(int pos) {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            int c = (i - 1) * 4 + j;
            if (pos & (1 << (c - 1)))a[i][j] = 1;
            else a[i][j] = 0;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 4; j++) {
            char c;
            cin >> c;
            if (c == 'b')a[i][j] = 1;
            else a[i][j] = 0;
        }
    }
    int pos = change();
    vis[pos] = 1;
    queue<int> q, p;
    q.push(pos);
    int tot = 0;
    bool ok = 0;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (x == 0 || x == 65535) {
            ok = 1;
            break;
        }
        back(x);
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                a[i][j] = 1 - a[i][j];
                if (i - 1 >= 1)a[i - 1][j] = 1 - a[i - 1][j];
                if (i + 1 <= 4)a[i + 1][j] = 1 - a[i + 1][j];
                if (j - 1 >= 1)a[i][j - 1] = 1 - a[i][j - 1];
                if (j + 1 <= 4)a[i][j + 1] = 1 - a[i][j + 1];
                int e = change();
                if (vis[e] == 0) {
                    vis[e] = 1;
                    p.push(e);
                }
                a[i][j] = 1 - a[i][j];
                if (i - 1 >= 1)a[i - 1][j] = 1 - a[i - 1][j];
                if (i + 1 <= 4)a[i + 1][j] = 1 - a[i + 1][j];
                if (j - 1 >= 1)a[i][j - 1] = 1 - a[i][j - 1];
                if (j + 1 <= 4)a[i][j + 1] = 1 - a[i][j + 1];
            }
        }
        if (q.empty()) {
            tot++;
            swap(p, q);
        }
    }
    if (ok)cout << tot;
    else cout << "Impossible";
    return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值