zoukankan      html  css  js  c++  java
  • hdu 1242 Rescue(bfs+优先队列)

    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+优先队列...
    bfs能找到最优解的原因是...时间更短的点一定是先访问到先入队...
    但是由于守卫的存在...先访问到的节点如果是守卫的话就不是时间更短的节点...
    所以这个时候我们要用优先队列...
    第一次写.
    然后学习了重载<的写法...写在结构体里面和外面的..
    感觉q老师...窝只是请教他这个重载在写结构体里面要怎样...他就把kuangbin的模板发了我一份
    学到了不少.
    还有就是,因为朋友有多个,而小天使只有一个...
    不如让小天使去找朋友...逆向思维...
    一遍ac,开心.
      1 /*************************************************************************
      2     > File Name: code/hdu/1242.cpp
      3     > Author: 111qqz
      4     > Email: rkz2013@126.com 
      5     > Created Time: 2015年10月02日 星期五 14时38分17秒
      6  ************************************************************************/
      7 
      8 #include<iostream>
      9 #include<iomanip>
     10 #include<cstdio>
     11 #include<algorithm>
     12 #include<cmath>
     13 #include<cstring>
     14 #include<string>
     15 #include<map>
     16 #include<set>
     17 #include<queue>
     18 #include<vector>
     19 #include<stack>
     20 #include<cctype>
     21 using namespace std;
     22 #define yn hez111qqz
     23 #define j1 cute111qqz
     24 #define ms(a,x) memset(a,x,sizeof(a))
     25 #define lr dying111qqz
     26 const int dx4[4]={1,0,0,-1};
     27 const int dy4[4]={0,-1,1,0};
     28 typedef long long LL;
     29 typedef double DB;
     30 const int inf = 0x3f3f3f3f;
     31 const int N=2E2+5;
     32 char maze[N][N];
     33 int m,n;
     34 int ans;
     35 bool vis[N][N];
     36 struct node
     37 {
     38     int x,y;
     39     int d;
     40 
     41     bool ok ()
     42     {
     43     if (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&maze[x][y]!='#') return true;
     44     return false;
     45     }
     46     bool hasguard()
     47     {
     48     if (maze[x][y]=='x') return true;
     49     return false;
     50     }
     51     bool goal()
     52     {
     53     if (maze[x][y]=='r') return true;
     54     return false;
     55     }
     56 }s;
     57 
     58 bool operator<(const node &a,const node &b)    //重载优先级的<关系...返回true时优先级更小,在队列的更后面.
     59 {
     60     return a.d>b.d;
     61 }
     62 
     63 bool bfs()
     64 {
     65     priority_queue<node>q;
     66     q.push(s);
     67     
     68     while (!q.empty())
     69     {
     70     node pre = q.top();
     71     q.pop();
     72 //    cout<<pre.x<<" "<<pre.y<<" "<<pre.d<<endl;
     73     if (pre.goal())
     74     {
     75         ans = pre.d;
     76         return true;
     77     }
     78 
     79     for ( int i = 0 ; i < 4 ; i++)
     80     {
     81         node next;
     82         next.x = pre.x + dx4[i];
     83         next.y = pre.y + dy4[i];
     84         next.d = pre.d + 1;
     85         if (!next.ok()) continue;
     86         vis[next.x][next.y] = true;
     87         if (next.hasguard()) next.d++;
     88         q.push(next);
     89         
     90 
     91     }
     92     }
     93     return false;
     94 }
     95 int main()
     96 {
     97   #ifndef  ONLINE_JUDGE 
     98    freopen("in.txt","r",stdin);
     99   #endif
    100     while(scanf("%d %d",&n,&m)!=EOF){
    101     ms(vis,false);
    102     for ( int i = 0 ; i < n ; i++) scanf("%s",maze[i]);
    103     for ( int i = 0 ; i < n ; i++)
    104     {
    105     for ( int j = 0 ; j < m ; j++)
    106     {
    107         if (maze[i][j]=='a')
    108         {
    109         s.x = i ;
    110         s.y = j ;
    111         s.d = 0 ;
    112         vis[i][j] = true;
    113         break;
    114         }
    115     }
    116     }
    117 
    118     if (bfs())
    119     {
    120     printf("%d
    ",ans);
    121     }
    122     else
    123     {
    124     puts("Poor ANGEL has to stay in the prison all his life.");
    125     }
    126 }
    127  #ifndef ONLINE_JUDGE  
    128   fclose(stdin);
    129   #endif
    130     return 0;
    131 }
    View Code
     
     
  • 相关阅读:
    C# partial 作用
    C#中internal关键字是什么意思?什么叫做“只能在包含它的程序集中访问该方法”
    [转]利用.NET中的反射机制实现IList到DataTable的转换
    你可能已经知道或者不知道的ASP.NET 2.0技巧
    SQL Server基本函数详细介绍--字符串函数
    在Web.config配置文件中自定义配置节点
    SQL SERVER事务处理
    专用于SqlServer2005的高效分页存储过程(支持多字段任意排序,不要求排序字段唯一)
    SQL SERVER 高效存储过程分页(Max/Min方法)
    该字符串未被识别为有效的 DateTime
  • 原文地址:https://www.cnblogs.com/111qqz/p/4853964.html
Copyright © 2011-2022 走看看