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

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 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

 
Output
Single line at which year the communication got cut off.

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.

 
Sample Input
1 4 6 011010 000010 100001 001000 7 0 3 1 5 1 3 0 0 1 2 2 4 2 1
 
Sample Output
4
 
Source
这个题直接dfs搜黑色的从左到右的路会超时,按照官方题解:先离线处理并查集(就是把所有的Q年里出现的所有山峰都直接按照1来处理),然后再照年份从后往前的顺序把新出现的平原加入并查集,记住要给起点和终点设两个新节点,不然只能循环查找(x=1)的节点来处理,会超时。。。
 
#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的更多相关文章

  1. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  2. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

  3. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...

  4. HDU 5652 India and China Origins 二分+并查集

    India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...

  5. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  6. HDU 5652 India and China Origins(经典并查集)

    特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...

  7. 并查集(逆序处理):HDU 5652 India and China Origins

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. hdu 5652 India and China Origins 二分+bfs

    题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...

  9. HDU 5652 India and China Origins 二分优化+BFS剪枝

    题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...

  10. HDU 5652 India and China Origins

    二分答案+验证,注意一开始就不连通的话输出0 #include<cstdio> #include<cstring> #include<cmath> #include ...

随机推荐

  1. ssh整合步骤整理

    PackageUtil.createPackages(PackageUtil.changePath(System.getProperty("user.dir")));

  2. JavaScript Patterns 2.3 For loops

    HTMLCollections are objects returned by DOM methods such as: • document.getElementsByName() • docume ...

  3. geckofx

    geckofx是skybound工作室开发的一个开源的用于方便将gecko引擎(最主要的浏览器是firefox)链接到·net 窗体应用的一个组建.   外文名 geckofx 开发商 skyboun ...

  4. UVA 1640(DFS)

    题意:给你a,b两个数 问你a b区间中0 9出现的次数 其实就是求1-n中0-9出现的次数 ans[n]   答案就是ans[b]-ans[a-1] 怎么求的话看代码吧 #include<io ...

  5. la3211

    2-sat+二分... 每次二分答案然后连边2-sat...边要开到n*n 样例水得跟没有一样... #include<bits/stdc++.h> using namespace std ...

  6. Rails5 Controller Document

    更新: 2017/06/28 大致完成全部 更新: 2017/06/29 补充module文件命名规则 更新: 2017/07/09 补充session的设置 更新: 2018/03/06 修正ren ...

  7. spring基础学习---简单配置文件

  8. 【转】 [MySQL 查询语句]——分组查询group by

    group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...

  9. wxwidgets安装环境配置

    一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...

  10. Hadoop基础(二)

    HDFS 读写流程 我们知道在HDFS中我们的文件按数据块进行存储,那么当我们写入或者读取一个文件的时候HDFS到底进行了哪些操作呢? HDFS 写流程 如上图所示,假如我们有一个四个节点的集群,并且 ...