hdu 5652
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 333
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.
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 Qlines 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
print -1 if these two countries still connected in the end.
Hint:
From the picture above, we can see that China and India have no communication since 4th year.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int Max=;
int n,m;
int cnt;
char ch[Max];
bool used[Max][Max];
int mat[Max][Max];
int a[Max][Max];
struct node
{
int x,y;
} que[Max*Max],qq[Max*Max];
int f[Max*Max];
const int inf=Max*Max-;
int find(int x)
{
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
void Merge(int i,int j)
{
int t1=find(i),t2=find(j);
if(t1!=t2)
{
f[t2]=t1;
}
}
int dx[]= {,-,,};
int dy[]= {,,,-};
void solve()
{
for(int i=; i<=cnt; i++)
{
if(used[que[i].x][que[i].y]) continue;
for(int k=; k<; k++)
{
int x=que[i].x+dx[k],y=que[i].y+dy[k];
if(x<||y<||x>n||y>m) continue;
if(a[x][y]==) continue;
int j=mat[x][y];
if(used[x][y]) continue;
Merge(i,j);
}
}
}
int main()
{
int T;
for(scanf("%d",&T); T; T--)
{
scanf("%d%d",&n,&m);
cnt=;
for(int i=; i<=n; i++)
{
scanf("%s",ch);
for(int j=; j<strlen(ch); j++)
{
if(ch[j]=='')
{
a[i][j+]=;
que[++cnt].x=i;
que[cnt].y=j+;
mat[i][j+]=cnt;
}
else a[i][j+]=;
}
}
int Q,x,y;
int flag=;
memset(used,false,sizeof(used));
scanf("%d",&Q);
for(int i=; i<=Q; i++)
{
scanf("%d%d",&x,&y);
x+=;
y+=;
used[x][y]=true;
qq[i].x=x;
qq[i].y=y;
}
for(int i=; i<Max*Max; i++) f[i]=i;
for(int i=; i<=m; i++)
{
if(!used[][i])
if(a[][i]==)
Merge(inf-,mat[][i]);
if(!used[n][i])
if(a[n][i]==)
Merge(inf,mat[n][i]);
}
solve();
// cout<<find(inf-1)<<" "<<find(inf)<<endl;
int sum=Q;
while(find(inf-)!=find(inf))
{
int x=qq[Q].x,y=qq[Q].y;
used[x][y]=false;
Q--;
if(Q==-) break;
for(int k=; k<; k++)
{
int nx=x+dx[k],ny=y+dy[k];
if(nx<||ny<||nx>n||ny>m) continue;
if(used[nx][ny]) continue;
if(a[nx][ny]==)
Merge(mat[nx][ny],mat[x][y]);
}
if(x==) Merge(mat[x][y],inf-);
if(x==n) Merge(mat[x][y],inf);
}
printf("%d\n",Q==sum?-:Q+);
}
return ;
}
hdu 5652的更多相关文章
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
- HDU 5652 India and China Origins 二分+并查集
India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- 并查集(逆序处理):HDU 5652 India and China Origins
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5652 India and China Origins 二分+bfs
题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- HDU 5652 India and China Origins
二分答案+验证,注意一开始就不连通的话输出0 #include<cstdio> #include<cstring> #include<cmath> #include ...
随机推荐
- 快速编译system.img和boot.img的方法【转】
本文转载自:http://www.cnblogs.com/wanqieddy/archive/2012/10/22/2734024.html 快速编译system.img,可以使用这个命令: #mak ...
- 深入理解7816(1)---- 关于F/D和etu【转】
本文转载自:http://blog.sina.com.cn/s/blog_4df8400a0101gkss.html 深入理解7816(1)---- 关于F/D和etu 对于刚接触智能卡的工程师来说, ...
- Bing Maps进阶系列五:通过DeepEarth的MiniMap控件为Bing Maps扩展迷你小地图
Bing Maps进阶系列五:通过DeepEarth的MiniMap控件为Bing Maps扩展迷你小地图 Bing Maps Silverlight Control虽然为我们提供了简洁.方便的开发模 ...
- [bzoj5404]party
https://zybuluo.com/ysner/note/1240918 题面 这题面不好概括啊 解析 \(5pts\)算法 既然\(q=0\),打上文件输入输出即可. 当然不开够空间且不特判的小 ...
- codevs1574广义斐波那契数列
1574 广义斐波那契数列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 广义的斐波那契数列是指形如an=p* ...
- [Swift通天遁地]四、网络和线程-(3)线程组:使用DispatchGroup(调度组)对线程进行分组管理
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- unittest举例
步骤:1.先导入unittest2.编写一个测试类(继承unittest.TestCase)3.编写测试用例类,一个方法就是一条用例4.加载测试用例,有三种方式:加载测试方法,加载测试类,加载测试模块 ...
- ACM_Ruin of Titanic(简单贪心)
Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...
- zblog实现后台导航栏增加链接功能的最简单方法
首先在ftp中找到这个目录 zb_system/admin/ 然后找到 admin_top.php 这个文件 再然后找到这行代码 <?php ResponseAdm ...
- TCP简要讲解
TCP被称为面向连接.面向流,可靠的传输层协议.它与UDP最大的不同在于它是可靠性的,所以需要可靠传输的时候就需要用到TCP,如果需要快速传输则选择UDP. TCP的连接又称为3次握手. 1).客 ...