hdu 5652 India and China Origins 二分+bfs
题目链接
给一个图, 由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的更多相关文章
- 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 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 二分优化+BFS剪枝
题目大意:给你一个地图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判断连通)
题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 并查集(逆序处理):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(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
随机推荐
- .net 加密错误:填充无效,无法移除
今天用System.Security.Cryptography加密.使用了AesManaged,报错:填充无效,无法移除.分析是解密失败,密文损坏,或者KEY,IV不正确. using (AesMan ...
- Oracle存储过程的一点使用总结
[博客文章背景]博客开通已经1.2年了,一直碍于技术能力,不敢献丑.想起1年前在一个数据处理相关项目结束后,代金排主管让我做一个数据库开发总结文档和一个Toad.PL/SQL Developer工具的 ...
- 动态获取UIWebView的真正高度
场景 在 App 中使用UIWebView加载网页, 与原生的 UI 显示在一起,一般情况下,webView 的 内容一页是肯定不够的,换句话说,webView 的高度是不定的,那如果原生的 UI是一 ...
- STM32学习笔记之一(初窥STM32)
怎么做好学习笔记? 答:自我感知-->学习知识-->归纳总结-->自我升华(真正属于自己的知识是,抛开书本,运用时,你还能记得的思想) 自我感知--看到知识概念,先自我感觉那应该是个 ...
- n条直线的最多交点
#include <iostream>using namespace std;int main(){int i,n;while(cin>>n){if(n==0||n==1) c ...
- spring集成mongodb jar包版本问题
在开发过程中,spring集成mongodb的jar包. 如果需要使用排序功能. spring-data-mongodb-1.4.1.RELEASE.jar 的版本为1.4.1,如果使用如下代码: Q ...
- 在Linux手动把文件转码的方法,防止乱码出现
iconv -f utf-8 -t gb2312 report.html > report_test.html
- windows下使用mingw编译python扩展模块
环境: 1.python2.7.2 2.mingw20120426 说明: PYTHON_HOME:不做特殊说明的话,默认代表python的安装路径. MINGW_HOME:不做特殊说明的话,默认代表 ...
- [技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档
[技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档 Doxygen本来是一个很好的工具,可是我感觉在mac系统下,如果用doxygen最后生成的CHM文件感觉就不是那么恰当, ...
- POJ 3579 Median(二分答案+Two pointers)
[题目链接] http://poj.org/problem?id=3579 [题目大意] 给出一个数列,求两两差值绝对值的中位数. [题解] 因为如果直接计算中位数的话,数量过于庞大,难以有效计算, ...