Survive the flood

原题链接:
http://acm.timus.ru/problem.aspx?space=1&num=2113

                     Time limit: 2.0 secondMemory limit: 256 MB

Every rabbit knows what to do in case of a flood. All you need to do is to get to a point above the water level and wait for Mazai. But when the actual flood happens, rabbits start to panic. That’s why we ask for your help to create an optimal evacuation plan for rabbits.Let’s describe the flood as a game between a rabbit and water. Game takes place in a rectangular grid with n rows and m columns. Let’s say that a point in a row i and a column j has coordinates (i, j). It is known, that for all 1 ≤ i ≤ n, 1 ≤ j ≤ m a cell with coordinates (i, j) has height hij. The rabbit starts at a point with coordinates (r1, c1), water starts at point with coordinates (r2, c2). Moreover, the rabbit has a property named jump height.The rabbit and water take turns, the rabbit makes the first move. After each move the rabbit either doesn’t move or jumps to any adjacent cell. Cells are adjacent, if they have a common side. In addition to this, the rabbit cannot move to a cell, which height exceeds the height of a current cell more than on jump height. Water just fills all the cells that have adjacent cell filled with water of greater or equal height. Both water and the rabbit should stay within the game grid.Rabbit can only survive in the cells not filled with water. The game doesn’t ever stop. Your task is to find the minimum jump height the rabbit needs to have in order to survive for infinite time.InputFirst line of input contains 2 integer numbers n and m separated with a space (1 ≤ n, m ≤ 100, n · m ≠1) — size of the game grid.The next n lines describe grid cells. Each of n strings contains m integer numbers, separated with a space hij (0 ≤ hij ≤ 105) — heights of cells.The next line contains two integer numbers r1 and c1, separated with a space (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — initial location of the rabbit.The last line contains two integers r2 and c2, separated with a space (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — initial location of water.It is guaranteed, that the rabbit and water start in different cells. Columns are numerated from 1 to mfrom left to right. Rows are numerated from 1 to n from top to bottom.OutputIf the rabbit cannot survive in any case, in a single line output -1.Otherwise, in a single line output a single number — the least jump height that rabbit should have in order to survive for infinite time.

题目大意:
开始给出n行m 列的图上每个点的高度,兔子的位置和水的位置
兔子每次可以跳到上下左右格子(要跳到的目标格子和当前格子的高度差小于等于兔子的跳跃高度d),洪水每次可以蔓延到上下左右格子,(洪水的高度大于或等于要蔓延到的格子的高度。问兔子要是能安全(跳到比洪水高的格子),那兔子最小需要多少的跳跃高度?

解题思路:
1.先对洪水所在位置为根,进行bfs,这样可以算出洪水到达每个点的时间。

用一个mp[n][m]的数组记录洪水到达时间。首先初始化所有点为-1;令洪水所在点的时间为0,往上下左右搜索,如果能蔓延就更新时间。bfs完后,所有-1的点表示水蔓延不到的点。

2.二分法(O(logN))和对兔子路径bfs相结合
兔子跳跃高度最大为r=100000,最小为l=0,首先判断中点兔子能否安全,如果安全记录下高度,令r=mid-1,看能否找到更小的高度;如果不安全更新l=mid+1;

那判断在指定高度兔子能否幸存的方法就是对兔子的路径进行bfs,如果兔子能到达mp[i][j]==-1的点,那就安全了。另外对于bfs我们可以剪枝。如果之前已经访问过的点,用visit[n][m]数组记录下来,之后就不用再访问了。

#include<bits/stdc++.h>
using namespace std;const int maxn=105;
int h[maxn][maxn];//原始数据
int mp[maxn][maxn];//水到达每个点的时间
int vis[maxn][maxn];
int vis2[maxn][maxn];
int n,m,r1,c1;//兔子
int r2,c2;//水
int dx[5]={1,0,-1,0},dy[5]={0,1,0,-1};
struct st//描述点的结构体
{int x;//行坐标int y;//列坐标int d;//高度
};void bfs()
{int r,c,d;queue <st> que;que.push(st{r2,c2,0});while(!que.empty()){st t=que.front();que.pop();r=t.x;c=t.y;d=t.d;vis[r][c]=0;mp[r][c]=t.d;for(int i=0;i<4;i++){int x=r+dx[i],y=c+dy[i];if(x<=n&&x>=1&&y<=m&&y>=1&&h[x][y]<=h[r][c]&&vis[x][y]==0&&mp[x][y]==-1){vis[x][y]=1;que.push(st{x,y,d+1});}}}
}int bfs2(int mid)
{int r,c,d;queue <st> que;que.push(st{r1,c1,0});while(!que.empty()){st t=que.front();que.pop();r=t.x;c=t.y;d=t.d;if(mp[r][c]==-1)return 1;vis[r][c]=1;//记录该点已经走过了,下次就不要走了vis2[r][c]=0;//记录该店在不在队列里面,现在出队了for(int i=0;i<4;i++){int x=r+dx[i],y=c+dy[i];if(x<=n&&x>=1&&y>=1&&y<=m&&vis[x][y]==0&&vis2[x][y]==0&&h[x][y]<=mid+h[r][c]&&(mp[x][y]==-1||mp[x][y]>d+1)){que.push(st{x,y,d+1});vis2[x][y]=1;}}}return 0;
}int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&h[i][j]);scanf("%d%d",&r1,&c1);scanf("%d%d",&r2,&c2);memset(mp,-1,sizeof(mp));bfs();int ans=-1;int l=0,r=100000;while(l<=r) {int mid=(l+r)>>1;memset(vis,0,sizeof(vis));memset(vis2,0,sizeof(vis2));if(bfs2(mid)){ans=mid;r=mid-1;}else{l=mid+1;}}printf("%d\n",ans);return 0;
}

C - Survive the flood URAL - 2113相关推荐

  1. 考研计算机考点精讲课程笔记新东方,新东方词汇笔记非常完整版

    第3课 倒装:倒装有全部倒装和部分倒装. 谓语部分所有单词都放在主语前是全部倒装.谓语的一部分放在主语的前面是部分倒装. 谓语中的一部分通常是指:1.系动词:2.助动词:3.情态动词. 全部倒装的五条 ...

  2. Ural 1018 (树形DP+背包+优化)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给 ...

  3. linux测试网页装载时间,如何用Flood测试Web服务器响应时间

    当你设置好服务器投入使用后,你最关心的事莫过于服务器的性能了.你可以用一些手动的方法进行测试,但手动方法有很多局限性. 先不论手工测试方法所投入的时间和精力问题,用手工方法测试的一大不足就是它不容易揭 ...

  4. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 924  Solved: 351 [Submit][S ...

  5. DDOS SYN Flood攻击、DNS Query Flood, CC攻击简介——ddos攻击打死给钱。限网吧、黄网、博彩,,,好熟悉的感觉有木有...

    摘自:https://zhuanlan.zhihu.com/p/22953451 首先我们说说ddos攻击方式,记住一句话,这是一个世界级的难题并没有解决办法只能缓解 DDoS(Distributed ...

  6. LeetCode Number of Islands(flood fill)

    问题:给出一个由0和1组成的二维网格图(m*n),1表示陆地,0表示水.要求统计有多少块陆地 思路:常见的flood fill算法有三种,深度优先搜索.广度优先搜索以及广度扫描法.广度扫描法其实原理与 ...

  7. flood fill算法

    flood fill算法实现有三种形式 1.depth first search 2.breadth first search 3.breadth first scan 基本思想是找到还没有分配com ...

  8. usaco The Castle(flood fill)

    问题:城堡有n*m个方块组成,方块四周可能有墙,分别用1(W),2(N),4(E),8(S)来表示,每个方块由一个数字来表示,由四周的分布的墙值和来表示.要求求出城堡有多少个房间,最大房间的大小及删除 ...

  9. 如何应対syn flood

    syn flood的原理就不介绍了 主要总结一下解决方法: 防火墙禁IP 内核参数的调优 Linux内核参数调优主要有下面三个: 增大tcp_max_syn_backlog 减小tcp_synack_ ...

最新文章

  1. pandas计算dataframe两列数据值相等的行号、取出DataFrame中两列值相等的行号
  2. 物理机与虚拟机IP互ping通,而互ping主机名不通
  3. golang 截取字符串
  4. gdb工作原理(一)
  5. 学生时代的最后一个新年,请一定要做这五件事...
  6. CCNP实验+笔记(完整版)
  7. Virtual Box中Centos虚拟机设置静态IP
  8. 移动web——touch事件介绍
  9. concurrently同时开启多个监听服务
  10. 概率逗号分号_概率里面的逗号
  11. python--字符串
  12. shiro权限框架中五张基本数据表
  13. GE PLC的EGD协议通信
  14. 已经阻止语音服务器,关闭语音服务器
  15. STLINK : Warning: Connection to device 0x413 is lost
  16. 隐私政策说明 - 掌上软考答题速记系统
  17. Pycharm关闭错误提示,关闭“This inspection detects shadowing names defined in outer scopes.”等
  18. Pr:Lumetri范围
  19. qq邮箱993服务器地址,ios邮箱绑定qq邮箱提示993服务器连接超时
  20. Android音乐播放器开发(2)—登录

热门文章

  1. Excel打开csv或文本文件乱码怎么办?
  2. 基于java的电脑配件报价网站系统
  3. 考博英语-连接词What与although的用法
  4. 计算机科学与技术专业有哪些课程,计算机科学与技术专业课程有哪些
  5. R语言学习笔记(1)——建立list
  6. 外泌体的三种分离方法及其临床意义
  7. 激光投影电视和液晶电视哪个好 激光投影电视和液晶电视什么区别
  8. 深度探索C++对象模型 学习笔记 第二章 构造函数语意学
  9. 小程序:七巧板拼图世界图案大全
  10. 项目8 数据库的安全性维护