zoukankan      html  css  js  c++  java
  • hdu

    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
    ",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
    ",s.x,s.y,s.time);
                                if(!check(s)) continue;
                                s.time+=1;
                               // printf("%d %d %d
    ",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
    ",s.x,s.y,s.time);
                                if(!check(s)) continue;
                                s.time+=2;
                               // printf("%d %d %d
    ",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
    ",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
    ",bfs());
        }
        return 0;
    }
  • 相关阅读:
    程序员保持快乐活跃的6个好习惯(转)
    Spring MVC Hello World Example(转)
    Oracle定时执行存储过程(转)
    各种常见数据库分页实现(转)
    Linux SSH常用总结(转)
    让人深思......
    void及void指针含义的深刻解析
    UVa 11988
    网络编程学习小结
    Deep Learning(深度学习) 学习笔记(四)
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4528870.html
Copyright © 2011-2022 走看看