pid=4775" target="_blank" style="">题目链接: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 5

2 4

2 3

3 1

3 2

3 5

3 4

4 2

4 3

4 4

1 6

5 3

3 3

1 10

3 3

12

1 2

1 1

2 1

2 2

1 3

3 1

2 3

1 4

3 2

3 3

4 2

2 4

4

1 1

1 2

2 2

2 1

4

2000000000 2000000000

2000000000 1999999999

1999999999 1999999999

1999999999 2000000000

8

1 2

4 1

2 1

4 2

2 3

4 3

3 2

2 2

17

1 3

1 4

2 2

1 5

2 4

2 3

3 1

3 2

3 5

3 4

4 2

4 3

4 4

1 6

5 3

30 30

3 3

17

1 3

1 4

2 2

1 5

2 4

2 3

3 1

3 2

3 5

3 4

4 2

3 3

4 4

1 6

5 3

4 3

100 100

答案

4 2

9 4

6 4

1 2

2 2

4 3

9 4

9 3

#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <algorithm> using namespace std;
const int maxn = 1e4;
const int INF = 2*1e9+10;
const int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
typedef pair<int, int> pii; int N, Nw, Nb, X[maxn+5], Y[maxn+5], f[maxn+5], c[maxn+5];
map<pii, int> R; void init () {
scanf("%d", &N); Nw = N / 2;
Nb = N - Nw; R.clear();
for (int i = 0; i < N; i++) {
f[i] = i;
c[i] = 0;
}
} inline int bit (int x) {
return x&1;
} int getfar (int x) {
return f[x] == x ? x : f[x] = getfar(f[x]);
} inline bool isEmpty (int x, int y) {
if (x <= 0 || y <= 0 || x >= INF || y >= INF)
return false;
if (R.count(make_pair(x, y)))
return false;
return true;
} inline int count_empty (pii u) {
int cnt = 0;
for (int i = 0; i < 4; i++) {
int x = u.first + dir[i][0];
int y = u.second + dir[i][1];
if (isEmpty(x, y))
cnt++;
}
return cnt;
} inline void link_board (int x, int y) {
int fx = getfar(x);
int fy = getfar(y); f[fy] = fx;
c[fx] += c[fy];
/**/
c[fx]--;
} int del_board (int col, int x, int y) {
int cnt = 1;
pii u = make_pair(x, y);
queue<pii> que;
que.push(u); f[R[u]] = R[u];
R.erase(u); while (!que.empty()) {
u = que.front();
que.pop(); for (int i = 0; i < 4; i++) {
int p = u.first + dir[i][0];
int q = u.second + dir[i][1]; if (p <= 0 || p >= INF || q <= 0 || q >= INF)
continue; pii v = make_pair(p, q);
if (!R.count(v))
continue; int set = R[v]; if (bit(set) != col) {
int k = getfar(set);
c[k]++;
continue;
} f[R[v]] = R[v];
R.erase(v);
cnt++;
que.push(v);
}
}
return cnt;
} void del_empty (int k) {
int fk = getfar(k);
c[fk]--; if (c[fk] == 0) {
int set = bit(fk);
int cnt = del_board(set, X[fk], Y[fk]);
if (set)
Nw -= cnt;
else
Nb -= cnt;
}
} void solve () { for (int i = 0; i < N; i++) {
scanf("%d%d", &X[i], &Y[i]);
pii v = make_pair(X[i], Y[i]);
c[i] = count_empty(v);
R[v] = i; for (int j = 0; j < 4; j++) {
int p = X[i] + dir[j][0];
int q = Y[i] + dir[j][1]; if (p <= 0 || q <= 0 || p >= INF || q >= INF)
continue; pii u = make_pair(p, q);
if (!R.count(u))
continue; int k = R[u];
if (bit(i) == bit(k))
link_board(i, k);
else
del_empty(k);
} int fi = getfar(i);
if (c[fi] == 0) {
int cnt = del_board(bit(fi), X[fi], Y[fi]);
if (bit(fi))
Nw -= cnt;
else
Nb -= cnt;
}
}
printf("%d %d\n", Nb, Nw);
} int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
}
return 0;
}

hdu 4775 Infinite Go(暴力)的更多相关文章

  1. hdu 5461 Largest Point 暴力

    Largest Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  2. hdu 5762 Teacher Bo 暴力

    Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  3. HDU 1333 基础数论 暴力

    定义一种数位simth数,该数的各位之和等于其所有质因子所有位数字之和,现给出n求大于n的最小该种数,n最大不超过8位,那么直接暴力就可以了. /** @Date : 2017-09-08 14:12 ...

  4. HDU 4618 Palindrome Sub-Array 暴力

    Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...

  5. HDU 2089 不要62 | 暴力(其实是个DP)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2089 题解: 暴力水过 #include<cstdio> #include<algor ...

  6. hdu 3689 Infinite monkey theorem

    Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. HDU 6115 Factory LCA,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...

  8. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  9. hdu 3624 City Planning(暴力,也可扫描线)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. Scala 函数(五)

    函数是一组一起执行一个任务的语句. 您可以把代码划分到不同的函数中.如何划分代码到不同的函数中是由您来决定的,但在逻辑上,划分通常是根据每个函数执行一个特定的任务来进行的. Scala 有函数和方法, ...

  2. SQL注入原理深度解析

    本文转自:http://www.iii-soft.com/forum.php?mod=viewthread&tid=1613&extra=page%3D1 对于Web应用来说,注射式攻 ...

  3. oracle ORA_ROWSCN 行记录的更新时间

    在这介绍两个oracle 10G开始提供的一个伪列ORA_ROWSCN,它又分为两种模式一种是基于block,这是默认的模式,还有一种是基于row上,这种模式只能在建里表时指定ROWDEPENDENC ...

  4. NPOI兼容 excel2003,2007版本

    根据项目需要,需要对excel进行导入导出,所以选择NPOI,优点在这里就不详细介绍了,下面进入正题. public int Import(string path) { IList<Studen ...

  5. Javascript进阶篇——( JavaScript内置对象---上-Date,string,charAt,indexOf,split,substring,substr)笔记整理

    什么是对象JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法.对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等:对象的方法:能够在 ...

  6. SQL日期格式转换(经常用又经常忘记的东西)转载自http://www.cnblogs.com/wangyuelang0526/archive/2012/06/06/2538224.html

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14Select CONVERT(varchar(100), GETDATE(), 9): 06 6 ...

  7. 在IIS上Office Word下载失败,检索 COM 类工厂中 CLSID 为000209FF的组件失败,80070005 拒绝访问。

    最近在做一个网站时,有一个下载word文档功能,在本地直接调试是可以下载的,但部署到IIS上就出现问题了. 出现问题如下:Error:下载简历方法出错:检索 COM 类工厂中 CLSID 为 {000 ...

  8. NodeJS初学者实战之旅(I) —— 介绍、目录

    旅行之初 作为一个工作了两年多后端开发人员,未接触过NodeJS,对它的认知也仅仅停留在“可以使用JavaScript代码编写服务端程序”上.最近空闲时间 较多,便想来啃它一啃.但是没有一个实际的玩意 ...

  9. override和new的区别

    override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...

  10. HDU 5794 - A Simple Nim

    题意:    n堆石子,先拿光就赢,操作分为两种:        1.任意一堆中拿走任意颗石子        2.将任意一堆分成三小堆 ( 每堆至少一颗 )        分析:    答案为每一堆的 ...