hdu5652 India and China Origins(并查集联通)
2016-06-04 15:18
197 查看
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1444 Accepted Submission(s): 493
Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.
Let’s assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven’t yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can’t travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can’t travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.
Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
Input
There are multi test cases. the first line is a sinle integer T which represents the number of test cases.
For each test case, the first line contains two space seperated integers N,M. next N lines consists of strings composed of 0,1 characters. 1 denoting that there’s already a mountain at that place, 0 denoting the plateau. on N+2 line there will be an integer Q denoting the number of mountains that rised up in the order of times. Next Q lines contain 2 space seperated integers X,Y denoting that at ith year a mountain rised up at location X,Y.
T≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X < N
0≤Y < M
Output
Single line at which year the communication got cut off.
print -1 if these two countries still connected in the end.
左边建立一个源点,右边一个汇点,只要有黑色的块联通,中国和印度就失联了。
模拟每次即可。
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1444 Accepted Submission(s): 493
Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.
Let’s assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven’t yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can’t travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can’t travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.
Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
Input
There are multi test cases. the first line is a sinle integer T which represents the number of test cases.
For each test case, the first line contains two space seperated integers N,M. next N lines consists of strings composed of 0,1 characters. 1 denoting that there’s already a mountain at that place, 0 denoting the plateau. on N+2 line there will be an integer Q denoting the number of mountains that rised up in the order of times. Next Q lines contain 2 space seperated integers X,Y denoting that at ith year a mountain rised up at location X,Y.
T≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X < N
0≤Y < M
Output
Single line at which year the communication got cut off.
print -1 if these two countries still connected in the end.
左边建立一个源点,右边一个汇点,只要有黑色的块联通,中国和印度就失联了。
模拟每次即可。
#include<cstdio> #include<queue> const int maxn=505; int a[maxn][maxn]; struct note { int x,y; note() { } note(int a1,int b1) { x=a1,y=b1; } } link[maxn*maxn]; int n,m; int d[8][2]= {{1,0},{0,1},{0,-1},{-1,0},{1,-1},{-1,1},{1,1},{-1,-1}}; bool used[maxn][maxn]; int fa[maxn*maxn]; int finds(int x) { if(x!=fa[x])fa[x]=finds(fa[x]); return fa[x]; } int add(int x,int y) { int fx=finds(x),fy=finds(y); if(fx^fy) { fa[fx]=fy; } } void islink() { std::queue<note> que; while(!que.empty())que.pop(); for(int i=0;i<n;++i) for(int j=0; j<m; ++j) { que.push(note(i,j)); } while(!que.empty()) { note p=que.front(); que.pop(); for(int i=0; i<8; ++i) { int tx=p.x+d[i][0],ty=p.y+d[i][1]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[p.x][p.y]&&a[tx][ty]) { add(p.x*m+p.y,tx*m+ty); // printf("add=%d %d\n",p.x*m+p.y,tx*m+ty); } } } } void loo(int s,int t){ printf("%d %d\n",finds(s),finds(t)); } int main() { #ifndef ONLINE_JUDGE freopen("my1.txt","r",stdin); #endif int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { scanf("%1d",&a[i][j]); } } int Q; scanf("%d",&Q); int Qs=Q; while(Qs--) { scanf("%d%d",&link[Qs].x,&link[Qs].y); } for(int i=0; i<=n*m+1; ++i)fa[i]=i; islink(); int s=n*m,t=n*m+1; for(int i=0; i<n; ++i) { add(s,i*m); add(t,i*m+m-1); } int ans=-1; int day=0; for(int i=Q-1; i>=0; --i) { if(finds(s)^finds(t)) { int lx=link[i].x,ly=link[i].y; a[lx][ly]=1; int pos=lx*m+ly; int fl=finds(pos); for(int j=0; j<8; ++j) { int tx=lx+d[j][0],ty=ly+d[j][1]; if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]) { add(tx*m+ty,pos); } } } else { ans=day; break; } ++day; } if(finds(s)^finds(t))ans=day; printf("%d\n",ans); } return 0; }
相关文章推荐
- UVA 10099 - The Tourist Guide
- Java多线程初探——正确停止线程
- 软件版本字符串排序
- SetupWizard界面长按Power键没有飞行模式选项
- JAVA操作Excel文件
- c#大文件分割过程
- XTU Dormitory's Elevator(动态规划)
- php学习第二天---基础语法3
- mysql-索引
- <SearchView > 搜索框的简单使用
- qnx:从API开始理解QNX -- 消息传递
- JQuery.cycle()实现图片轮播简单使用
- 文件上传
- AsyncTask串并行 源码分析
- 正则表达式--Group
- TCP网络编程demo
- java开发 注解相关总结
- "围观"设计模式(17)--结构型之享元模式(Flyweight Pattern)
- C++ 实现一个二进与文本形式存储的小型数据库(未完成)
- ActivityManager获得正在运行的activity与service