Codeforces 515D - Drazil and Tiles (拓扑排序)

本文介绍了一种利用二维拓扑排序的方法来判断一个图是否能被1*2或2*1的方块唯一覆盖,并提供了详细的算法步骤和代码实现。通过将所有度数为1的点加入队列,进行匹配覆盖并更新度数,最终验证是否存在无法匹配的情况或未被访问的点,以此判断解的存在性和唯一性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意

给一个图,问能不能用1*2或者2*1的方块覆盖,并且只有一种覆盖方法。

思路

二维的拓扑排序。

引用一下Peter的解释(http://blog.youkuaiyun.com/uestc_peterpan/article/details/43875195)

二维拓扑排序。

  1. 将所有度数为1的点加入队列;
  2. 把队列里的一个点v1 和 与v1相邻的一个点v2 用1×2或2×1方格覆盖,再用v2更新周围点的度数,若更新后点度数为1,则加入队列;
  3. 若2中存在一个v1无法与一个v2匹配,或者2结束后,存在一个点没有访问过,则无解或有多解;否则输出解即可。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 2e3 + 10;
const int MOD = 1e9 + 7;
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
const int hash_size = 4e5 + 10;
int cases = 0;
typedef pair<int, int> pii;

char mp[MAXN][MAXN];
int row, col, deg[MAXN][MAXN];
queue<pii> Q;

void AdjustDegree(int x, int y)
{
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dir[i][0], yy = y + dir[i][1];
        deg[xx][yy]--;
        if (deg[xx][yy] == 1) Q.push({xx, yy});
    }
}

void GetDegree()
{
    for (int i = 1; i <= row; i++)
        for (int j = 1; j <= col; j++)
        {
            if (mp[i][j] != '.') continue;
            if (i > 1 && mp[i-1][j] == '.') deg[i][j]++;
            if (j > 1 && mp[i][j-1] == '.') deg[i][j]++;
            if (i < row && mp[i+1][j] == '.') deg[i][j]++;
            if (j < col && mp[i][j+1] == '.') deg[i][j]++;
            if (deg[i][j] == 1) Q.push({i, j});
        }
}

void Toposort()
{
    while (!Q.empty())
    {
        pii cur = Q.front(); Q.pop();
        int x = cur.X, y = cur.Y;
        if (mp[x-1][y] == '.')
        {
            mp[x][y] = 'v';
            mp[x-1][y] = '^';
            AdjustDegree(x-1, y);
        }
        else if (mp[x+1][y] == '.')
        {
            mp[x][y] = '^';
            mp[x+1][y] = 'v';
            AdjustDegree(x+1, y);
        }
        else if (mp[x][y+1] == '.')
        {
            mp[x][y] = '<';
            mp[x][y+1] = '>';
            AdjustDegree(x, y+1);
        }
        else if (mp[x][y-1] == '.')
        {
            mp[x][y] = '>';
            mp[x][y-1] = '<';
            AdjustDegree(x, y-1);
        }
        else
        {
            if (mp[x][y] == '.')
            {
                printf("Not unique\n");
                return;
            }
        }
    }
    for (int i = 1; i <= row; i++)
        for (int j = 1; j <= col; j++)
            if (mp[i][j] == '.') { puts("Not unique"); return; }
    for (int i = 1; i <= row; i++)
        printf("%s\n", mp[i]+1);
}

int main()
{
    //ROP;
    scanf("%d%d", &row, &col);
    for (int i = 1; i <= row; i++)
        scanf("%s", mp[i]+1);
    GetDegree();
    Toposort();
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值