zoukankan      html  css  js  c++  java
  • hdu 1180 诡异的楼梯 BFS + 优先队列

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<2|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in1.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    const int maxn=1000100;
    char grid[22][22];
    int vis[22][22];
    int dx[]={-1,0,1,0};
    int dy[]={0,1,0,-1};
    int n,m;
    struct point
    {
        int x,y,t;
        bool operator < (const point &pt) const
        {
            return t>pt.t;
        }
    };
    int bfs()
    {
        priority_queue<point> q;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(grid[i][j]=='S')
                {
                    q.push((point){i,j,0});
                    vis[i][j]=1;
                    break;
                }
        while(!q.empty())
        {
            point pt=q.top();q.pop();
            if(grid[pt.x][pt.y]=='T')return pt.t;
    
            for(int d=0;d<4;d++)
            {
                int nx=pt.x+dx[d];
                int ny=pt.y+dy[d];
                int nt=pt.t+1;
                if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
                {
                    if((grid[nx][ny]=='.'||grid[nx][ny]=='T')&&vis[nx][ny]!=1)
                    {
                        q.push( (point) {nx,ny,nt} );
                        vis[nx][ny]=1;
                    }
                    else if(grid[nx][ny]=='|')
                    {
                        if(nt%2==d%2)nt++;
                        int nnx=nx+dx[d],nny=ny+dy[d];
                        if(nnx>=1&&nnx<=n&&nny>=1&&nny<=m&&!vis[nnx][nny])
                        {
    
                            q.push( (point) {nnx,nny,nt} );
                            vis[nnx][nny]=1;
                        }
                    }
                    else if(grid[nx][ny]=='-')
    
                    {
                        if(nt%2!=d%2)nt++;
                        int nnx=nx+dx[d],nny=ny+dy[d];
                        if(nnx>=1&&nnx<=n&&nny>=1&&nny<=m&&!vis[nnx][nny])
                        {
    
                            q.push( (point) {nnx,nny,nt} );
                            vis[nnx][nny]=1;
                        }
                    }
                }
            }
        }
        return -1;
    }
    int main()
    {
          while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%s",grid[i]+1);
            int num=bfs();
            printf("%d
    ",num);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    JDK5后的特性整理
    正向代理与反向代理的区别与异同
    我所用过的nginx的功能
    网页端消息推送之推与拉
    在一个py脚本中调用另外一个py脚本中的类或函数
    import与from ... import ...的区别
    python 读取文件
    shell中的特殊变量IFS
    shell 重定向以及文件描述符
    shell下读取文件数据
  • 原文地址:https://www.cnblogs.com/BMan/p/3475172.html
Copyright © 2011-2022 走看看