zoukankan      html  css  js  c++  java
  • HDU 1026

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<cstdio>
    #include<stack>
    #include<queue>
    #define maxn 101
    using namespace std;
    
    char mapp[maxn][maxn];
    int visited[maxn][maxn],n,m;
    int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
    struct node{
        int x;
        int y;
        int second;
        int x_next;
        int y_next;
        friend bool operator< (node a, node b){return a.second > b.second;}  //按照时间顺序从小到大
    };
    node path[maxn][maxn];
    node en;
    stack<node> S;
    priority_queue <node> pq;
    int bfs(){
        while(!pq.empty()){
            node temp,next;
            temp=pq.top();
            if(temp.x==n-1&&temp.y==m-1){
                en.x=temp.x;
                en.y=temp.y;
                en.x_next=0;
                en.y_next=0;
                return temp.second;
            }
               pq.pop();
            for(int i=0;i<4;i++){
                next.x=temp.x+dir[i][0];
                next.y=temp.y+dir[i][1];
                if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&visited[next.x][next.y]==0){
                    if(mapp[next.x][next.y]=='.'){
                        next.second=temp.second+1;
                        visited[next.x][next.y]=1;
    
                          temp.x_next=next.x;
                          temp.y_next=next.y;
                          path[next.x][next.y]=temp;
                          pq.push(next);
                    }else if(mapp[next.x][next.y]>'0'&&mapp[next.x][next.y]<='9'){
                        next.second=temp.second+1+(mapp[next.x][next.y]-'0');
    
                          visited[next.x][next.y]=1;
    
                          temp.x_next=next.x;
                          temp.y_next=next.y;
                           path[next.x][next.y]=temp;
                        //  cout<<temp.x_next<<"  "<<temp.y_next<<endl;
                          pq.push(next);
                    }
    
                }
            }
        }
        return -1;
    }
    
    int main(){
        while(cin>>n>>m){
        memset(visited,0,sizeof(visited));
        memset(mapp,0,sizeof(mapp));
        memset(path,0,sizeof(path));
        while(!pq.empty()) pq.pop();
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    cin>>mapp[i][j];
                }
            }
            visited[0][0]=1;
            node t1;
            t1.x=0;t1.y=0;t1.second=0;
            pq.push(t1);
            int t=bfs();
            if(t==-1)
                cout<<"God please help our poor hero."<<endl;
            else{
                cout<<"It takes "<<t<<" seconds to reach the target position, let me show you the way."<<endl;
                S.push(en);
                node temp=path[en.x][en.y];
                while(temp.x||temp.y){
                    S.push(temp);
                   // cout<<temp.x<<" "<<temp.y<<endl;
                    temp = path[temp.x][temp.y];
                }
                S.push(temp);
                 int num = 1;
                while(!S.empty()){
                    temp = S.top();
                    S.pop();
                    if(mapp[temp.x][temp.y] == '.' && !(temp.x_next == 0 && temp.y_next == 0)){
                        printf("%ds:(%d,%d)->(%d,%d)
    ", num ++, temp.x, temp.y, temp.x_next, temp.y_next);
                    }
                    else if(mapp[temp.x][temp.y] >='0' && mapp[temp.x][temp.y] <= '9'){
                        int t = mapp[temp.x][temp.y] - '0';
                        while(t --){
                            printf("%ds:FIGHT AT (%d,%d)
    ", num ++, temp.x, temp.y);
                        }
                        if(!(temp.x_next == 0 && temp.y_next == 0)){
                            printf("%ds:(%d,%d)->(%d,%d)
    ", num ++, temp.x, temp.y, temp.x_next, temp.y_next);
                        }
                    }
                }
            }
            printf("FINISH
    ");
            }
    
    
    
    
       return 0;
    }
  • 相关阅读:
    Azure PowerShell (2) 修改Azure订阅名称
    Windows Azure Platform Introduction (11) 了解Org ID、Windows Azure订阅、账户
    Azure PowerShell (3) 上传证书
    Azure PowerShell (1) PowerShell入门
    Windows Azure Service Bus (2) 队列(Queue)入门
    Windows Azure Service Bus (1) 基础
    Windows Azure Cloud Service (10) Role的生命周期
    Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书
    Android studio 使用心得(一)—android studio快速掌握快捷键
    android 签名、混淆打包
  • 原文地址:https://www.cnblogs.com/wintersong/p/5187296.html
Copyright © 2011-2022 走看看