1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 254 Solved: 163
Description
Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木并带一棵给他们.当然,贝茜想早点离开这可怕的森林,于是她必须尽快完成骑士们给的任务,贝茜随身带着这片森林的地图,地图上的森林被放入了直角坐标系,并按x,y轴上的单位长度划分成了W×H(1≤W,H≤1000)块,贝茜在地图上查出了她自己以及骑士们所在的位置,当然地图上也标注了她所需要的灌木生长的区域.某些区域是不能通过的(比如说沼泽地,悬崖,以及食人兔的聚居地).在没有找到灌木之前,贝茜不能通过骑士们所在的那个区域,为了确保她自己不会迷路,贝茜只向正北、正东、正南、正西四个方向移动(注意,她不会走对角线).她要走整整一天,才能从某块区域走到与它相邻的那块区域. 输入数据保证贝茜一定能完成骑士的任务.贝茜希望你能帮她计算一下,她最少需要多少天才可脱离这可怕的地方?
Input
第1行输入2个用空格隔开的整数,即题目中提到的W、H.
接下来输入贝茜持有的地图,每一行用若干个数字代表地图上对应行的地形.第1行描述了地图最北的那一排土地;最后一行描述的则是最南面的.相邻的数字所对应的区域是相邻的.如果地图的宽小于或等于40,那每一行数字恰好对应了地图上的一排土地.如果地图的宽大于40,那每行只会给出40个数字,并且保证除了最后一行的每一行都包含恰好40个数字.没有哪一行描述的区域分布在两个不同的行里.
地图上的数字所对应的地形:
0:贝茜可以通过的空地
1:由于各种原因而不可通行的区域
2:贝茜现在所在的位置
3:骑士们的位置
4:长着贝茜需要的灌木的土地
Output
输出一个正整数D,即贝茜最少要花多少天才能完成骑士们给的任务.
Sample Input
8 4 4 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 2 1 1 3 0 4 0 0 0 0 4 1 1 1 0 INPUT DETAILS: Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.
Sample Output
11
HINT
这片森林的长为8,宽为4.贝茜的起始位置在第3行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
Source
题解:又是一道灌水法。。。。usaco灌水题真多= =
不过幸亏骑士的个数貌似就1个。。。要是骑士再一大堆的话那就惨了TT
还是灌水法不解释
1 const dir:array[1..4,1..2] of longint=((1,0),(-1,0),(0,1),(0,-1));
2 type arr=array[0..1000005] of longint;
3 var
4 i,j,k,l,m,n,x0,x1,y0,y1,head,tot,ans:longint;
5 a,b,h:array[0..1001,0..1001] of longint;
6 d:array[0..1000005,1..3] of longint;
7 c,e:arr;
8 function min(x,y:longint):longint;
9 begin
10 if x<y then min:=x else min:=y;
11 end;
12 procedure bfs(x,y:longint;var c:arr);
13 var f,r,xx,yy,i:longint;
14 begin
15 f:=1;r:=2;d[1,1]:=x;d[1,2]:=y;d[1,3]:=0;b[x,y]:=1;
16 while f<r do
17 begin
18 for i:=1 to 4 do
19 begin
20 xx:=d[f,1]+dir[i,1];
21 yy:=d[f,2]+dir[i,2];
22 if b[xx,yy]=1 then continue;
23 b[xx,yy]:=1;
24 d[r,3]:=d[f,3]+1;
25 d[r,1]:=xx;d[r,2]:=yy;
26 if h[xx,yy]>0 then
27 begin
28 c[h[xx,yy]]:=d[r,3];
29 end;
30 inc(r);
31 end;
32 inc(f);
33 end;
34 end;
35 begin
36 randomize;tot:=0;
37 readln(m,n);
38 for i:=0 to n+1 do
39 begin
40 a[i,m+1]:=1;
41 a[i,0]:=1;
42 end;
43 for i:=0 to m+1 do
44 begin
45 a[n+1,i]:=1;
46 a[0,i]:=1;
47 end;
48 fillchar(h,sizeof(h),0);
49 for i:=1 to n do
50 for j:=1 to m do
51 begin
52 read(a[i,j]);
53 case a[i,j] of
54 2:begin
55 x0:=i;y0:=j;
56 end;
57 3:begin
58 x1:=i;y1:=j;
59 a[i,j]:=1;
60 end;
61 4:begin
62 inc(tot);
63 h[i,j]:=tot;
64 a[i,j]:=0;
65 end;
66 end;
67 if ((j mod 40)=0) or (j=m) then readln;
68 end;
69 for i:=0 to n+1 do
70 for j:=0 to m+1 do
71 b[i,j]:=a[i,j];
72 fillchar(c,sizeof(c),0);
73 bfs(x0,y0,c);
74 for i:=0 to n+1 do
75 for j:=0 to m+1 do
76 b[i,j]:=a[i,j];
77 fillchar(e,sizeof(e),0);
78 bfs(x1,y1,e);
79 ans:=maxlongint;
80 for i:=1 to tot do
81 begin
82 if (c[i]=0) or (e[i]=0) then continue;
83 ans:=min(ans,(e[i]+c[i]))
84 end;
85 writeln(ans);
86 readln;
87 end.
- No.3 啥是数据运营(三):思维方式
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(7)-MVC与EasyUI DataGrid
- MySQL数据同步【双主热备】
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(4)-创建项目解决方案
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(3)-漂亮系统登陆界面
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(2)-easyui构建前端页面框架[附源码]
- 扬言毁灭人类的索菲亚再一次挑战了人类
- SQL Server 存储过程生成insert语句
- silverlight 背景透明
- .Net 转战 Android 4.4 日常笔记(1)--工具及环境搭建
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(33)-MVC 表单验证
- 如何利用深度学习识别千万张图片?
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(32)-swfupload多文件上传[附源码]
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 个性化终端 | zsh bash oh-my-zsh
- VirtualBox无限嵌套方法 | 虚拟机套娃
- Win10 LTSC 激活方法 | Win10专业版(永久激活)长期服务版 LTSC 2019密钥
- Nginx代理以及面向未来的HTTP
- git的基本使用
- 一、玩转Git三剑客-Git基础
- Docker和k8s的故障排除和监控利器 Weave-Scope服务
- docker 配置Consul+registrator实时服务发现
- 使用Docker搭建Zookeeper集群
- GDCRNATools内置的gdc-client不好用
- 字符编码与字符串表达式
- 关于批次效应矫正后出现负值
- 在SAP WebIDE里使用Fiori Elements快速开发SAP UI5应用
- SAP Fiori Elements里的Smart Table工作原理解析
- SAP Fiori Elements里Smart Link工作原理介绍