jks88995656 2021-09-29 10:45 采纳率: 100%
浏览 38
已结题

c语言看一下,能给出代码

题目:
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.

Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

输入输出见 hdu 1026

  • 写回答

2条回答 默认 最新

  • 白白白白白8 2021-09-29 11:24
    关注

    优先队列+bfs,bfs第一次达到右下角的点就是结果

    
    #include <bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    int n,m,num;
    struct Node{
        int x,y;
        int time;
        int prex,prey;
        char c;
        
        bool operator>(const Node & node) const{
            return time>node.time;
        }
    };
    Node node[105][105];
    int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    bool BFS(){
        //queue<Node> pq;
        priority_queue<Node,vector<Node>,greater<Node> > pq;
        node[0][0].time=0;
        pq.push(node[0][0]);
        while(!pq.empty()){
            Node now=pq.top();
            //Node now=pq.front();
            pq.pop();
            for(int i=0;i<4;i++){
                int x=now.x+dir[i][0];   //下一个点 
                int y=now.y+dir[i][1];
                if(x>=0&&x<n&&y>=0&&y<m&&node[x][y].c=='.'){
                    if(node[x][y].time>now.time+1){
                        node[x][y].time=now.time+1;
                        node[x][y].prex=now.x;
                        node[x][y].prey=now.y;
                        pq.push(node[x][y]);
                        //printf("点(%d,%d)压入栈中\n",node[x][y].x,node[x][y].y);
                    }
                }else if(x>=0&&x<n&&y>=0&&y<m&&node[x][y].c>='0'&&node[x][y].c<='9'){
                    if(node[x][y].time>now.time+node[x][y].c-'0'+1){
                        node[x][y].time=now.time+node[x][y].c-'0'+1;
                        node[x][y].prex=now.x;
                        node[x][y].prey=now.y;
                        pq.push(node[x][y]);
                        //printf("点(%d,%d)压入栈中\n",node[x][y].x,node[x][y].y);
                    }
                }
                //cout<<x<<y<<endl;
                if(x==n-1&&y==m-1)
                    return true;
            }
        }
        return false;
        
    }
    void print_result(int i,int j){
        if(i==0&&j==0)
            return;
        int px=node[i][j].prex;//前一点坐标 
        int py=node[i][j].prey;
        print_result(px,py);
        printf("%ds:(%d,%d)->(%d,%d)\n",node[px][py].time+1,node[px][py].x,node[px][py].y,node[i][j].x,node[i][j].y);
        if(node[i][j].c>='0'&&node[i][j].c<='9'){
            for(int tmp=node[px][py].time+2;tmp<node[px][py].time+2+node[i][j].c-'0';tmp++){
                printf("%ds:FIGHT AT (%d,%d)\n",tmp,node[i][j].x,node[i][j].y);
            }
        }
    }
    int main(){
        
        while(~scanf("%d%d",&n,&m)){
            getchar();
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    scanf("%c",&node[i][j].c);
                    node[i][j].x=i;node[i][j].y=j;
                    node[i][j].time=inf;
                }
                getchar();
            }
            bool state=false;
            state=BFS();
            if(!state){
                printf("God please help our poor hero.\n");
            }else{
                printf("It takes %d seconds to reach the target position, let me show you the way.\n",node[n-1][m-1].time);
                print_result(n-1,m-1);
            }
            printf("FINISH\n");
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月7日
  • 已采纳回答 9月29日
  • 创建了问题 9月29日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么