题目链接

给一个图, 由01组成, 1不能走。 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通。

二分操作, 然后bfs判联通就好了。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
char s[505][505];
int g[505][505], f[505][505], vis[595][505], n, m;
pll a[250001];
int bfs(int x, int y) {
queue<pll> q;
q.push(mk(x, y));
vis[x][y] = 1;
while(!q.empty()) {
pll tmp = q.front(); q.pop();
if(tmp.fi == n+1)
return 0;
for(int i = 0; i < 4; i++) {
int tmpx = tmp.fi+dir[i][0];
int tmpy = tmp.se+dir[i][1];
if(tmpx>=0&&tmpx<=n+1&&tmpy>=0&&tmpy<m) {
if(vis[tmpx][tmpy]||f[tmpx][tmpy])
continue;
vis[tmpx][tmpy] = 1;
q.push(mk(tmpx, tmpy));
}
}
}
return 1;
}
int check(int x) {
memcpy(f, g, sizeof(f));
for(int i = 0; i < x; i++) {
f[a[i].fi+1][a[i].se] = 1;
}
mem(vis);
return bfs(0, 0);
}
int main()
{ int t, q, x, y;
cin>>t;
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
scanf("%s", s[i]);
}
cin>>q;
for(int i = 0; i < q; i++) {
scanf("%d%d", &a[i].fi, &a[i].se);
}
mem(g);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
g[i+1][j] = s[i][j]-'0';
}
}
int l = 0, r = q, ans = -1;
while(l<=r) {
int mid = l+r>>1;
if(check(mid)) {
ans = mid;
r = mid-1;
} else {
l = mid+1;
}
}
printf("%d\n", ans);
}
return 0;
}

hdu 5652 India and China Origins 二分+bfs的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu-5652 India and China Origins(二分+bfs判断连通)

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

  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. js提取整数部分,移除首末空格

    给Object.prototype增加方法可使该方法对所有对象可用,这样的方式对函数.数组.字符串.数字.正则表达式和布尔值同样适用.比如说为Function.prototype增加方法来使得改方法对 ...

  2. TcpClient

    public class TcpClientSession { protected TcpClient Client { get; set; } /// <summary> /// 远程地 ...

  3. size对齐

    写了很多程序,在很多地方都要用到内存长度对齐,比如文件的大小,pe文件内存的对齐等等. 下面记下两种简单的方法,觉得没啥大用处. #include <IOSTREAM> #include ...

  4. Lazy evaluation

    是一段源码,关于Lazy evaluation的,看了很久才懂,记录一下 一,lazy方法返回的比较复杂,一层一层将其剥开. wraps(func)跳转到curry(update_wrapper, f ...

  5. ubuntu 12.04 安装谷歌浏览器

    http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 sudo dpkg -i google-chrome-stable_current ...

  6. 关于 zend studio 中有些php 内置函数没有提示,或是有‘小黄色感叹号’

    解决办法: 1.修改项目 .buildpath 文件 <?xml version="1.0" encoding="UTF-8"?> <buil ...

  7. Android onSaveInstanceState()

    我们知道,由于手机的内存问题,很容易造成切换activity之后上一个activity被回收的情况,虽然我们按下back按键的时候,还是能够回到上一个activity,但是此时我们并不是执行的onRe ...

  8. 当tomcat有两个链接数据库的应用同时运行可能冲突

    -Xms512M   -Xmx1024M -XX:MaxPermSize=256M

  9. Android L 之 RecyclerView 、CardView 、Palette

    转: http://blog.csdn.net/xyz_lmn/article/details/38735117 <Material Design>提到,Android L版本中新增了Re ...

  10. HTML 5 学习之应用程序缓存

    什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏 ...