HDU 1180 诡异的楼梯
生活随笔
收集整理的這篇文章主要介紹了
HDU 1180 诡异的楼梯
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
詭異的樓梯
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正式開學以后,Harry發現在Hogwarts里,某些樓梯并不是靜止不動的,相反,他們每隔一分鐘就變動一次方向.比如下面的例子里,一開始樓梯在豎直方向,一分鐘以后它移動到了水平方向,再過一分鐘它又回到了豎直方向.Harry發現對他來說很難找到能使得他最快到達目的地的路線,這時Ron(Harry最好的朋友)告訴Harry正好有一個魔法道具可以幫助他尋找這樣的路線,而那個魔法道具上的咒語,正是由你纂寫的.
Input
測試數據有多組,每組的表述如下: 第一行有兩個數,M和N,接下來是一個M行N列的地圖,'*'表示障礙物,'.'表示走廊,'|'或者'-'表示一個樓梯,并且標明了它在一開始時所處的位置:'|'表示的樓梯在最開始是豎直方向,'-'表示的樓梯在一開始是水平方向.地圖中還有一個'S'是起點,'T'是目標,0<=M,N<=20,地圖中不會出現兩個相連的梯子.Harry每秒只能停留在'.'或'S'和'T'所標記的格子內.Output
只有一行,包含一個數T,表示到達目標的最短時間.注意:Harry只能每次走到相鄰的格子而不能斜走,每移動一次恰好為一分鐘,并且Harry登上樓梯并經過樓梯到達對面的整個過程只需要一分鐘,Harry從來不在樓梯上停留.并且每次樓梯都恰好在Harry移動完畢以后才改變方向.
Sample Input
5 5 **..T **.*. ..|.. .*.*. S....Sample Output
7Hint
?
Source Gardon-DYGG Contest 1 Recommend JGShining 思路: 這個題目思路挺簡單的,就是BFS 加優先隊列,但是我卻調試到了凌晨一點才過,本人覺得 挺坑爹,關鍵在于在樓梯處怎么處理,因為可以在原處等待直到可以過去為止,看別人可以用 普通隊列,我沒怎么知道怎么去做 代碼: #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;
}
???????????
?????????????
轉載于:https://www.cnblogs.com/GODLIKEING/p/3284058.html
總結
以上是生活随笔為你收集整理的HDU 1180 诡异的楼梯的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑怎么装双系统教程 双系统安装教程:详
- 下一篇: 用usb大白菜怎么装系统 使用USB大白