zoukankan      html  css  js  c++  java
  • 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 << '
    ';
    	return 0;
    }
    
  • 相关阅读:
    JS学习之旅2
    JS学习之旅1
    Stack 栈
    Linked List 链表
    Array 数组
    时间/空间复杂度
    What/Why/How
    Https 握手过程
    JS跨域解决方案
    JS 的内存管理-GC
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/11123857.html
Copyright © 2011-2022 走看看