首页 > ACM题库 > HDU-杭电 > HDU 4775-Infinite Go-并查集-[解题报告]HOJ
2015
09-17

HDU 4775-Infinite Go-并查集-[解题报告]HOJ

Infinite Go

问题描述 :

  Go is a proverbial board game originated in China. It has been proved to be the most difficult board game in the world. “The rules of Go are so elegant, organic, and rigorously logical that if intelligent life forms exist elsewhere in the universe, they almost certainly play Go.” said Emanuel Lasker, a famous chess master.
  A Go board consists of 19 horizontal lines and 19 vertical lines. So there are 361 cross points. At the beginning, all cross points are vacant.
  Go is played by two players. The basic rules are:
  1. One player owns black stones and the other owns white stones.
  2. Players place one of his stones on any vacant cross points of the board alternately. The player owns black stones moves first.
  3. Vertically and horizontally adjacent stones of the same color form a chain.
  4. The number of vacant points adjacent (vertically or horizontally) to a chain is called the liberty of this chain. Once the chain has no liberty, it will be captured and removed from the board.
  5. While a player place a new stone such that its chain immediately has no liberty, this chain will be captured at once unless this action will also capture one or more enemy’s chains. In that case, the enemy’s chains are captured, and this chain is not captured.
  
  In effect, Go also has many advanced and complex rules. However, we only use these basic rules mentioned above in this problem.
  Now we are going to deal with another game which is quite similar to Go. We call it “Infinite Go”. The only difference is that the size of the board is no longer 19 times 19 — it becomes infinite. The rows are numbered 1, 2, 3, …, from top to down, and columns are numbered 1, 2, 3, …, from left to right. Notice that the board has neither row 0 nor column 0, which means even though the board is infinite, it has boundaries on the top and on the left.
  In this problem, we are solving the problem that, given the actions of two players in a set of Infinite Go, find out the number of remaining stones of each player on the final board.

输入:

  The input begins with a line containing an integer T (1 <= T <= 20), the number of test cases.
  For each test case, the first line contains a single integer N (1 <= N <= 10000), the number of stones placed during this set. Then follows N lines, the i-th line contains two integer X and Y (1 <= X, Y <= 2,000,000,000), indicates that the i-th stone was put on row X and column Y (i starts from 1). The stones are given in chronological order, and it is obvious that odd-numbered stones are black and even-numbered ones are white.

输出:

  The input begins with a line containing an integer T (1 <= T <= 20), the number of test cases.
  For each test case, the first line contains a single integer N (1 <= N <= 10000), the number of stones placed during this set. Then follows N lines, the i-th line contains two integer X and Y (1 <= X, Y <= 2,000,000,000), indicates that the i-th stone was put on row X and column Y (i starts from 1). The stones are given in chronological order, and it is obvious that odd-numbered stones are black and even-numbered ones are white.

样例输入:

1
7
5 5
4 5
3 5
3 4
4 4
3 3
4 6

样例输出:

4 2

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;
}

参考:http://blog.csdn.net/accelerator_/article/details/37511267