洪水_纪中1235_bfs
来源:互联网 发布:js时间戳有什么作用 编辑:程序博客网 时间:2024/04/30 05:43
Description
一天, 一个画家在森林里写生,突然爆发了山洪,他需要尽快返回住所中,那里是安
全的。
森林的地图由R行C列组成,空白区域用点“.”表示,洪水的区域用“*”表示,而
岩石用“X”表示,另画家的住所用“D”表示,画家用“S”表示。
有以下几点需要说明:
1、 每一分钟画家能向四个方向移动一格(上、下、左、右)
2、 每一分钟洪水能蔓延到四个方向的相邻格子(空白区域)
3、 洪水和画家都不能通过岩石区域
4、 画家不能通过洪水区域(同时也不行,即画家不能移到某个格子,该格子在画家达到的同时被洪水蔓延到了,这也是不允许的)
5、 洪水蔓不到画家的住所。
给你森林的地图,编写程序输出最少需要花费多长时间才能从开始的位置赶回家中。
Input
输入第一行包含两个整数R和C(R,C<=50)。
接下来R行每行包含C个字符(“.”、“*”、“X”、“D”或“S”)。地图保证只有一个“D”和一个“S”。
Output
输出画家最快安全到达住所所需的时间,如果画家不可能安全回家则输出“KAKTUS”。
Sample Input
输入1:
3 3
D.*
…
.S.
输入2:
3 3
D.*
…
..S
输入3:
3 6
D…*.
.X.X..
….S.
Sample Output
输出1:
3
输出2:
KAKTUS
输出3:
6
题解:
昨天晚上的专题就是暴力搜索,于是乎就想到了搜索
记录洪水和画家的位置分别bfs两次,记录当前点的深度dep[i,j]
若同一位置,画家的深度>=洪水的深度就KAKTUS,否则输出到达房子时的深度
这里是槽点:
- 这尼玛不止一个洪水的源头啊!!!
- 这尼玛可能会有没有洪水的情况啊啊啊!!!
代码/pas:
type axis=record x,y,w:longint; end; state=record head,tail:longint; s:array[1..3000]of axis; end;const dx:array[1..4]of integer=(-1,1,0,0); dy:array[1..4]of integer=(0,0,-1,1);var n,m:longint; map,t,g:array[-1..100,-1..100]of longint; sop,fop:axis; a,b:state;procedure bfs;var i,x,y:longint;begin t[fop.x,fop.y]:=maxlongint; repeat inc(b.head); for i:=1 to 4 do begin x:=b.s[b.head].x+dx[i]; y:=b.s[b.head].y+dy[i]; if (map[x,y]=1)and(t[x,y]=0) then begin inc(b.tail); b.s[b.tail].x:=x; b.s[b.tail].y:=y; b.s[b.tail].w:=b.s[b.head].w+1; t[x,y]:=b.s[b.tail].w; end; end; until b.head>=b.tail; fillchar(a,sizeof(a),0); a.tail:=1; a.s[1]:=sop; repeat; inc(a.head); if map[a.s[a.head].x,a.s[a.head].y]=2 then begin writeln(a.s[a.head].w); halt; end; for i:=1 to 4 do begin x:=a.s[a.head].x+dx[i]; y:=a.s[a.head].y+dy[i]; if (map[x,y]in[1,2])and((t[x,y]>a.s[a.head].w+1)or(t[x,y]=0))and(g[x,y]=0) then begin inc(a.tail); a.s[a.tail].x:=x; a.s[a.tail].y:=y; a.s[a.tail].w:=a.s[a.head].w+1; g[x,y]:=a.s[a.tail].w; end; end; until a.head>=a.tail;end;procedure init;var i,j:longint; c:char;begin fillchar(map,sizeof(map),0); readln(n,m); for i:=1 to n do begin for j:=1 to m do begin read(c); if c='X' then map[i,j]:=0; if c='.' then map[i,j]:=1; if c='D' then map[i,j]:=2; if c='S' then begin map[i,j]:=3; sop.x:=i; sop.y:=j; end; if c='*' then begin map[i,j]:=4; inc(b.tail); b.s[b.tail].x:=i; b.s[b.tail].y:=j; end; end; readln; end;end;begin init; bfs; writeln('KAKTUS');end.
1 0
- 洪水_纪中1235_bfs
- 回家_纪中1445_bfs+二分
- 位图_纪中3076_bfs
- 跟踪_纪中4805_bfs
- 数字生成游戏_纪中2570_bfs
- 洪水+纪中1235+bfs+水
- Number_纪中1781_bfs+hash
- codevs 1215_迷宫_bfs
- codevs 2919_选择题_bfs
- SSL 2352_面积_bfs
- jzoj 3450_山峰_bfs
- 洛谷 1162_填涂颜色_bfs
- 洛谷 1443_马的遍历_bfs
- 封锁阳光大学_洛谷1330_bfs
- codevs 2806_红与黑_bfs
- codevs 1099_字串变换_bfs
- codevs 1026_逃跑的拉尔夫_bfs
- codevs 1225_八数码难题_bfs+hash
- Linux系统PWM驱动
- jquery $(this).attr $(this).val方法使用介绍
- 使用TextView实现消息提醒(徽章控件)
- java-框架-OkHttp
- 枚举用法
- 洪水_纪中1235_bfs
- wind下compser安装配置
- 常用网站
- ScrollView嵌套ListView冲突问题的最优解决方案
- Meeting Rooms II
- 安装storm1.0.1后worker启动不起来 nimbus.log中显示executor循环not alive
- Android杂谈(7)搞一搞Fragment+官方API底部导航制作
- JSON拼接字符串
- schedule调度相关