zoukankan      html  css  js  c++  java
  • HDU1242 BFS+优先队列

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 27406    Accepted Submission(s): 9711


    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
     
    Author
    CHEN, Xue
     
    Source
    题意:
    ~
    代码:
     1 //一道很简单的题卡了很长时间。从重点出发找起点,优先队列存
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 #include<functional>
     8 using namespace std;
     9 int n,m;
    10 struct Lu
    11 {
    12     int x,y,cnt;
    13     friend bool operator<(Lu a,Lu b)
    14     {
    15         return a.cnt>b.cnt;
    16     }
    17 };
    18 bool vis[205][205];
    19 int dir[4][2]={1,0,-1,0,0,1,0,-1};
    20 char ch[205][205];
    21 int bfs(int sx,int sy)
    22 {
    23     priority_queue<Lu>q;
    24     Lu L1,L2;
    25     L1.x=sx;L1.y=sy;L1.cnt=0;
    26     q.push(L1);
    27     vis[sx][sy]=1;
    28     while(!q.empty())
    29     {
    30         L2=q.top();
    31         q.pop();
    32         if(ch[L2.x][L2.y]=='r')
    33         return L2.cnt;
    34         for(int i=0;i<4;i++)
    35         {
    36             L1.x=L2.x+dir[i][0];
    37             L1.y=L2.y+dir[i][1];
    38             if(L1.x<0||L1.x>=n||L1.y<0||L1.y>=m) continue;
    39             if(vis[L1.x][L1.y]) continue;
    40             if(ch[L1.x][L1.y]=='#') continue;
    41             if(ch[L1.x][L1.y]=='x')
    42             L1.cnt=L2.cnt+2;
    43             else L1.cnt=L2.cnt+1;
    44             vis[L1.x][L1.y]=1;
    45             q.push(L1);
    46         }
    47     }
    48     return -1;
    49 }
    50 int main()
    51 {
    52     int sx,sy;
    53     while(scanf("%d%d",&n,&m)!=EOF)
    54     {
    55         for(int i=0;i<n;i++)
    56         {
    57             scanf("%s",ch[i]);
    58             for(int j=0;j<m;j++)
    59             {
    60                 if(ch[i][j]=='a')
    61                 {
    62                     sx=i;sy=j;
    63                 }
    64             }
    65         }
    66         memset(vis,0,sizeof(vis));
    67         int ans=bfs(sx,sy);
    68         if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.
    ");
    69         else printf("%d
    ",ans);
    70     }
    71     return 0;
    72 }
     
  • 相关阅读:
    js 前端开发 编程 常见知识点笔记
    重置 PowerShell 和 cmd 设置 样式 为系统默认值 powershell windows10
    useMemo和useCallback的区别 及使用场景
    数组去重,利用 ES6 的 reduce() 方法 和 include 判断 实现
    Java 中 Lombok 的使用,提高开发速度必备
    记录 windows 系统常用的 CMD 命令
    React Native 的 FlatList 组件 实现每次滑动一整项(item)
    Spring------mysql读写分离
    Webservice与CXF框架快速入门
    quartz
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5994949.html
Copyright © 2011-2022 走看看