日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

5793. 【NOIP2008模拟】小S练跑步

發(fā)布時(shí)間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 5793. 【NOIP2008模拟】小S练跑步 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Description

? ? ? ?小S是一個(gè)愛鍛煉的孩子,他在放假期間堅(jiān)持在A公園練習(xí)跑步。
??????但不久后,他就開始為在重復(fù)的地點(diǎn)練習(xí)感到厭煩了,他就打算去B公園跑步。
??????但是小S由于沒有去過(guò)B公園,他不知道B公園是否適合練習(xí)跑步,又不知道在B公園怎樣跑是最優(yōu)的。所以小S就去B公園進(jìn)行了一番勘測(cè)。
??????小S在進(jìn)行了一番勘測(cè)后,畫出了一張地圖,地圖每一個(gè)位置上都辨識(shí)了小S到達(dá)該位置后不能往哪一個(gè)方位移動(dòng)。其中有5種表示的符號(hào):“U”代表不能向上移動(dòng),“D”代表不能向下移動(dòng),“L”代表不能向左移動(dòng),“R”代表不能向右移動(dòng),如果該位置有障礙物,小S到達(dá)那里后無(wú)法繼續(xù)訓(xùn)練,就用“S”來(lái)代表。整個(gè)公園共有n行m列,小S會(huì)從第1行第1列出發(fā),到第n行第m列結(jié)束他的練習(xí)。
??????現(xiàn)在小S想要知道,從起點(diǎn)(即第1行第1列)到終點(diǎn)(第n行第m列),途中最少要改變幾次方向(即上一次移動(dòng)的方向和這一次移動(dòng)的方向不一樣)?
??????注意:小S如在訓(xùn)練途中離開公園(即地圖范圍),則算是結(jié)束訓(xùn)練。

Input

? ? ??第1行兩個(gè)整數(shù)n和m,它們的定義請(qǐng)看【題目描述】。
??????第2~n+1行,每行有m個(gè)字符,表示小S的移動(dòng)方向。

Output

? ? ??如果小S從第1行第1列出發(fā)無(wú)論如何都到達(dá)不了第n行第m列,輸出“No Solution”,否則輸出小S途中最少要改變方向的次數(shù)。
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.

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zyx-crying/p/9457099.html

總結(jié)

以上是生活随笔為你收集整理的5793. 【NOIP2008模拟】小S练跑步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。