HDOJ1026 优先队列bfs

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char Map[150][150];
int vis[150][150];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct NODE
{
    int x,y;
}NODE;
struct node
{
    int s;
    NODE now;
    NODE pre;
    friend bool operator <(node a,node b)	//从大到小排序
    {
        return a.s>b.s;
    }
}a[150][150];
void init()				  //初始化标志数组
{
    memset(vis,0,sizeof(vis));
}
bool check(int x,int y)			 //未出界 并且 不是墙  并且 未访问
{
    return !vis[x][y]&&x>=0&&y>=0&&x<n&&y<m&&(Map[x][y]!='X');
}
void print(node st)
{
    cout<<"It takes "<<st.s<<" seconds to reach the target position, let me show you the way."<<endl;
    NODE route[10050];
    route[0]=st.now; //终点
    int cnt=1;
    while(st.now.x!=0||st.now.y!=0){		 //把路径存在route数组中
        st=a[st.pre.x][st.pre.y];
        route[cnt++]=st.now;
    }
    int t=1;	//记时


    for(int i=cnt-1;i>0;i--){  //只需选在n-1个点
        if(Map[route[i].x][route[i].y]!='.'){  //如果为数字
            char tt=Map[route[i].x][route[i].y];
            while(tt!='0'){		 //数字为多少循环几次 
                cout<<t++<<"s:FIGHT AT ("<<route[i].x<<','<<route[i].y<<")"<<endl;
                tt--;
            }
        }
        cout<<t++<<"s:("<<route[i].x<<','<<route[i].y<<")->("<<route[i-1].x<<','<<route[i-1].y<<')'<<endl;
    }


    if(Map[route[0].x][route[0].y]!='.'){		 //若终点为数字这种情况单独考虑
        char tt=Map[route[0].x][route[0].y];
        while(tt!='0'){
            cout<<t++<<"s:FIGHT AT ("<<route[0].x<<','<<route[0].y<<")"<<endl;
            tt--;
        }
    }
    return;
}
void bfs()
{
    priority_queue<node>q;	  //优先队列
    node st;


    st.s=0;
    st.now.x=0;
    st.now.y=0;
    st.pre.x=0;
    st.pre.y=0;


    a[0][0]=st;
    vis[0][0]=1;


    q.push(st);		   //当前点入队
    while(!q.empty()){


        st=q.top();
        q.pop();


        if(st.now.x==n-1&&st.now.y==m-1){	   //找到终点
            print(st);
            //cout<<st.s<<endl;
            return;
        }


        for(int i=0;i<4;i++){
            node nt=st;
            nt.pre.x=st.now.x;
            nt.pre.y=st.now.y;
            nt.now.x+=dir[i][0];
            nt.now.y+=dir[i][1];	 


            if(check(nt.now.x,nt.now.y)){
                vis[nt.now.x][nt.now.y]=1;
                a[nt.now.x][nt.now.y]=nt;
                int t=1;
                if(Map[nt.now.x][nt.now.y]!='.'){
                    t+=Map[nt.now.x][nt.now.y]-'0';
                }
                nt.s+=t;
                q.push(nt);
            }


        }
    }


    cout<<"God please help our poor hero."<<endl;
}
int main()
{
    cin.sync_with_stdio(false);	   //加快cout cin的速度
    while(cin>>n>>m){
        init();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>Map[i][j];
            }
        }
        bfs();
        cout<<"FINISH"<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值