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

hdu - 1180 诡异的楼梯 (bfs+优先队列)

时间:2015-05-25 22:11:56      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=1180

注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是水平的还是垂直的。

并且我们需要知道当前到达楼梯这个点的方向,这样才知道下一个往哪个方向走,可以根据dir数组来判断方向。

楼梯不用判重。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;

char field[25][25];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
struct point
{
    int x,y,time;
    bool operator < (const point a) const
    {
        return time>a.time;
    }
};
point s,e;

bool check(point t)
{
    if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&field[s.x][s.y]!=*)
        return true;
    return false;
}

int bfs()
{
    priority_queue<point>que;
    que.push(s);
    field[s.x][s.y]=*;
    while(que.size())
    {
        point t=que.top(); que.pop();

        if(t.x==e.x&&t.y==e.y) return t.time;
        for(int i=0;i<4;i++)
        {
            s=t;
            s.x=t.x+dir[i][0];
            s.y=t.y+dir[i][1];
            if(check(s))
            {
                if(field[s.x][s.y]==|)
                {
                    //printf("%d %d %d %d %d\n",dir[i][0],dir[i][1],t.x,t.y,t.time);
                    if(s.time%2==0)  //偶数 那么 还是 ‘|‘
                    {
                        if(dir[i][0]!=0) //如果是垂直方向只要1分钟就可以到达
                        {
                            s.x+=dir[i][0];
                           // printf("%d %d %d\n",s.x,s.y,s.time);
                            if(!check(s)) continue;
                            s.time+=1;
                           // printf("%d %d %d\n",s.x,s.y,s.time);
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                        else   //是水平方向,就要等2秒 因为要等一秒
                        {
                            s.y+=dir[i][1];
                            if(!check(s)) continue;
                            s.time+=2;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                    }
                    else  //奇数 那么变成了 ‘-‘  下面同理
                    {
                        if(dir[i][0]!=0)
                        {
                            s.x+=dir[i][0];
                          //  printf("%d %d %d\n",s.x,s.y,s.time);
                            if(!check(s)) continue;
                            s.time+=2;
                           // printf("%d %d %d\n",s.x,s.y,s.time);
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                        else
                        {
                            s.y+=dir[i][1];
                            if(!check(s)) continue;
                            s.time+=1;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                    }
                }
                else if(field[s.x][s.y]==-)
                {
                    if(s.time%2==0)
                    {
                        if(dir[i][0]!=0)
                        {
                            s.x+=dir[i][0];
                            if(!check(s)) continue;
                            s.time+=2;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                        else
                        {
                            s.y+=dir[i][1];
                            if(!check(s)) continue;
                            s.time+=1;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                    }
                    else
                    {
                        if(dir[i][0]!=0)
                        {
                            s.x+=dir[i][0];
                            if(!check(s)) continue;
                            s.time+=1;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                        else
                        {
                            s.y+=dir[i][1];
                            if(!check(s)) continue;
                            s.time+=2;
                            field[s.x][s.y]=*;
                            que.push(s);
                        }
                    }
                }
                else
                {
                    s.time+=1;
                    field[s.x][s.y]=*;
                    que.push(s);
                }
            }
        }
    }
}
int main()
{
    //freopen("a.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%s",field[i]);
           // printf("%s\n",field[i]);
            for(int j=0;j<m;j++)
            {
                if(field[i][j]==S)
                {
                    s.x=i;
                    s.y=j;
                    s.time=0;
                }
                else if(field[i][j]==T)
                {
                    e.x=i;
                    e.y=j;
                }
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}

 

hdu - 1180 诡异的楼梯 (bfs+优先队列)

标签:

原文地址:http://www.cnblogs.com/nowandforever/p/4528870.html

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