1671: [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 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
还是灌水法不解释
const dir:array[..,..] of longint=((,),(-,),(,),(,-));
type arr=array[..] of longint;
var
i,j,k,l,m,n,x0,x1,y0,y1,head,tot,ans:longint;
a,b,h:array[..,..] of longint;
d:array[..,..] of longint;
c,e:arr;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
procedure bfs(x,y:longint;var c:arr);
var f,r,xx,yy,i:longint;
begin
f:=;r:=;d[,]:=x;d[,]:=y;d[,]:=;b[x,y]:=;
while f<r do
begin
for i:= to do
begin
xx:=d[f,]+dir[i,];
yy:=d[f,]+dir[i,];
if b[xx,yy]= then continue;
b[xx,yy]:=;
d[r,]:=d[f,]+;
d[r,]:=xx;d[r,]:=yy;
if h[xx,yy]> then
begin
c[h[xx,yy]]:=d[r,];
end;
inc(r);
end;
inc(f);
end;
end;
begin
randomize;tot:=;
readln(m,n);
for i:= to n+ do
begin
a[i,m+]:=;
a[i,]:=;
end;
for i:= to m+ do
begin
a[n+,i]:=;
a[,i]:=;
end;
fillchar(h,sizeof(h),);
for i:= to n do
for j:= to m do
begin
read(a[i,j]);
case a[i,j] of
:begin
x0:=i;y0:=j;
end;
:begin
x1:=i;y1:=j;
a[i,j]:=;
end;
:begin
inc(tot);
h[i,j]:=tot;
a[i,j]:=;
end;
end;
if ((j mod )=) or (j=m) then readln;
end;
for i:= to n+ do
for j:= to m+ do
b[i,j]:=a[i,j];
fillchar(c,sizeof(c),);
bfs(x0,y0,c);
for i:= to n+ do
for j:= to m+ do
b[i,j]:=a[i,j];
fillchar(e,sizeof(e),);
bfs(x1,y1,e);
ans:=maxlongint;
for i:= to tot do
begin
if (c[i]=) or (e[i]=) then continue;
ans:=min(ans,(e[i]+c[i]))
end;
writeln(ans);
readln;
end.
1671: [Usaco2005 Dec]Knights of Ni 骑士的更多相关文章
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 (bfs)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1671 题解: 按题意分别从贝茜和骑士bfs然后meet_in_middle.. 把一个逗号 ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...
- POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- BZOJ_1671_[Usaco2005 Dec]Knights of Ni 骑士_BFS
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- [Usaco2005 Dec]Knights of Ni 骑士
Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through t ...
- BZOJ1671: [Usaco2005 Dec]Knights of Ni
1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 175 Solved: 107[Su ...
随机推荐
- Win10下CISCO VPN Client无法安装解决方案
Cisco vpn client 在Windows升级到Windows 10 之后无法正常安装使用,在这种情况下:1.先安装Dell SonicWALL Global VPN Client(GVCSe ...
- Android EditText获取焦点和失去焦点监听事件
实现方法也很简单.那就是绑定OnFocusChangeListener事件.实现onFocusChange(View v, boolean hasFocus) 方法.第二个参数就是判断得到焦点或失去焦 ...
- AIX上面Oracle数据库相关启动
1,启动停止Oracle实例 (1) su -oracle (2) echo $ORACLE_SID (3) sqlplus /nolog //以不登录到数据库的方式进入sqlplus环境 (4) c ...
- 在ASP.NET Core下使用SignalR技术
一.前言 上次我们讲到过如何在ASP.NET Core中使用WebSocket,没有阅读过的朋友请参考 WebSocket in ASP.NET Core 文章 .这次的主角是SignalR它为我们提 ...
- IE6支持透明PNG图片解决方案:DD_belatedPNG.js
DD_belatedPNG.js 是一个能是IE6支持p显示ng透明图片,而且还支持背景循环(background-repeat)和定位(backgrond-position) ,支持focus,Ho ...
- localStorage , sessionStorage ,cookie 使用介绍
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 3783 ZOJ
ZOJ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- oracle decode函数的用法
含义解释: decode(字段,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 用法如下:IF 字段=值1 返回 返回值1ELSIF 字段=值2 返回 返回值2 ......ELSIF ...
- DataReader的用法程序简析
// 2015/07/05 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- Java中显示图片的方法
最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...