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 ...
随机推荐
- ssh整合步骤整理
PackageUtil.createPackages(PackageUtil.changePath(System.getProperty("user.dir")));
- JavaScript Patterns 2.3 For loops
HTMLCollections are objects returned by DOM methods such as: • document.getElementsByName() • docume ...
- geckofx
geckofx是skybound工作室开发的一个开源的用于方便将gecko引擎(最主要的浏览器是firefox)链接到·net 窗体应用的一个组建. 外文名 geckofx 开发商 skyboun ...
- UVA 1640(DFS)
题意:给你a,b两个数 问你a b区间中0 9出现的次数 其实就是求1-n中0-9出现的次数 ans[n] 答案就是ans[b]-ans[a-1] 怎么求的话看代码吧 #include<io ...
- la3211
2-sat+二分... 每次二分答案然后连边2-sat...边要开到n*n 样例水得跟没有一样... #include<bits/stdc++.h> using namespace std ...
- Rails5 Controller Document
更新: 2017/06/28 大致完成全部 更新: 2017/06/29 补充module文件命名规则 更新: 2017/07/09 补充session的设置 更新: 2018/03/06 修正ren ...
- spring基础学习---简单配置文件
- 【转】 [MySQL 查询语句]——分组查询group by
group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...
- wxwidgets安装环境配置
一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...
- Hadoop基础(二)
HDFS 读写流程 我们知道在HDFS中我们的文件按数据块进行存储,那么当我们写入或者读取一个文件的时候HDFS到底进行了哪些操作呢? HDFS 写流程 如上图所示,假如我们有一个四个节点的集群,并且 ...