• hdu Problem 1242 Rescue bfs + 优先队列


    问题地址

    Rescue

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


    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
    这是刚写的, 因为题中说有多个朋友,所以原来的就不太对了
    #include <bits/stdc++.h> using namespace std; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; bool vis[205][205]; char mp[205][205]; int ex, ey, n, m; struct node{     int x, y, step;     friend bool operator < (node a, node b) {         return a.step > b.step;     } }temp, a, b; int bfs(node x) {     priority_queue<node> que;     x.step = 0;     vis[x.x][x.y] = true;     while (!que.empty()) que.pop();     que.push(x);     while (que.size()) {         a = que.top();         que.pop();         //printf("%d %d %d ", a.x, b.y, a.step);         if (a.x == ex && a.y == ey) return a.step;         for (int i = 0; i < 4; i++) {             b.x = a.x + dx[i];             b.y = a.y + dy[i];             b.step = a.step + 1;             if (b.x < 0 || b.x >= n || b.y < 0 || b.y >= m || mp[b.x][b.y] == '#')                 continue;             if (vis[b.x][b.y]) continue;             if (mp[b.x][b.y] == 'x') b.step++;             que.push(b);             vis[b.x][b.y] = true;         }     }     return -1; } int main() {     while (scanf("%d%d", &n, &m) != EOF) {         for (int i = 0; i < n; i++) {             scanf("%s", mp[i]);             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'a') {                     ex = i, ey = j;                   //  printf("%d %d ", ex, ey);                 }             }         }         int ans = 1e9;         for (int i = 0; i < n; i++) {             for (int j = 0; j < m; j++) {                 if (mp[i][j] == 'r') {                     memset(vis, false, sizeof(vis));                     temp.x = i, temp.y = j;                     int t = bfs(temp);                     if (t != -1)                         ans = min(ans, t);                 }             }         }         if (ans != 1e9)              printf("%d ", ans);         else printf("Poor ANGEL has to stay in the prison all his life. ");     }     return 0; }
    这是原来的:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #define MIN(a, b)	(a < b)? a: b
    #define MAX_N 1000
    using namespace std;
    typedef pair<int, int> P;
    const int INF = 99999;
    char maze[MAX_N][MAX_N];
    bool vis[MAX_N][MAX_N];
    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};
    int w, h;
    struct node {
        int x;
        int y;
        int step;
        friend bool operator < (node a,node b)
        {
            return a.step > b.step;
        }
    }temp, Next;
    int bfs(int x, int y ,int x2, int y2)
    {
    	memset(vis, false, sizeof(vis));
    	priority_queue<node> que;
    	if (!que.empty())	que.pop();
    	temp.x = x, temp.y = y;
    	temp.step = 0;
    	que.push(temp);
    	while (que.size()) {
    		temp = que.top();
    		que.pop();
    		if (temp.x == x2 && temp.y == y2)	return temp.step;
    		for (int i = 0; i < 4; i++) {
    			Next.x = dx[i] + temp.x;
    			Next.y = dy[i] + temp.y;
    			if (Next.x >= h || Next.y >= w || Next.x < 0 || Next.y < 0 || maze[Next.x][Next.y] == '#' || vis[Next.x][Next.y])	continue;
    			Next.step = temp.step + 1;
    			vis[Next.x][Next.y] = true;
    			if (maze[Next.x][Next.y] == 'x') {
    				Next.step++;
    			}
    			que.push(Next);
    		}
    	}
    	return -1;
    }
    int main()
    {
    	int sy, sx, ex, ey;
    	while (scanf("%d %d", &h, &w) != EOF) {
    		int ans;
    		for (int i = 0; i < h; i++) {
    			scanf("%s", &maze[i]);
    		}
    		for (int i = 0; i < h; i++) {
    			for (int j = 0; j < w; j++) {
    				if(maze[i][j] == 'r')
                    {
                        sx = i;
                        sy = j;
                    }
                    if(maze[i][j] == 'a')
                    {
                        ex = i;
                        ey = j;
                    }
    			}
    		}
    		ans = bfs(sx, sy, ex, ey);
    		if (ans == -1)	printf("Poor ANGEL has to stay in the prison all his life.
    ");
    		else	printf("%d
    ", ans);
    	}
    	return 0;
    }
    

  • 相关阅读:
    C++成员函数在内存中的存储方式
    C++重写(覆盖)、重载、重定义、
    C++中的覆盖与隐藏(详细讲解)
    c++中被忽视的隐藏
    C++对象的内存分布和虚函数表
    C++ explicit关键字详解
    命名空间 extern的用法 static全局变量
    extern和include的作用
    extern用法总结
    KMP算法
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770961.html
Copyright © 2020-2023  润新知