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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1180 诡异的楼梯

發(fā)布時(shí)間:2024/10/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1180 诡异的楼梯 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

詭異的樓梯

Time Limit : 2000/1000ms (Java/Other)???Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 19???Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Hogwarts正式開學(xué)以后,Harry發(fā)現(xiàn)在Hogwarts里,某些樓梯并不是靜止不動(dòng)的,相反,他們每隔一分鐘就變動(dòng)一次方向.
比如下面的例子里,一開始樓梯在豎直方向,一分鐘以后它移動(dòng)到了水平方向,再過一分鐘它又回到了豎直方向.Harry發(fā)現(xiàn)對(duì)他來說很難找到能使得他最快到達(dá)目的地的路線,這時(shí)Ron(Harry最好的朋友)告訴Harry正好有一個(gè)魔法道具可以幫助他尋找這樣的路線,而那個(gè)魔法道具上的咒語,正是由你纂寫的.

Input

測(cè)試數(shù)據(jù)有多組,每組的表述如下: 第一行有兩個(gè)數(shù),M和N,接下來是一個(gè)M行N列的地圖,'*'表示障礙物,'.'表示走廊,'|'或者'-'表示一個(gè)樓梯,并且標(biāo)明了它在一開始時(shí)所處的位置:'|'表示的樓梯在最開始是豎直方向,'-'表示的樓梯在一開始是水平方向.地圖中還有一個(gè)'S'是起點(diǎn),'T'是目標(biāo),0<=M,N<=20,地圖中不會(huì)出現(xiàn)兩個(gè)相連的梯子.Harry每秒只能停留在'.'或'S'和'T'所標(biāo)記的格子內(nèi).

Output

只有一行,包含一個(gè)數(shù)T,表示到達(dá)目標(biāo)的最短時(shí)間.
注意:Harry只能每次走到相鄰的格子而不能斜走,每移動(dòng)一次恰好為一分鐘,并且Harry登上樓梯并經(jīng)過樓梯到達(dá)對(duì)面的整個(gè)過程只需要一分鐘,Harry從來不在樓梯上停留.并且每次樓梯都恰好在Harry移動(dòng)完畢以后才改變方向.

Sample Input

5 5 **..T **.*. ..|.. .*.*. S....

Sample Output

7

Hint

?

Source Gardon-DYGG Contest 1 Recommend JGShining 思路: 這個(gè)題目思路挺簡(jiǎn)單的,就是BFS 加優(yōu)先隊(duì)列,但是我卻調(diào)試到了凌晨一點(diǎn)才過,本人覺得 挺坑爹,關(guān)鍵在于在樓梯處怎么處理,因?yàn)榭梢栽谠幍却钡娇梢赃^去為止,看別人可以用 普通隊(duì)列,我沒怎么知道怎么去做 代碼: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
char map[22][22];
int hash[22][22];
int n,m;
int move[4][2] = {1,0,-1,0,0,1,0,-1};
int sx,sy,tx,ty;
struct Node
{
??? int x,y;
??? int time;
??? friend bool operator < (const Node & a,const Node & b)
??? {
??????? return a.time > b.time;
??? }
};
void BFS()
{
??? priority_queue < Node > q;
??? Node top;top.x = sx;top.y = sy;top.time = 0;
??? q.push(top);
??? while(!q.empty())
??? {
??????? Node temp;
??????? temp = q.top();q.pop();
??????? //printf("%d %d have %d\n",temp.x,temp.y,temp.time);
??????? if(temp.x == tx && temp.y == ty)
??????? {
?????????????? printf("%d\n",temp.time);
?????????????? break;
??????? }
??????? for(int i = 0;i < 4;i ++)
??????? {
??????????? int x = temp.x + move[i][0];int y = temp.y + move[i][1];
??????????? //printf("%d??????????????? %d %c\n",x,y,map[x][y]);
??????????? int time = temp.time;
??????????? if(map[x][y] != '*' && hash[x][y] == 0 && x >= 1 && x <= n
??????????? && y >= 1 && y <= m)
??????????? {
??????????????? if(map[x][y] == '.' || map[x][y] == 'T')
??????????????? {
??????????????????? Node xin;xin.x = x;xin.y = y;xin.time = time + 1;
??????????????????? hash[x][y] = 1;
??????????????????? //if(temp.x == 2 && temp.y == 5)
??????????????????? //printf("%d %d %c\n",x,y,map[x][y]);
??????????????????? q.push(xin);
??????????????? }
???????????? if(hash[x + move[i][0]][y + move[i][1]] == 0)
????????????? {
??????????????? if(map[x][y] == '|')
??????????????? {
??????????????????? //printf("%d? %d? %c\n",x,y,map[x][y]);
??????????????????? //hash[x][y] = 1;
??????????????????? if(time % 2 == 0)
???????????????????? {
??????????????????????? if(x == temp.x)
??????????????????????? {
??????????????????????????? time = time + 2;
??????????????????????????? y += move[i][1];
??????????????????????? }
??????????????????????? if(y == temp.y)
??????????????????????? {
??????????????????????????? time = time + 1;
??????????????????????????? x += move[i][0];
??????????????????????? }
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????? if(time % 2 == 1)
??????????????????? {
??????????????????????? if(x == temp.x)
??????????????????????? {
??????????????????????????? time = time + 1;
??????????????????????????? y += move[i][1];
??????????????????????? }
??????????????????????? if(y == temp.y)
??????????????????????? {
??????????????????????????? time = time + 2;
??????????????????????????? x += move[i][0];
??????????????????????? }
???????????????????? }
??????????????????? }
???????????????????? Node xin;xin.x = x;xin.y = y;xin.time = time;
???????????????????? q.push(xin);
???????????????????? //printf("%d %d\n",x,y);
???????????????????? hash[x][y] = 1;
????????????????? }
???????????????? if(map[x][y] == '-')
???????????????? {
??????????????????? //printf("%d? %d? %c\n",x,y,map[x][y]);
??????????????????? //hash[x][y] = 1;
??????????????????? if(time % 2 == 0)
??????????????????? {
??????????????????????? if(x == temp.x)
??????????????????????? {
??????????????????????????? time = time + 1;
??????????????????????????? y += move[i][1];
??????????????????????? }
??????????????????????? if(y == temp.y)
??????????????????????? {
??????????????????????????? time = time + 2;
??????????????????????????? x += move[i][0];
??????????????????????? }
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????? if(time % 2 == 1)
??????????????????? {
??????????????????????? if(x == temp.x)
??????????????????????? {
??????????????????????????? time = time + 2;
??????????????????????????? y += move[i][1];
??????????????????????? }
??????????????????????? if(y == temp.y)
??????????????????????? {
??????????????????????????? time = time + 1;
??????????????????????????? x += move[i][0];
??????????????????????? }
???????????????????? }
??????????????????? }
???????????????????? Node xin;xin.x = x;xin.y = y;xin.time = time;
???????????????????? q.push(xin);
???????????????????? //printf("%d %d\n",x,y);
???????????????????? hash[x][y] = 1;
???????????????? }
??????????????? }
???????????? }
???????? }
??? }
}
int main()
{
??? while(~scanf("%d%d",&n,&m))
??? {
??????? memset(hash,0,sizeof(hash));
??????? for(int i = 1;i <= n;i ++)
?????????? for(int j = 1;j <= m;j ++)
?????????? {
????????????? scanf(" %c",&map[i][j]);
????????????? if(map[i][j] == 'S')
????????????? {
??????????????????? sx = i;sy = j;
??????????????????? hash[i][j] = 1;
????????????? }
????????????? if(map[i][j] == 'T')
????????????? {
??????????????????? tx = i;ty = j;
????????????? }
??????????? }
??????? BFS();
??? }
??? return 0;
}
???????????
?????????????

轉(zhuǎn)載于:https://www.cnblogs.com/GODLIKEING/p/3284058.html

總結(jié)

以上是生活随笔為你收集整理的HDU 1180 诡异的楼梯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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