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日

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗