zoukankan      html  css  js  c++  java
  • 杭电1242--Rescue(BFS+优先队列)

    Rescue

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


    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
     

     

    Recommend
    Eddy   |   We have carefully selected several similar problems for you:  1240 1016 1010 1072 1253 
    //第一道这样的题,思路大致了解:
      用最少时间找到天使 → → 天使找到最近朋友用的时间;
     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 
     7 
     8 int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1};
     9 char map[220][220];  int vis[220][220]; 
    10 int i, j, m, n;
    11 
    12 struct prision
    13 {
    14     int x, y, time;
    15     friend bool operator < (prision x, prision y)
    16     {
    17         return x.time > y.time;      //从小  →  大; 
    18     }
    19 } current, next;
    20 
    21 
    22 int BFS(int x,int y)
    23 {
    24 
    25 
    26     priority_queue <prision>q;
    27     prision current,next;
    28     memset(vis,0,sizeof(vis));
    29 
    30     current.x=x;
    31     current.y=y;
    32     current.time=0;
    33     vis[current.x][current.y]=1;
    34     q.push(current);
    35 
    36 
    37     while(!q.empty())
    38     {
    39 
    40         current=q.top();
    41         q.pop();
    42         for(int i=0;i<4;i++)
    43         {
    44             next.x=current.x+dir[i][0];
    45             next.y=current.y+dir[i][1];
    46 
    47             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y])
    48             {
    49 
    50                 if(map[next.x][next.y]=='r')
    51                     return current.time+1;
    52                 
    53                 if(map[next.x][next.y]=='x')
    54                     next.time=current.time+2;
    55                 else
    56                     next.time=current.time+1;
    57                 vis[next.x][next.y]=1;
    58                 q.push(next);
    59             }
    60         }
    61     }
    62     return -1;
    63 } 
    64 
    65 
    66 int main()
    67 {
    68     prision angle;
    69     while(~scanf("%d %d", &n, &m))
    70     {
    71         for(i=0; i<n; i++)
    72         {
    73             for(j=0; j<m; j++)
    74             {
    75                 cin >> map[i][j];
    76                 if(map[i][j] == 'a')
    77                 {
    78                     angle.x = i;
    79                     angle.y = j;
    80                 }
    81             }
    82         }
    83         int time = BFS(angle.x, angle.y);
    84         
    85         if(time == -1)
    86             cout <<"Poor ANGEL has to stay in the prison all his life."<<endl;
    87         else
    88             cout <<time<< endl;  
    89     }
    90     return 0;
    91 }  
      1 #include <queue>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 using namespace std;
      6 
      7 int n, m;
      8 //const int maxn = 220;
      9 int vis[220][220]; char map[220][220];
     10 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
     11 
     12 struct male
     13 {
     14     int x, y, step;
     15     friend bool operator < (male x, male y)
     16     {
     17         return x.step > y.step;
     18     }
     19 } s, t; 
     20 
     21 int Bfs(int x, int y)
     22 {
     23 //    int nx, ny;
     24     
     25     priority_queue <male> q;
     26     memset(vis, 0, sizeof(vis));
     27     vis[x][y] = 1;
     28     s.x = x; s.y = y; s.step = 0;
     29     q.push(s);
     30     male now;
     31     while(!q.empty())
     32     {
     33         t = q.top(); q.pop();    
     34         for(int i=0; i<4; i++)
     35         {
     36             now.x = t.x + ac[i][0];  
     37             now.y = t.y + ac[i][1];    
     38             if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < m && !vis[now.x][now.y] && map[now.x][now.y] != '#')
     39             {
     40                 if(map[now.x][now.y] == 'r')
     41                     return t.step + 1;    
     42                 else if(map[now.x][now.y] == '.')
     43                     now.step = t.step + 1;
     44                 else 
     45                     now.step = t.step + 2;
     46                 vis[now.x][now.y] = 1;
     47                  q.push(now); 
     48             }        
     49         }
     50     }
     51     return -1;  
     52 }
     53 int main()
     54 {
     55     male angle;
     56     int i, j, x, y;
     57     while(~scanf("%d %d", &n, &m))
     58     {
     59         for(int i = 0; i < n; i++)
     60         {
     61             for(int j = 0; j < m; j++)
     62             {
     63                 cin >> map[i][j];
     64                 if(map[i][j] == 'a')
     65                 {
     66                     angle.x = i;   angle.y = j;
     67                 }
     68             }
     69         }
     70         int time = Bfs(angle.x, angle.y);
     71         if(time == -1)
     72             printf("Poor ANGEL has to stay in the prison all his life.
    ");
     73         else
     74             printf("%d
    ", time);
     75     }
     76     return 0;
     77 } 
     78 
     79 /*#include <queue>
     80 #include <cstdio>
     81 #include <cstring>
     82 #include <iostream>
     83 using namespace std;
     84 const int maxn = 220;
     85 char map[maxn][maxn]; 
     86 int m, n, vis[maxn][maxn];
     87 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
     88 
     89 struct male
     90 {
     91     int x, y, t;
     92     bool friend operator < (male x, male y)
     93     {
     94         return x.t > y.t;
     95     }
     96 }s, t, now;
     97 
     98 int Bfs(int x,int y)
     99 {
    100     memset(vis, 0, sizeof(vis));
    101     priority_queue <male> q;
    102     vis[x][y] = 1;
    103     s.x = x; s.y = y; s.t = 0;
    104     q.push(s);
    105     while(!q.empty())
    106     {
    107         t = q.top(); q.pop();
    108         for(int i = 0; i < 4; i++)
    109         {
    110             now.x = t.x + ac[i][0];
    111             now.y = t.y + ac[i][1]; 
    112         //    printf("%d %d
    ", now.x, now.y);
    113             if(now.x >= 0 && now.x < m && now.y >= 0 && now.y < n && !vis[now.x][now.y] && map[now.x][now.y] != '#')  //容易错; 
    114             {
    115                 if(map[now.x][now.y] == 'r')
    116                     return t.t + 1;
    117                 else if(map[now.x][now.y] == '.')
    118                     now.t = t.t + 1;
    119                 else 
    120                     now.t = t.t + 2;
    121                 vis[now.x][now.y] = 1;                     // so important ;
    122                 q.push(now);  
    123             }
    124         }        
    125     } 
    126     return -1;
    127 }
    128 
    129 int main()
    130 {
    131     int x, y;
    132     while(~scanf("%d %d", &m, &n))
    133     {
    134         for(int i = 0; i < m; i++)
    135         {
    136             for(int j = 0; j < n; j++)
    137             {
    138                 cin >> map[i][j];
    139                 if(map[i][j] == 'a')
    140                 {
    141                     x = i; y = j;
    142                 }
    143             }
    144         }
    145             //printf("%d %d
    ", x, y);
    146             int time = Bfs(x, y);
    147         if(time == -1)
    148             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    149         else
    150             printf("%d
    ", time);
    151     }
    152     return 0;
    153 }*/
    我的渣渣码, 细节决定成败。
     
     
  • 相关阅读:
    二次开发注意
    LAMP集群项目五 nfs分发文件到服务器
    LAMP集群项目五 nfs存储的数据实时同步到backupserver
    LAMP集群项目五 项目备份
    LAMP集群项目五 部署NFS存储服务并设置WEB服务挂载
    LAMP集群项目四 安装apache、php及其插件
    iOS-单选cell的实现
    iOS-省市区选择的实现
    随机颜色的产生
    刷新轮的使用
  • 原文地址:https://www.cnblogs.com/soTired/p/4690331.html
Copyright © 2011-2022 走看看