HDU 1180——詭異的樓梯( BFS)

來源:互聯網
上載者:User

標籤:des   style   http   color   java   os   strong   io   

詭異的樓梯Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 8717    Accepted Submission(s): 2148


Problem DescriptionHogwarts正式開學以後,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
7HintHint 地圖如下:



—————————————————————分割線———————————————————————————————


題目的注意點:

樓梯每一分鐘會改變方向(可以根據走的步數的奇偶來判斷當前方向)

如果碰到樓梯,方向不對,可以停留在原地等下一分鐘樓梯改變方向

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#define M 25using namespace std;int vis[M][M],n,m,sx,sy;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//右左下上char map[M][M];struct node{    int x,y,st;};bool ck(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='*'&&!vis[x][y]) return true;    //判斷是否合法    return false;}int jd(int x,int y,int d,int st){    if(map[x][y]=='|'&&(d==0||d==1)&&(st%2==0)) return 1;//走不了    if(map[x][y]=='|'&&(d==2||d==3)&&(st&1)) return 1;    if(map[x][y]=='|'&&(d==2||d==3)&&(st%2==0)) return 0;//能走    if(map[x][y]=='|'&&(d==0||d==1)&&(st&1)) return 0;    if(map[x][y]=='-'&&(d==2||d==3)&&(st%2==0)) return 1;    if(map[x][y]=='-'&&(d==0||d==1)&&(st&1)) return 1;    if(map[x][y]=='-'&&(d==0||d==1)&&(st%2==0)) return 0;    if(map[x][y]=='-'&&(d==2||d==3)&&(st&1)) return 0;}int bfs(int x,int y){    memset(vis,0,sizeof vis);    queue<node> q;    node a,b;    a.x=x,a.y=y,a.st=0;    vis[x][y]=1;    q.push(a);    while(!q.empty()){        a=q.front();q.pop();        if(map[a.x][a.y]=='T'){            return a.st;        }        for(int i=0;i<4;++i){            b.x=a.x+dir[i][0];            b.y=a.y+dir[i][1];            b.st=a.st+1;            if(!ck(b.x,b.y)) continue;            if(map[b.x][b.y]=='.'||map[b.x][b.y]=='T'){                vis[b.x][b.y]=1;                q.push(b);            }            else{//碰到樓梯                if(!jd(b.x,b.y,i,a.st)){//能走                    b.x+=dir[i][0];                    b.y+=dir[i][1];                    if(!ck(b.x,b.y)) continue;                    else{                        vis[b.x][b.y]=1;                        q.push(b);                    }                }                else{//不能走,保留在原地                    b.x=a.x;                    b.y=a.y;                    q.push(b);                }            }        }    }    return -1;}int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    while(scanf("%d%d",&n,&m)!=EOF){        for(int i=0;i<n;++i){            for(int j=0;j<m;++j){                cin>>map[i][j];                if(map[i][j]=='S'){                    sx=i,sy=j;                }            }        }        printf("%d\n",bfs(sx,sy));    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.