题目链接:

India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 685    Accepted Submission(s): 230

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.

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
 
 
 
题意:    
 
给你一些0和1组成的字符串,表示i行j列的状态,0表示平原,1表示山峰,然后再给你q个数对表示在第i年位置l,r处要变成山峰;问最早在第几年印度和中国之间不再连通了;
 
 
思路:
 
比赛的时候直接bfs判断连通性,最后tle了,后来看别人博客说要二分,或者用并查集,想了一会还是想不好并查集怎么处理,就写了个二分+bfs;啊啊,写了几个二分之后发现现在写二分好好玩啊,一开始入ACM坑的时候写的二分都忒傻逼的那种,现在终于变成熟了,啊哈哈哈哈;我骄傲!不过现在我仍然好弱好弱,还是要不断加油才行;
 
 
 
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,q,dir[][]={,,,-,,,-,},vis[][],l[],r[];
char str[][];
struct node
{
int x,y;
};
node a;
queue<node>qu;
int check(int num)
{
while(!qu.empty())qu.pop();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='')vis[i][j]=;
else vis[i][j]=;
}
}
for(int i=;i<=num;i++)
{
vis[l[i]][r[i]]=;
}
for(int k=;k<m;k++)
{
if(!vis[][k])
{
a.x=;
a.y=k;
qu.push(a);
}
while(!qu.empty())
{
int topx=qu.front().x,topy=qu.front().y;
qu.pop();
if(topx==n-)return ;
for(int i=;i<;i++)
{
int fx=topx+dir[i][],fy=topy+dir[i][];
if(fx>=&&fx<n&&fy>=&&fy<m)
{
if(!vis[fx][fy])
{
if(fx==n-)return ;
a.x=fx;
a.y=fy;
qu.push(a);
vis[fx][fy]=;
}
}
}
}
}
return ;
}
int bis()
{
int L=,R=q,mid;
while(L<=R)
{
mid=(L+R)>>;
if(check(mid))L=mid+;
else R=mid-;
}
if(L>q)return -;
return L;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
}
scanf("%d",&q);
for(int i=;i<=q;i++)
{
scanf("%d%d",&l[i],&r[i]);
}
printf("%d\n",bis());
}
return ;
}

hdu-5652 India and China Origins(二分+bfs判断连通)的更多相关文章

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

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

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

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

  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 二分优化+BFS剪枝

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

  5. hdu 5652 India and China Origins 并查集+二分

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

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

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

  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 || 并查集)BestCoder Round #77 (div.2)

    题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...

  9. hdu 5652 India and China Origins 并查集

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

随机推荐

  1. 小图拼接大图MATLAB实现

    小图拼接大图MATLAB实现 1.实现效果图 原图 效果图 2.代码 files = dir(fullfile('D:\document\GitHub\homework\digital image p ...

  2. vue实践---vue动态加载组件

    开发中遇到要加载10个或者更多的,类型相同的组件时,如果用普通的 import 引入组件,components注册组件,代码显得太啰嗦了,这时候就需要用到 require.context 动态加载这些 ...

  3. 鼠标画矩形openCV

    #include <cv.h> #include <highgui.h> #include <stdio.h> /*鼠标画矩形*/ void my_mouse_ca ...

  4. 不怕慢 就怕站 不怕单线程 不怕 裸露ip

    import sys import os import requests import threading from time import sleep from bs4 import Beautif ...

  5. 【JavaScript专题】--- 立即执行函数表达式

    一 什么是立即执行函数表达式 立即执行函数表达式,其实也可以叫初始化函数表达式,英文名:IIFE,immediately-inovked-function expression.立即执行函数表达式就是 ...

  6. iOS线程浅析

    一.线程概述 1. iOS里面的线程按种类可分为同步线程和异步线程.同步线程指调用同步线程的地方必须等到同步线程运行完成才干够继续向下运行.而调用异步线程的地方则在运行完调用异步线程的语句后就能够继续 ...

  7. linux c编程:系统数据文件和信息

    linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...

  8. MySQL5.7.26 忘记Root密码小计

    以前直接修改mysql.user就ok了,现在不行了,正好虚拟机MySQL的root密码忘记了,就简单记录下:(本方法不适合互联网线上项目,除非你不在意这段时间的损失) PS:以UbuntuServe ...

  9. HTTP 304 详解

    把Last-Modified 和ETags请求的http报头一起使用,这样可利用客户端(例如浏览器)的缓存.因为服务器首先产生 Last-Modified/Etag标记,服务器可在稍后使用它来判断页面 ...

  10. 找到最大或最小的N个值

    对于python原生的数据类型来说,并不存在直接的方法可以找到最大或最小的N个值, 传统的方法必须先排序,然后再截取相应的值,而且对于集合这类数据类型来说还不能直接排序, 需要先转化为列表才行,有的时 ...