题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E

题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格

子。之后过了M年,每年把一个格子变成1, 即每年会有一个格子不能走。问地

图上界和下界连通共多少年。如果到M年之后依然连通,则输出-1.

给定的年份数据范围是10^5, 图的大小是500*500,由于图是一直在改变的,

因此每次都需要进行变更操作。然而如果变更一次广搜一次明显会TLE。仔细思

考会发现,当某一年上下界不再连通时,之后的所有年份必定不会连通;如果某

年上下界依然连通,则此年之前的所有年份都是连通的(因为此年是以前的年份

时的地图再封杀一些后得到的)。这么想之后可以发现寻找不连通的那一年可以

通过二分查找来实现。一些神犇的题解报告都是用并查集过的,然而蒟蒻的并查

集实在是太渣了...暴力解题出奇迹。

下面是AC代码:

/**

Memory: 3308 KB         Time: 1216 MS
Language: G++ Result: Accepted **/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
int n, m, x[], y[], vis[][];
char maps[][];
int dir[][] = {{, }, {, -}, {, }, {-, }};
struct ad
{
int x, y;
};
bool bfs(int x, int y)
{
queue<ad>Q;
ad now, next;
memset(vis, , sizeof(vis));
now.x = x;
now.y = y;
vis[now.x][now.y] = ;
Q.push(now);
while(Q.size())
{
now = Q.front();
Q.pop();
if(now.x==n-)return true;
for(int i=; i<; i++)
{
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
if(next.x>= && next.x<n && next.y>= && next.y<m && maps[next.x][next.y]=='' && !vis[next.x][next.y])
{
vis[next.x][next.y] = ;
Q.push(next);
}
}
}
return false;
}
bool check()
{
for(int i=; i<m; i++)
{
if(maps[][i]=='' && bfs(, i))
return true;
}
return false;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
for(int i=; i<n; i++)
scanf("%s", maps[i]);
int t;
scanf("%d", &t);
for(int i=; i<=t; i++)
scanf("%d %d", &x[i], &y[i]);
int l = , r = t, mid, ans = -;
while(l<=r)
{
mid = (l+r)/;
for(int i=; i<=mid; i++)
maps[x[i]][y[i]] = '';
if(check()) l = mid + ;
else
{
ans = mid;
r = mid-;
}
for(int i=; i<=mid; i++)
maps[x[i]][y[i]] = '';
}
printf("%d\n", ans);
}
return ;
}

HDU 5652(二分+广搜)的更多相关文章

  1. NOIP 模拟赛 23 T4 大逃亡O(二分+广搜)(∩_∩)O

    题目描述 给出数字N(1≤N≤10000),X(1≤x≤1000),Y(1≤Y≤1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从0到Y-1再给出四个数字x1,y1,x ...

  2. hdu 1495 非常可乐 广搜

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> ][][]; ...

  3. hdu 1342.. 复习广搜 顺便练习一下一个脑残的格式

    In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49}. A popular stra ...

  4. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  5. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

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

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

  7. Combine String HDU - 5707 dp or 广搜

    Combine String HDU - 5707 题目大意:给你三个串a,b,c,问a和b是不是恰好能组成c,也就是a,b是不是c的两个互补的子序列. 根据题意就可以知道对于c的第一个就应该是a第一 ...

  8. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  9. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. Linux学习三:Ubuntu下使用minicom和开发板通信

    备注:如果你是用的是Windows则使用超级终端即可:开始-程序-附件-通讯-超级终端 现在我们在Ubuntu下安装配置minicom: 1.进入ubuntu桌面ctrl+alt+t打开终端 输入:s ...

  2. 杭电acm 1003

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  3. hbase 协处理器

    一.服务端1.安装Protobuf2.RPC proto 定义文件:Examples.protooption java_package = "org.apache.hadoop.hbase. ...

  4. STL之序列式容器list与forward_list

    List (双向链表) 与 forwardlist (单向链表) 算是非常基础的数据结构了,这里只是简单介绍下其结构及应用. 以list为例: 其节点模板: template <class T& ...

  5. SICP练习记录

    -------------求一个数的平方根(牛顿法平方根求解法): (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt ...

  6. [C#]List<int>转string[],string[]转为string

    // List<int>转string[] public string[] ListInt2StringArray(List<int> input) { return Arra ...

  7. AJAX实现跨域的三种种方法(代理,JSONP,XHR2)

    由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问. 比如说你的网站域名是aaa.com,想要通过AJAX请 ...

  8. final的使用

    final对基本类型,限定常量. final对对象的引用,不可引用其他对象. final的字段,必须在定义时或者构造器内完成初始化.构造内才完成初始化的Blank Final(空白final). cl ...

  9. jq之ajax以及json数据传递

    <html> <head><meta http-equiv="Content-Type" content="text/html; chars ...

  10. SQL Server 显示执行一条语句的执行时间

    set statistics time on执行语句set statistics time off