1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 254 Solved: 163
[Submit][Status][Discuss]
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.
Input
Output
Sample Input
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
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)