HDU 4775 Infinite Go(并查集,模拟)

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

HDU 4775 Infinite Go(并查集,模拟)

时间: 2024-10-07 09:08:49

HDU 4775 Infinite Go(并查集,模拟)的相关文章

hdu 4775 Infinite Go(暴力)

题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋,总共走了n步,黑棋和白棋交替走,如果一片棋的上下左右被封死,那么该片棋子就会被吃掉,问说最后黑白棋各剩多少个. 解题思路:比较恶心的模拟题,相邻相同色的棋子要用并查集连接,并且要记录每片棋子还剩的空格数,如果空格数为0的话说明该片棋子被其他颜色围住,则要剔除掉,不且将相邻的位置不同色的棋空格数加1.主要是细节上的问题. 样例 8 7 5 5 4 5 3 5 3 4 4 4 3 3 4 6 18 1 3 1 4 2 2 1

hdu 3047 Zjnu Stadium 并查集高级应用

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1631    Accepted Submission(s): 616 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built

hdu 3234 Exclusive-OR (并查集+异或性质)

Exclusive-OR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2177    Accepted Submission(s): 603 Problem Description You are not given n non-negative integers X0, X1, ..., Xn-1 less than 220 ,

HDU 4496 D-City (并查集)

题意:给你n个点m条边,问删除前i条边后有多少个连通分块. 思路:从后往前操作,从后往前添加i条边等于添加完m条边后删掉前m-i条边,可知刚开始没有边,所以sum[m]=n; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010

hdu 1811Rank of Tetris (并查集 + 拓扑排序)

1 /* 2 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B,小于B. 3 4 现在Lele并不是让你来帮他制作这个高手榜,他只是想知道,根据这些信息是否能够确定出这个高手榜,是的话就输出"OK". 5 否则就请你判断出错的原因,到底是因为信息不完全(输出"UNCERTAIN"),还是因为这些信息中包含冲突(输出&quo

HDU 1232 畅通工程(并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30485    Accepted Submission(s): 16013 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有

HDU 2017 Code Lock (并查集的应用+快速幂)

链接:HDU 3461 题目大意: 题目的大意是一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限次的"增加"操作后可以变成另一组密码,那么我们认为这两组密码是相同的.该题的目标就是在给定N.M和M个区间的前提下计算有多少种不同的密码. 根据题意,如果一个可调整的区间都没有的话,答案应该是26

HDU 1198-Farm Irrigation(并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5820    Accepted Submission(s): 2523 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

HDU 1198 Farm Irrigation (并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5809    Accepted Submission(s): 2516 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle,