UVALive 6459 Infinite Go (模拟,搜索)
来源:互联网 发布:activiti python 编辑:程序博客网 时间:2024/06/06 02:10
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4470
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.
Input
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.
Output
For each test case, output two integers N b and Nw in one line, separated by a single space. N b is the
number of black stones left on the board, while Nw is the number of white stones left on the board.
Sample Input
1
7
5 5
4 5
3 5
3 4
4 4
3 3
4 6
Sample Output
4 2
题意:
有一个1->INF*1->INF的围棋棋盘,给出双方的落子,求最后双方剩余棋子的数量。
分析:
又是一道大力出奇迹的题,直接裸的搜索就行。每走一步就搜索该点和它周围4个点影响的联通块,如果没有气就删掉。但要注意先搜它周围4个点再搜它自己,反过来虽然也能AC,但这是不对的。
/* * *Author:fcbruce * *Date:2014-09-06 19:34:30 * */#include <cstdio>#include <iostream>#include <sstream>#include <cstdlib>#include <algorithm>#include <ctime>#include <cctype>#include <cmath>#include <string>#include <cstring>#include <stack>#include <queue>#include <list>#include <vector>#include <map>#include <set>#define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32#define lld "%I64d"#else#define lld "%lld"#endif#define maxm #define maxn using namespace std;int dx[]={0,0,1,-1};int dy[]={-1,1,0,0};map<pair<int,int>,int>MAP,vis;pair<int,int> q[11111];void bfs(int x,int y,int type){vis.clear();int f=0,r=-1;vis[q[++r]=make_pair(x,y)]=1;while (f<=r){auto status=q[f++];for (int i=0;i<4;i++){auto to_status=make_pair(status.first+dx[i],status.second+dy[i]);if (to_status.first>0 && to_status.second>0 && vis[to_status]==0){int colour=MAP[to_status];if (colour==0)return ;if (colour==type)q[++r]=to_status;vis[to_status]=1;}}}for (int i=0;i<=r;i++)MAP[q[i]]=0;}int main(){#ifdef FCBRUCEfreopen("/home/fcbruce/code/t","r",stdin);#endif // FCBRUCEint T_T;scanf( "%d",&T_T);while (T_T--){MAP.clear();int n;scanf( "%d",&n);for (int i=0,x,y;i<n;i++){scanf( "%d%d",&x,&y);MAP[make_pair(x,y)]=i&1?1:-1;for (int i=0;i<4;i++){int tx=x+dx[i],ty=y+dy[i];if (MAP[make_pair(tx,ty)])bfs(tx,ty,MAP[make_pair(tx,ty)]);}bfs(x,y,i&1?1:-1);}int black=0,red=0;for (auto it : MAP){if (it.second==1)red++;if (it.second==-1)black++;}printf( "%d %d\n",black,red);}return 0;}
- UVALive 6459 Infinite Go (模拟,搜索)
- HDU 4775 Infinite Go(并查集,模拟)
- hdu 4775 Infinite Go(并查集 模拟)
- HDU 4775 Infinite Go(暴力模拟+BFS)
- Ancient Go(模拟+搜索)
- HDU 4775 Infinite Go
- HDU -4775:Infinite Go
- hdu 4775 Infinite Go(暴力)
- HDU 4775 Infinite Go 解题报告
- HDU 4775 Infinite Go 并查集
- HDU-4775---Infinite Go (bfs)
- Let's Go Green UVALive
- Let's Go Green UVALive
- UVALive 3497 brainfuck [模拟]
- UVALive 4723 水模拟
- UVALive 6833 (模拟 水~)
- UVALive 7503--模拟
- UValive 7269 (模拟)
- 【LeetCode】Balanced Tree & Binary Search Tree
- MVC入门的一个例子(一)
- 将矩阵中元素0所在的行和列置为0
- jsp Hibernate 数据持久化技术(一)
- 【HDU 4453】 Looploop(Splay)
- UVALive 6459 Infinite Go (模拟,搜索)
- 数组字符串那些经典算法:最大子序列和,最长递增子序列,最长公共子串,最长公共子序列,字符串编辑距离,最长不重复子串,最长回文子串
- 文件分割,make及makefile的使用
- hdu1142 A Walk Through the Forest(前向星+SPFA解法)
- 12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 我经历的那些骗局(要承认, 骗子智商比我们高)———今天一IT程序猿哥们被骗4-5万, 遂有感而发
- 记于工作一个月
- ZOJ 2587 Unique Attack (判断最小割的唯一性)
- C#清除缓存、清除IE临时文件缓存cookies的方法