HDU 4775 Infinite Go(並查集,類比)

來源:互聯網
上載者:User

標籤:style   http   color   os   for   io   

HDU 4775 Infinite Go

題目連結

題意:圍棋,兩人輪流走,如果有一鏈被圍死,就會被吃掉,問下完後最後黑色和白色各剩多少棋

思路:類比,利用一個並查集來儲存鏈,然後並記錄下周圍有多少個空格,然後去類比,注意幾個點,就是刪除的時候,要把空格還回去,還有邊界的位置是也算被圍死的

代碼:

#include <stdio.h>#include <string.h>#include <queue>#include <map>using namespace std;#define MP(a,b) make_pair(a,b)const int N = 10005;const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};int T, n;typedef pair<int, int> pii;map<pii, int> vi, vis;int parent[N], sum[N], x[N], y[N];int find(int x) {    if (x == parent[x]) return x;    return parent[x] = find(parent[x]);}void init() {    scanf("%d", &n);    vi.clear();    for (int i = 1; i <= n; i++) {parent[i] = i; sum[i] = 0;scanf("%d%d", &x[i], &y[i]);    }}void del(int x, int y, int who) {    queue<pii> Q;    Q.push(MP(x, y));    vis.clear();    vis[MP(x,y)] = 1;    while (!Q.empty()) {pii now = Q.front();parent[vi[now]] = vi[now];sum[vi[now]] = 0;vi.erase(now);Q.pop();for (int i = 0; i < 4; i++) {    int xx = now.first + d[i][0];    int yy = now.second + d[i][1];    if (xx <= 0 || yy <= 0 || vis[MP(xx,yy)]) continue;    int tmp = vi[MP(xx,yy)];    if ((tmp&1)^who == 0) {vis[MP(xx,yy)] = 1;Q.push(MP(xx, yy));    }    else {int pt = find(tmp);sum[pt]++;    }}    }}void solve() {    for (int i = 1; i <= n; i++) {vi[MP(x[i],y[i])] = i;int empty = 0;for (int j = 0; j < 4; j++) {    int xx = x[i] + d[j][0];    int yy = y[i] + d[j][1];    if (xx <= 0 || yy <= 0) continue;    if (vi.count(MP(xx,yy)) == 0) {empty++;continue;    }    int pv = find(vi[MP(xx,yy)]);    sum[pv]--;}sum[i] = empty;for (int j = 0; j < 4; j++) {    int xx = x[i] + d[j][0];    int yy = y[i] + d[j][1];    if (xx <= 0 || yy <= 0) continue;    if (vi.count(MP(xx,yy)) == 0) continue;    if (((vi[MP(xx,yy)]&1)^(i&1)) == 0) {int pa = find(i);int pb = find(vi[MP(xx,yy)]);if (pa != pb) {    parent[pa] = pb;    sum[pb] += sum[pa];}    }    else {int pv = find(vi[MP(xx,yy)]);if (sum[pv] == 0)    del(xx, yy, vi[MP(xx,yy)]&1);    }}int pv = find(i);if (sum[pv] == 0)    del(x[i], y[i], i&1);    }    int ansa = 0, ansb = 0;    vis.clear();    for (int i = n; i >= 1; i--) {if (vi.count(MP(x[i],y[i])) == 0 || vis[MP(x[i], y[i])]) continue;vis[MP(x[i],y[i])] = 1;if (vi[MP(x[i],y[i])]&1) ansa++;else {    ansb++;}    }    printf("%d %d\n", ansa, ansb);}int main() {    scanf("%d", &T);    while (T--) {init();solve();    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.