Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2783109
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-08-07 19:32:03


Angel所在的位置是a;  Angel的朋友用r表示, Angel可能有多个朋友 ,也就是可能有多个r ;   #表示墙  ;x表示警卫  ,可能会有多个警卫;
Angel的朋友要进入a  只能上下左右的走, 每走一个格用一个单位时间,如果遇上警卫,杀掉警卫要花一个单位时间   问Angel能否救到
angel,
如果能 求最少的时间被这道题的有两个地方震了
   1.有多个r搜r到a有多条路最后比较   但是倒过来想 从a搜到r就搜一条最佳路
   2.涉及到一个杀警卫的时间,所以queue队列中的node 不是按时间大小排列的, 用priority_queue,
  第一学着次用priority_queue, 在此感谢cxl 
与平常的BFS不同,目标有多个,那么BFS搜索到的目标点不一定是最小的
所以每次拿最小的时间点来扩展(而不是平常的先进先出点来扩展)
 

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<queue>
  8. using namespace std;

  9. const int maxn = 201;
  10. struct Node {
  11.     int x, y, tme;
  12.     friend bool operator < (const Node &a, const Node &b) {
  13.         return a.tme > b.tme;
  14.     }//时间从小到大
  15. };

  16. int n, m;
  17. int sx, sy;
  18. char map[maxn][maxn];
  19. int mark[maxn][maxn];
  20. int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  21. bool Overmap(int x, int y) {
  22.     if (x < 0 || x >= n || y < 0 || y >= m) {
  23.         return true;
  24.     }
  25.     return false;
  26. }

  27. inline int BFS() {
  28.     memset(mark, 0, sizeof(mark));
  29.     priority_queue<Node> Q;
  30.     Node first, next;
  31.     first.x = sx, first.y = sy;
  32.     first.tme = 0;
  33.     mark[sx][sy] = true;
  34.     Q.push(first);
  35.     while (!Q.empty()) {
  36.         first = Q.top();
  37.         Q.pop();
  38.         //找到,退出
  39.         if (map[first.x][first.y] == 'r') {
  40.             return first.tme;
  41.         }
  42.         //四次循环分别对应四个方向
  43.         for (int i = 0; i < 4; i++) {
  44.             next.x = first.x + dir[i][0];
  45.             next.y = first.y + dir[i][1];
  46.             if (Overmap(next.x, next.y) || map[next.x][next.y] == '#' || mark[next.x][next.y]) {
  47.                 continue;
  48.             }
  49.             if (map[next.x][next.y] == 'x') {
  50.                 next.tme = first.tme + 2;
  51.             } else {
  52.                 next.tme = first.tme + 1;
  53.             }
  54.             mark[next.x][next.y] = 1;//标志已经走过的点
  55.             Q.push(next);//入队对它进行扩展
  56.         }
  57.     }
  58.     return -1;
  59. }

  60. int main() {
  61.     while (~scanf("%d%d", &n, &m)) {
  62.         for (int i = 0; i < n; i++) {
  63.             scanf("%s", map[i]);
  64.             for (int j = 0; j < m; j++) {
  65.                 if (map[i][j] == 'a') {
  66.                     sx = i, sy = j;
  67.                 }
  68.             }
  69.         }//for(i)
  70.         int res = BFS();
  71.         if (res == -1) {
  72.             puts("Poor ANGEL has to stay in the prison all his life.");
  73.         } else {
  74.             printf("%d\n", res);
  75.         }
  76.     }
  77.     return 0;
  78. }

阅读(2730) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~