题目链接: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. android上下文菜单

    XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmln ...

  2. java 配置文件读取

    1.getResourceAsStream Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path ...

  3. html5设计原理(转)

    转自:   http://www.cn-cuckoo.com/2010/10/21/the-design-of-html5-2151.html 今天我想跟大家谈一谈HTML5的设计.主要分两个方面:一 ...

  4. 项目中遇到的各种bug和踩过的坑

    zepto 赋值时单位转换问题 zepto 的 animate 方法移动某个元素的位置时,例如修改某个绝对定位的元素的 left 值,要与修改前的值单位一致,修改前如果是像素值,修改后也要是像素值,否 ...

  5. js中array的filter用法

    function bouncer(arr) { // Don't show a false ID to this bouncer. arr = arr.filter(function(val) { i ...

  6. Runtime Reconfiguration

    https://coreos.com/etcd/docs/latest/runtime-configuration.html Runtime Reconfiguration  运行时重新配置 etcd ...

  7. gfw列表

    https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt

  8. 1-12 ARP协议

    ARP(Address Resolution Protocol)地址解析协议,负责将相应的IP地址解析成MAC地址. 在局域网中,网络中实际传输的是‘帧’,帧里面包含了目的主机的MAC.ARP就是用来 ...

  9. 《Pro Git》阅读随想

    之前做版本管理,我使用最多的是SVN,而且也只是在用一些最常用的操作.最近公司里很多项目都开始上Git,借这个机会,我计划好好学习一下Git的操作和原理,以及蕴含在其中的设计思想.同事推荐了一本< ...

  10. varchar(n),nvarchar(n) 长度、性能、及所占空间的说明

    varchar(n),nvarchar(n) 中的n怎么解释: nvarchar(n)最多能存n个字符,不区分中英文. varchar(n)最多能存n个字节,一个中文是两个字节. 所占空间: nvar ...