zoukankan      html  css  js  c++  java
  • HDU1026

    迷宫题

    优先队列,然后输出路径

    wa的代码,还没找到错哪里

    先存起来把,等哪天心情好再改

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN=110;
    char g[MAXN][MAXN];
    int b[MAXN][MAXN];
    int n,m;
    int dx[4]={0,0,-1,1};
    int dy[4]={1,-1,0,0};
    bool vis[MAXN][MAXN];
    struct node{
        int x,y,c;
        friend bool operator <(node a, node b){
            return a.c > b.c;
        }
    };
    int a[MAXN][MAXN];
    priority_queue<node>que;
    void init(){
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                vis[i][j]=false;
                a[i][j]=-1;
            }
        }
        while(!que.empty())que.pop();
    }
    int bfs(){
        int sx=0,sy=0,gx=n-1,gy=m-1;
        node aa,tt;
        aa.x=sx;aa.y=sy;aa.c=0;
        init();
        que.push(aa);
        while(!que.empty()){
            aa=que.top();que.pop();
            if(aa.x==gx&&aa.y==gy){
                return aa.c;
            }
            if(vis[aa.x][aa.y])continue;
            vis[aa.x][aa.y]=true;
            for(int i=0;i<4;i++){
                int nx=aa.x+dx[i];
                int ny=aa.y+dy[i];
                if(nx<0||ny<0||nx>=n||ny>=m||vis[nx][ny]||g[nx][ny]=='X')continue;
                tt.x=nx;tt.y=ny;
                if(g[nx][ny]=='.')tt.c=aa.c+1;
                else tt.c=aa.c+g[nx][ny]-'0'+1;
                a[nx][ny]=i;//记录方向
                que.push(tt);
            }
        }
        return -1;
    }
    int tot;
    void find_road(int x,int y){
        if(x==0&&y==0)return ;
        find_road(x-dx[a[x][y]],y-dy[a[x][y]]);
        printf("%ds:(%d,%d)->(%d,%d)
    ",++tot,x-dx[a[x][y]],y-dy[a[x][y]],x,y);
            for(int i=0;i<b[x][y];i++){
                printf("%ds:FIGHT AT (%d,%d)
    ",++tot,x,y);
            }
    }
    int main(){
        while(~scanf("%d%d",&n,&m)){
            for(int i=0;i<n;i++){
                scanf("%s",g[i]);
                for(int j=0;j<m;j++){
                    if((g[i][j]>='0')&&(g[i][j]<='9')){
                        b[i][j]=g[i][j]-'0';
                    }
                    else b[i][j]=0;
                }
            }
            int ans=bfs();
            if(ans==-1)
            printf("God please help our poor hero.
    ");
            else{
                printf("It takes %d seconds to reach the target position, let me show you the way.
    ",ans);
                tot=0;
                find_road(n-1,m-1);
            }
            printf("FINISH
    ");
        }
    
    
        return 0;
    }
    View Code
  • 相关阅读:
    angular安装指定版本
    Fluttter通过按钮来打开抽屉Drawer或者endDrawer
    angular中的animation动画
    flutter pubspec.yaml配置文件详解
    angular-cli卸载安装
    angular的项目基本配置的了解
    angular使用代理解决跨域
    IOS开发之UI布局
    用Objective-C写了一个简单的批量更改文件名的程序
    使用Objective-C 计算代码运行时间
  • 原文地址:https://www.cnblogs.com/lin1874/p/12203505.html
Copyright © 2011-2022 走看看