• 【搜索】POJ1242:Rescue


    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

    BFS广搜 + 优先队列

      题意:从前有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,'.'表示路,可以通行。'#'代表墙,无法通行。'r'表示天使的朋友,代表起点。'a'表示天使的位置,代表终点。'x'表示守卫的位置。

      思路:因为是求“最短路径”的问题,正好用广搜可以解决。但是与普通广搜不同的是,遇到守卫的时候会多停留一秒。这就会导致队列中优先级的不稳定,所以需要用优先队列,让优先级最高的节点始终在队列最前面。

      代码

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <queue>
      4 #include <cstring>
      5 using namespace std;
      6 char mapp[205][205];
      7 bool vis[205][205];
      8 int dx[4] = {0,1,0,-1};
      9 int dy[4] = {1,0,-1,0};
     10 int n,m;
     11 int sx,sy,ex,ey;
     12 struct node
     13 {
     14     int x,y,step;
     15     friend bool operator < (node a,node b)
     16     {
     17         return a.step>b.step;
     18     }
     19 };
     20 bool check(int x,int y)
     21 {
     22     if(x<1||x>n||y<1||y>m)
     23         return true;
     24     if(vis[x][y])
     25         return true;
     26     if(mapp[x][y]=='#')
     27         return true;
     28     return false;
     29 }
     30 int bfs()
     31 {
     32     memset(vis,false,sizeof(vis));
     33     priority_queue<node> q;
     34     node now,nextt;
     35     now.x=sx;
     36     now.y=sy;
     37     now.step=0;
     38     vis[now.x][now.y]=true;
     39     while(!q.empty())
     40     {
     41         q.pop();
     42     }
     43     q.push(now);
     44     while(!q.empty())
     45     {
     46         now=q.top();
     47         q.pop();
     48         if(now.x==ex&&now.y==ey)
     49         {
     50             return now.step;
     51         }
     52         for(int i=0;i<4;i++)
     53         {
     54             int xx=now.x+dx[i];
     55             int yy=now.y+dy[i];
     56             if(check(xx,yy))
     57             {
     58                 continue;
     59             }
     60             nextt.x=xx;
     61             nextt.y=yy;
     62             if(mapp[xx][yy]=='x')
     63             {
     64                 nextt.step=now.step+2;
     65             }
     66             else
     67             {
     68                 nextt.step=now.step+1;
     69             }
     70             vis[xx][yy]=1;
     71             q.push(nextt);
     72         }
     73     }
     74     return -1;
     75 }
     76 int main()
     77 {
     78     while(scanf("%d%d",&n,&m)!=EOF)
     79     {
     80         getchar();
     81         for(int i=1;i<=n;i++)
     82         {
     83             for(int j=1;j<=m;j++)
     84             {
     85                 scanf(" %c",&mapp[i][j]);
     86                 if(mapp[i][j]=='a')
     87                 {
     88                     ex=i;
     89                     ey=j;
     90                 }
     91                 else if(mapp[i][j]=='r')
     92                 {
     93                     sx=i;
     94                     sy=j;
     95                 }
     96             }
     97         }
     98         int step=bfs();
     99         if(step==-1)
    100         {
    101             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    102         }
    103         else
    104         {
    105             printf("%d
    ",step);
    106         }
    107     }
    108     return 0;
    109 }
    View Code
  • 相关阅读:
    排版字号对应多少pt
    C# 定义常量 两种实现方法
    HttpApplication 类,HttpApplicationState 类
    [ASP.net教程]ASP.NET保存信息总结(Application、Session、Cookie、ViewState和Cache等)
    sqlserver 汉字转拼音(转载)
    几个不错的echarts +百度地图 案例
    ECharts 实现地图散点图上(转载)
    ECharts 散点图+百度地图(案例转载)
    SQL统计部门人数,人数为0的部门也要显示出来
    待看文档
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/8446771.html
Copyright © 2020-2023  润新知