5793. 【NOIP2008模拟】小S练跑步
生活随笔
收集整理的這篇文章主要介紹了
5793. 【NOIP2008模拟】小S练跑步
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
? ? ? ?小S是一個愛鍛煉的孩子,他在放假期間堅持在A公園練習跑步。??????但不久后,他就開始為在重復的地點練習感到厭煩了,他就打算去B公園跑步。
??????但是小S由于沒有去過B公園,他不知道B公園是否適合練習跑步,又不知道在B公園怎樣跑是最優的。所以小S就去B公園進行了一番勘測。
??????小S在進行了一番勘測后,畫出了一張地圖,地圖每一個位置上都辨識了小S到達該位置后不能往哪一個方位移動。其中有5種表示的符號:“U”代表不能向上移動,“D”代表不能向下移動,“L”代表不能向左移動,“R”代表不能向右移動,如果該位置有障礙物,小S到達那里后無法繼續訓練,就用“S”來代表。整個公園共有n行m列,小S會從第1行第1列出發,到第n行第m列結束他的練習。
??????現在小S想要知道,從起點(即第1行第1列)到終點(第n行第m列),途中最少要改變幾次方向(即上一次移動的方向和這一次移動的方向不一樣)?
??????注意:小S如在訓練途中離開公園(即地圖范圍),則算是結束訓練。
Input
? ? ??第1行兩個整數n和m,它們的定義請看【題目描述】。??????第2~n+1行,每行有m個字符,表示小S的移動方向。
Output
? ? ??如果小S從第1行第1列出發無論如何都到達不了第n行第m列,輸出“No Solution”,否則輸出小S途中最少要改變方向的次數。Solutions
裸的廣搜。
?
代碼
?
1 const 2 dx:array [1..4] of longint=(-1,1,0,0); 3 dy:array [1..4] of longint=(0,0,-1,1); 4 var 5 n,m:longint; 6 a:array [0..501,0..501] of longint; 7 d:array [0..501,0..501,0..4] of longint; 8 list:array [0..3000001,1..4] of longint; 9 procedure init; 10 var 11 i,j:longint; 12 c:char; 13 begin 14 readln(n,m); 15 for i:=1 to n do 16 begin 17 for j:=1 to m do 18 begin 19 read(c); 20 if c='U' then a[i,j]:=1; 21 if c='D' then a[i,j]:=2; 22 if c='L' then a[i,j]:=3; 23 if c='R' then a[i,j]:=4; 24 if c='S' then a[i,j]:=5; 25 end; 26 readln; 27 end; 28 end; 29 30 function min(o,p:longint):longint; 31 begin 32 if o<p then exit(o); 33 exit(p); 34 end; 35 36 function fd(x,y,w,tot:longint):boolean; 37 begin 38 if (a[x,y]=5) and ((x<>n) or (y<>m)) then exit(false); 39 if (x>n) or (x<1) then exit(false); 40 if (y>m) or (y<1) then exit(false); 41 if tot>=d[x,y,w] then exit(false); 42 if tot>=d[x,y,0]+1 then exit(false); 43 d[x,y,0]:=min(d[x,y,0],tot); 44 d[x,y,w]:=tot; 45 exit(true); 46 end; 47 48 procedure bfs; 49 var 50 i,j,k,head,tail,x,y,z,w,xx,yy,ww:longint; 51 begin 52 head:=0; tail:=0; 53 for i:=0 to 501 do 54 for j:=0 to 501 do 55 for k:=0 to 4 do 56 d[i,j,k]:=maxlongint div 2; 57 d[1,1,1]:=0; d[1,1,2]:=0; 58 d[1,1,3]:=0; d[1,1,4]:=0; 59 for i:=1 to 4 do 60 if a[1,1]<>i then 61 begin 62 inc(tail); 63 list[tail,1]:=1; list[tail,2]:=1; 64 list[tail,3]:=i; 65 end; 66 while head<tail do 67 begin 68 inc(head); 69 x:=list[head,1]; y:=list[head,2]; 70 z:=list[head,3]; w:=list[head,4]; 71 for i:=1 to 4 do 72 if a[x,y]<>i then 73 begin 74 xx:=x+dx[i]; yy:=y+dy[i]; ww:=w; 75 if z<>i then inc(ww); 76 if not fd(xx,yy,i,ww) then continue; 77 inc(tail); 78 list[tail,1]:=xx; list[tail,2]:=yy; 79 list[tail,3]:=i; list[tail,4]:=ww; 80 end; 81 end; 82 end; 83 84 procedure print; 85 var 86 ans,i:longint; 87 begin 88 ans:=d[0,0,0]; 89 for i:=1 to 4 do 90 ans:=min(ans,d[n,m,i]); 91 if ans<>d[0,0,0] then writeln(ans) 92 else writeln('No Solution'); 93 end; 94 95 begin 96 assign(input,'run.in'); 97 assign(output,'run.out'); 98 reset(input); 99 rewrite(output); 100 init; 101 bfs; 102 print; 103 close(input); 104 close(output); 105 end.
?
?
?
轉載于:https://www.cnblogs.com/zyx-crying/p/9457099.html
總結
以上是生活随笔為你收集整理的5793. 【NOIP2008模拟】小S练跑步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个女人独立微信网名
- 下一篇: 威风堂堂是谁唱的啊?