码迷,mamicode.com
首页 > 其他好文 > 详细

(dfs) hdu 1180

时间:2015-03-21 11:11:04      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 9929    Accepted Submission(s): 2465


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
7
Hint
Hint
地图如下: 技术分享
 

 

Source
 
 
擦 调了 一上午。。。我真是个SB!!!!
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m,sx,sy,ex,ey,ans;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char s[30][30];
bool vis[30][30];
struct node
{
    int x,y,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
};
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||vis[x][y]||s[x][y]==‘*‘)
        return false;
    return true;
}
void bfs()
{
    priority_queue<node> q;
    node fr;
    int nx,ny;
    memset(vis,0,sizeof(vis));
    fr.x=sx,fr.y=sy,fr.step=0;
    vis[sx][sy]=1;
    q.push(fr);
    while(!q.empty())
    {
        fr=q.top(),q.pop();
        if(fr.x==ex&&fr.y==ey)
        {
            ans=fr.step;
            break;
        }
        for(int i=0;i<4;i++)
        {
            nx=fr.x+dx[i],ny=fr.y+dy[i];
            if(!check(nx,ny)) continue;
            if(s[nx][ny]==‘.‘||s[nx][ny]==‘T‘)
            {
                node temp;
                temp.x=nx,temp.y=ny;
                temp.step=fr.step+1;
                vis[nx][ny]=1;
                q.push(temp);
            }
            else if(i==0||i==1)
            {
                if(s[nx][ny]==‘-‘&&(fr.step%2==0))
                {
                    node temp;
                    nx+=dx[i],ny+=dy[i];
                    if(!check(nx,ny)) continue;
                    vis[nx][ny]=1;
                    temp.x=nx,temp.y=ny;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
                else if(s[nx][ny]==‘|‘&&(fr.step%2==1))
                {
                    node temp;
                    nx+=dx[i],ny+=dy[i];
                    if(!check(nx,ny)) continue;
                    vis[nx][ny]=1;
                    temp.x=nx,temp.y=ny;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
                else
                {
                    node temp;
                    temp.x=fr.x,temp.y=fr.y;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
            }
            else if(i==2||i==3)
            {
                if(s[nx][ny]==‘|‘&&(fr.step%2==0))
                {
                    node temp;
                    nx+=dx[i],ny+=dy[i];
                    if(!check(nx,ny)) continue;
                    vis[nx][ny]=1;
                    temp.x=nx,temp.y=ny;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
                else if(s[nx][ny]==‘-‘&&(fr.step%2==1))
                {
                    node temp;
                    nx+=dx[i],ny+=dy[i];
                    if(!check(nx,ny)) continue;
                    vis[nx][ny]=1;
                    temp.x=nx,temp.y=ny;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
                else
                {
                    node temp;
                    temp.x=fr.x,temp.y=fr.y;
                    temp.step=fr.step+1;
                    q.push(temp);
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]==‘S‘)
                    sx=i,sy=j;
                else if(s[i][j]==‘T‘)
                    ex=i,ey=j;
            }
        }
        bfs();
        printf("%d\n",ans);
    }
    return 0;
}

  

(dfs) hdu 1180

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4355191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!