• HDU_1242——二维空间搜索,使用优先队列BFS


    优先队列,这道题被坑了,心情不好。

    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     
    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

    Process to the end of the file.
     
    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
     
    Sample Input
    7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
     
    Sample Output
    13
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <queue>
     5 #define MAX 200
     6 using namespace std;
     7 typedef struct node
     8 {
     9    int x,y;
    10    int move;
    11    
    12 }point;
    13 point start;
    14 //把move设置为优先级变量,top()返回最小的队 
    15 bool operator <(node n1,node n2)
    16    {
    17       return n1.move>n2.move;
    18    }
    19 
    20 int N,M;
    21 const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    22 char map[MAX][MAX];
    23 
    24 void Bfs(void);
    25 int main()
    26 {
    27     while(~scanf("%d %d",&N,&M))//~是把-1变成0 
    28       {
    29          memset(map,0,sizeof(map)); 
    30          for(int i=0;i<N;i++)
    31             for(int j=0;j<M;j++)
    32                {
    33                   //cin>>map[i][j];
    34                   scanf("%1s",&map[i][j]);
    35                   if(map[i][j]=='a')
    36                      start.x=i,start.y=j;
    37                }
    38          start.move=0;
    39          Bfs();
    40       }
    41    return 0;
    42 }
    43 
    44 void Bfs()
    45 {
    46    int visit[MAX][MAX];
    47    memset(visit,0,sizeof(visit));
    48    visit[start.x][start.y]=1;
    49    
    50    priority_queue<point>p;
    51    p.push(start);
    52    point temp,next;
    53    
    54    while(!p.empty())
    55       {
    56          temp=p.top();
    57          p.pop();
    58          for(int i=0;i<4;i++)
    59             {
    60                next.x=temp.x+dir[i][0];
    61                next.y=temp.y+dir[i][1];
    62                if(!visit[next.x][next.y] && map[next.x][next.y]!='#' && next.x>=0&&next.x<N && next.y>=0&&next.y<M)
    63                   {
    64                      visit[next.x][next.y]=1;
    65                      if(map[next.x][next.y]=='x')        
    66                         next.move=temp.move+2;
    67                      else if(map[next.x][next.y]=='.')
    68                         next.move=temp.move+1;
    69                      else if(map[next.x][next.y]=='r')
    70                         {
    71                            next.move=temp.move+1;
    72                            printf("%d\n",next.move);
    73                            return;
    74                         }
    75                      p.push(next);
    76                   }
    77             }
    78       }
    79    printf("Poor ANGEL has to stay in the prison all his life.\n");
    80    return;
    81 }
    82 /*
    83 7 8
    84 #.#####.
    85 #.a#..r.
    86 #..#x...
    87 ..#..#.#
    88 #...##..
    89 .#......
    90 ........
    91 */
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    机器学习算法原理与实践-决策树(文章迁移)
    机器学习算法原理与实践-正规方程、梯度下降(文章迁移)
    Kubernetes-PV和PVC的原理和实践
    算法系列之——希尔排序算法
    算法系列之——插入算法
    浏览器加载解析渲染网页原理
    Express session应用与原理源码解析
    Express4.x之中间件与路由详解及源码分析
    Express4.x之API:express
    webstorm不能提示node代码:coding assistance for node.js不能enable解决方案
  • 原文地址:https://www.cnblogs.com/pingge/p/3130058.html
Copyright © 2020-2023  润新知