TLE 81 分求助

P3355 骑士共存问题

plamya @ 2024-03-28 10:58:13

#include <bits/stdc++.h>

using namespace std;

const int K = 205, N = K * K, M = N << 2;
const int D[][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};

int n, m, h[N], e[M], ne[M], cnt, a, b, t[N], v, y, r;
bool g[K][K], st[N];

void add(int x, int y)
{
    e[ne[cnt] = h[x], h[x] = cnt++] = y;
}

bool dfs(int x)
{
    for (int i = h[x], y; ~i; i = ne[i])
        if (!st[y = e[i]])
        {
            st[y] = 1;
            if (!~t[y] || dfs(t[y]))
            {
                t[y] = x;
                return 1;
            }
        }
    return 0;
}

int main()
{
    cin >> n >> m, memset(h, -1, sizeof h), memset(t, -1, sizeof t);
    for (int i = 0; i < m; i++) cin >> a >> b, g[a - 1][b - 1] = 1;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (!g[i][j])
            {
                if (i + j & 1) continue;
                for (int k = 0; k < 8; k++)
                {
                    a = i + D[k][0], b = j + D[k][1];
                    if (0 <= a && a < n && 0 <= b && b < n && !g[a][b]) add(i * n + j, a * n + b);
                }
            }

    r = n * n - m;
    for (int i = 0; i < n * n; memset(st, 0, sizeof st), r -= dfs(i++));

    cout << r;

    return 0;
}

by Liuboom @ 2024-06-26 16:55:37

好像是建边顺序的问题,把


const int D[][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};

改成

int D[8][2] = {{-1, -2}, {-2, -1}, {2, -1}, {1, -2}, {-1, 2}, {-2, 1}, {2, 1}, {1, 2}};

就能过,具体说明参考 @ reclusive 大佬的讨论区 这里 (我也是被大佬捞上来的%%%)


by plamya @ 2024-07-03 11:51:33

@Liuboom 谢,此帖结


|