HDOJ1026

思路是用BFS来搜索路径,将路径存储在对应的数组中,同时定义一个优先队列来处理与士兵战斗的时间。具体解法见代码中的注释。

//

//  main.cpp

//  hdoj-test

//

//  Created by willam huang on 2018/4/6.

//  Copyright © 2018年 willam huang. All rights reserved.

//

 

#include <iostream>

#include <string.h>

#include <stdio.h>

#include <queue>

 

using namespace std;

 

struct zuobiao{

    int x,y,output;

    friend bool operator <(zuobiao a,zuobiao b){

        return a.output>b.output;

    }

    int zoufa[1000][2];

}now,nex;

 

int vis[1000][1000];//the mark of visited nodes

char a[1000][1000];//the graph

int n,m;//the height and width of the graph

int fx[4]={0,0,1,-1};

int fy[4]={1,-1,0,0};//represent four different directions

 

void bfs(int x,int y){

    memset(vis,0,sizeof(vis));//refresh the visit record

    priority_queue<zuobiao>s;//construct a priority queque

    now.x = x;

    now.y = y;

    now.output = 0;

    now.zoufa[0][0] = x;//record the initial step which is the start node

    now.zoufa[0][1] = y;

    vis[x][y] = 1;//set the start node as visited

    s.push(now);//let the node get into the queue

    while(!s.empty()){

        now = s.top();//check the current top node

        if(now.x==n-1 && now.y==m-1){//if has arrive the end node

            int bijiaox = 0,bijiaoy = 0;//use this two variable to mark the node where you have to fight with the monsters

            printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.output);

            for(int i=0;i<now.output;i++){

                if(bijiaox==now.zoufa[i+1][0] && bijiaoy==now.zoufa[i+1][1]){

                    printf("%ds:FIGHT AT (%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1]);

                    bijiaox = now.zoufa[i+1][0];

                    bijiaoy = now.zoufa[i+1][1];//refresh the variables

                    continue;

                }

            printf("%ds:(%d,%d)->(%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1],now.zoufa[i+1][0],now.zoufa[i+1][1]);

                bijiaox = now.zoufa[i+1][0];

                bijiaoy = now.zoufa[i+1][1];//if no fight, then output the path

            }

            printf("FINISH\n");

            return;//the output is finished, get out of the while loop

        }

        s.pop();//bfs,explore the nodes around it

        for(int i=0;i<4;i++){

            nex = now;//should copy the forward nodes, mainly for its path record

            nex.x = now.x+fx[i];

            nex.y = now.y+fy[i];

            if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && vis[nex.x][nex.y]==0 && a[nex.x][nex.y]!='X'){

                //satisfy all this conditions

                if(a[nex.x][nex.y]=='.'){

                    //if next is road

                    nex.output = now.output+1;

                    int num = nex.output;

                    nex.zoufa[num][0] = nex.x;

                    nex.zoufa[num][1] = nex.y;//record the step

                    vis[nex.x][nex.y] = 1;

                    s.push(nex);//bfs,set the next node as the new basic node and continue exlore its neighbours

                }

                if(a[nex.x][nex.y]>='1' && a[nex.x][nex.y]<='9'){

                    //if next is monster

                    int xunhuan = a[nex.x][nex.y]-'0';//we should go one step forward

                    nex.output = now.output+1;

                    int num = nex.output;

                    nex.zoufa[num][0] = nex.x;

                    nex.zoufa[num][1] = nex.y;

                    for(int k=0;k<xunhuan;k++){//fight with the monster

                        nex.output += 1;

                        int num = nex.output;

                        nex.zoufa[num][0] = nex.x;

                        nex.zoufa[num][1] = nex.y;

                        vis[nex.x][nex.y] = 1;

                    }

                    s.push(nex);//bfs,set the next node as the new basic node and continue exlore its neighbours

                }

            }

        }

    }

    printf("God please help our poor hero.\nFINISH\n");

}

 

 

int main(){

    while(~scanf("%d%d",&n,&m)){

        for(int i=0;i<n;i++){

            scanf("%s",a[i]);//input the graph

        }

        bfs(0,0);//if there is input, use bfs to solve the problem

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值