codeforces 515D Drazil and Tiles

题意:
类似骨牌覆盖。在n*m的格子上有一些不能放的格子。
询问是否有唯一的方法用1x2的牌覆盖地图。
思路:
建图:如果两个空格相邻,则连一条边。并计算每个格的度数。
然后用类似删源点法的方法不断删去度为1的格子和它的邻居。
如果最后有剩下的格子可以证明边是可二着色的(必然偶环?)

#include<bits/stdc++.h>
using namespace std;

#define SPEED_UP iostream::sync_with_stdio(false);
#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define in_bound(l, r, i) (l)<=(i)&&(i)<(r)
#define pb push_back

typedef long long LL;

const int inf = INT_MAX/2;
const int Maxn = 2000;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int d[Maxn+5][Maxn+5], used[Maxn+5][Maxn+5], n, m;
char mm[Maxn+5][Maxn+5];

typedef pair<int, int> P;
void go(pair<P, P> & i) {
    P u = i.first, v = i.second;
    //printf("pair: (%d %d), (%d %d)\n", u.first, u.second, v.first, v.second);
    if (u.first == v.first) {
        if (u.second < v.second)
            mm[u.first][u.second] = '<', mm[v.first][v.second] = '>';
        else
            mm[u.first][u.second] = '>', mm[v.first][v.second] = '<';
    }
    else {
        if (u.first < v.first)
            mm[u.first][u.second] = '^', mm[v.first][v.second] = 'v';
        else
            mm[u.first][u.second] = 'v', mm[v.first][v.second] = '^';
    }
}

int solve() {
    queue<P> q;
    int nx, ny;

    memset(d, 0, sizeof(d));
    memset(used, 0, sizeof(used));
    rep(i, 0, n-1)
        rep(j, 0, m-1)
            if (mm[i][j] != '*') {
                rep(k, 0, 3) {
                    nx = i+dx[k], ny = j + dy[k];
                    if (in_bound(0, n, nx) && in_bound(0, m, ny) && mm[nx][ny] != '*')
                        d[nx][ny] += 1;
                }
            }
    // debug
    //rep(i, 0, n-1) {rep(j, 0, m-1) cout << d[i][j] << ' ';cout << endl;}

    rep(i, 0, n-1) rep(j, 0, m-1)
        if (mm[i][j] == '.') {
            if (d[i][j]>1) continue;
            if (!d[i][j]) return 0;
            q.push(make_pair(i, j));
        }
    vector<pair<P, P> > ans;
    while (!q.empty()) {
        P u = q.front();q.pop();
        if (used[u.first][u.second]) continue;
        // debug
        //printf("scan: %d, %d\n", u.first, u.second);
        P v;
        rep(k, 0, 3) {
            nx = u.first+dx[k], ny = u.second+dy[k];
            if (in_bound(0, n, nx) && in_bound(0, m, ny)
                && d[nx][ny]) {
                    v = make_pair(nx, ny);
                    break;
                }
        }
        used[u.first][u.second] = used[v.first][v.second] = 1;
        d[u.first][u.second] = d[v.first][v.second] = 0;
        ans.push_back(make_pair(u, v));

        // debug
        //printf("del: (%d, %d) (%d, %d)\n", u.first, u.second, v.first, v.second);

        rep(k, 0, 3) {
            nx = v.first+dx[k], ny = v.second+dy[k];
            if (in_bound(0, n, nx) && in_bound(0, m, ny)
                && d[nx][ny]) {
                    d[nx][ny] -= 1;
                    if (!d[nx][ny]) return 0;
                    if (d[nx][ny] == 1) {
                        //printf("enque: (%d %d)\n", nx, ny);
                        q.push(make_pair(nx, ny));
                    }
                }
        }
    }

    rep(i, 0, n-1) rep(j, 0, m-1) if (mm[i][j] == '.' && d[i][j]) return 0;

    int sz = ans.size();
    rep(i, 0, sz-1)
        go(ans[i]);
    rep(i, 0, n-1) cout << mm[i] << endl;
    return 1;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    //SPEED_UP
    cin >> n >> m;
    rep(i, 0, n-1) cin >> mm[i];

    if (!solve()) cout << "Not unique\n";

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值