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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题