您的位置:首页 > 运维架构

1687: [Usaco2005 Open]Navigating the City 城市交通

2015-04-10 14:35 363 查看

1687: [Usaco2005 Open]Navigating the City 城市交通

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 94 Solved: 73
[Submit][Status][Discuss]

Description

由于牛奶市场的需求,奶牛必须前往城市,但是唯一可用的交通工具是出租车.教会奶牛如何在城市里打的.
给出一个城市地图,东西街区E(1≤E≤40),南北街区N(1≤N≤30).制作一个开车指南给出租车司机,告诉他如何从起点(用S表示)到终点(用E表示).每一个条目用空格分成两部分,第一个部分是方向(N,E,S,W之一),第二个是一个整数,表示要沿着这个方向开几个十字路口.如果存在多条路线,你应该给出最短的.数据保证,最短的路径存在且唯一. 地图中“+”表示十字路口,道路用“I”和“一”表示.建筑和其他设施用“.”表示.下面是一张地图:



出租车可以沿着东,北,西,北,东开两个十字路口,以此类推.具体将由样例给出

Input

第1行:两个用空格隔开的整数N和E.

第2到2N行:每行有2E-I个字符,表示地图.

Output

每行有一个表示方向的字母和一个表示要开几个十字路口的数字表示.

Sample Input



Sample Input

Sample Output

E 1

N 1

W 1

N 1

E 2

S 1

E 3

S 1

W 1

HINT

Source

Silver

题解:一开始没仔细看题的时候以为是灌水法。。。可是后来想到貌似不见得就一条路径,而且题目中貌似说了要最短路径

不知道正解是啥,反正我还是一如既往的逗比——以地图上每个+符号(S、E也算)为节点,建立无向图,然后就成了求最短路径的问题了,然后就是神烦的边spfa边记录路径了,话说这道水题居然写了我好久唉TT

/**************************************************************
Problem: 1687
User: HansBug
Language: Pascal
Result: Accepted
Time:32 ms
Memory:2600 kb
****************************************************************/

const dir:array[1..4] of char=('N','E','S','W');
type
point=^node;
node=record
g,w,q:longint;
next:point;
end;
var
i,j,k,l,m,n,x0,y0,x1,y1,x,y,f,r,s,t,v:longint;
map:array[0..100,0..100] of longint;
a:array[0..10000] of point;
b,c,g:array[0..10000] of longint;
d:array[0..100000] of longint;
e,h:array[0..100000,1..2] of longint;
ch:char;
p:point;
procedure add(x,y,z,t:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;
p^.q:=t;p^.next:=a[x];a[x]:=p;
end;
begin
fillchar(map,sizeof(map),0);
readln(n,m);
n:=n*2-1;m:=m*2-1;
for i:=1 to n do
begin
for j:=1 to m do
begin
read(ch);
case upcase(ch) of
'S':begin
map[i,j]:=-1;
x0:=i;y0:=j;
end;
'E':begin
map[i,j]:=-1;
x1:=i;y1:=j;
end;
'.':map[i,j]:=0;
'-':map[i,j]:=-2;
'|':map[i,j]:=-3;
'+':map[i,j]:=-1;
end;
end;
readln;
end;
s:=0;t:=0;v:=0;
for i:=1 to n do
for j:=1 to m do
if map[i,j]=-1 then
begin
inc(v);
map[i,j]:=v;
if (x0=i) and (y0=j) then s:=v;
if (x1=i) and (y1=j) then t:=v;
end;
for i:=1 to n do
for j:=1 to m do
begin
if map[i-1,j]<>0 then
begin
x:=i-1;y:=j;
while map[x,y]=-3 do dec(x);
if map[x,y]>0 then add(map[i,j],map[x,y],1,1);
end;
if map[i+1,j]<>0 then
begin
x:=i+1;y:=j;
while map[x,y]=-3 do inc(x);
if map[x,y]>0 then add(map[i,j],map[x,y],1,3);
end;
if map[i,j-1]<>0 then
begin
x:=i;y:=j-1;
while map[x,y]=-2 do dec(y);
if map[x,y]>0 then add(map[i,j],map[x,y],1,4);
end;
if map[i,j+1]<>0 then
begin
x:=i;y:=j+1;
while map[x,y]=-2 do inc(y);
if map[x,y]>0 then add(map[i,j],map[x,y],1,2);
end;
end;
fillchar(g,sizeof(g),0);
fillchar(c,sizeof(c),0);
fillchar(b,sizeof(b),0);
fillchar(e,sizeof(e),0);
d[1]:=s;f:=1;r:=2;g[s]:=1;c[s]:=1;
while f<r do
begin
p:=a[d[f]];
while p<>nil do
begin
if (c[p^.g]=0) or ((c[p^.g]>0) and (c[p^.g]>(c[d[f]]+p^.w))) then
begin
c[p^.g]:=c[d[f]]+p^.w;
b[p^.g]:=d[f];
e[p^.g,1]:=p^.q;e[p^.g,2]:=p^.w;
if g[p^.g]=0 then
begin
g[p^.g]:=1;
d[r]:=p^.g;
inc(r);
end;
end;
p:=p^.next;
end;
inc(f);
g[d[f]]:=0;
end;
for i:=1 to v do dec(c[i]);
i:=0;fillchar(h,sizeof(h),0);
while t<>0 do
begin
inc(i);
h[i,1]:=e[t,1];
h[i,2]:=e[t,2];
t:=b[t];
end;
v:=i-1;
for i:=1 to v div 2 do
begin
j:=h[i,1];h[i,1]:=h[v+1-i,1];h[v+1-i,1]:=j;
j:=h[i,2];h[i,2]:=h[v+1-i,2];h[v+1-i,2]:=j;
end;
inc(v);h[v,1]:=5;h[v,2]:=0;
j:=h[1,1];k:=h[1,2];
for i:=2 to v do
begin
if h[i,1]=j then
k:=k+h[i,2]
else
begin
writeln(dir[j],' ',k);
k:=h[i,2];j:=h[i,1];
end;
end;
readln;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: