hdu1242 rescue

在迷宫般的楼梯中,一位勇敢的天使姐姐从起点出发,利用优先队列算法,通过智能搜索策略,克服重重障碍,寻找通往自由的道路。通过深度优先遍历,她巧妙地避开陷阱,最终找到了逃脱的出口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同诡异的楼梯,也是优先队列,注意这里从天使姐姐开始搜起

#include <iostream>

#include <cstring>

#include <cstdio>

#include <queue>

using namespace std;

struct Node

{

       int x, y;

       int step;

       bool operator <(const Node t)const{

            return step > t.step;

       }       

};

 

const int Go[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

 

char map[210][210];

 

int n, m;

Node start;

int ans;

int check(int x, int y)

{

    return (x>=0 && x<n && y>=0 &&y < m);    

}

int visited[210][210];

void bfs()

{

     priority_queue <Node>Que;

     //while (!Que.empty())Que.pop();

     Que.push(start);

     memset(visited, 0, sizeof(visited));

     visited[start.x][start.y] = 1;

     //Node pre, next;

     while (!Que.empty()){

           Node pre = Que.top();

           Que.pop();

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

               int xx = pre.x+Go[i][0];

               int yy = pre.y+Go[i][1];//cout<<"asdasd"<<endl; 

               if (check(xx, yy)&&map[xx][yy] != '#'&&!visited[xx][yy]){

                  visited[xx][yy] = 1;

                  if (map[xx][yy]=='r'){

                     ans = pre.step+1;

                     return ;                                                        

                  }

                  if (map[xx][yy]=='x'){

                     Node next; 

                     next.x = xx, next.y = yy;

                     next.step = pre.step+2;

                     Que.push(next);

                  }

                  else {

                       Node next;

                       next.x = xx, next.y = yy;

                       next.step = pre.step+1;  

                       Que.push(next);

                  }

                  //visited[next.x][next.y] = 1;                  

               }      

           }    

     }    

}

int main()

{

 

    while (scanf("%d %d", &n, &m)!=EOF ){

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

              getchar();

              scanf("%s", map[i]);

              //cin>>map[i];

              for (int j  = 0; j < m; j ++){

                  //cin >> map[i][j];

                  if (map[i][j] == 'a'){

                     start.x = i;

                     start.y = j;              

                  }        

              }    

          }

          start.step = 0;

          ans = -1;

          bfs();

          if (ans != -1){

             printf("%d/n", ans);

          }

          else printf("Poor ANGEL has to stay in the prison all his life./n");

    }

    return 0;    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值