Codeforces 142B(二分染色、搜索)

要点

  • 会发现本质上棋盘分成了若干个独立集,本集合内的点放不放棋子并不影响其他集合内的
  • 集合的划分方式就是满棋盘跳马步直到全跳过了,然后每个集合就分成两队,我们选人多的那队放棋子,人少那队当禁区
const int maxn = 1e3 + 5;
const int nx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int ny[] = {-1, 1, -2, 2, -2, 2, -1, 1};

int n, m;
bool grid[maxn][maxn];
int ans, cnt[2];

void dfs(int i, int j, int c) {
    cnt[c]++;
    grid[i][j] = 1;
    rep(k, 0, 7) {
        int x = i + nx[k], y = j + ny[k];
        if (x < 1 || x > n || y < 1 || y > m || grid[x][y]) continue;
        dfs(x, y, 1 - c);
    }
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    rep(i, 1, n)
        rep(j, 1, m)
            if (!grid[i][j]) {
                cnt[0] = cnt[1] = 0;
                dfs(i, j, 0);
                ans += max(cnt[0], cnt[1]);
            }
    cout << ans << '\n';
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AlphaWA/p/11123857.html