zoukankan      html  css  js  c++  java
  • ZOJ 1649: Rescue(BFS)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

    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

    题意分析:

    一个人被抓进了监狱,现在他的盆友要去救他, 每次只能上下左右移动,每移动一次需要1分钟, 监狱中可能会有守卫, 杀死一个守卫需要1分钟,也就是说想到达一个有守卫的地方需要2分钟,假设能够杀死所有守,求能救人所需要的最短时间。

    解题思路:

    遍历所有能够走到的格子,需要注意的是每次杀死一个守卫需要的时间更长,所以不直接加入原队列之中,如果直接加入可能后面会先处理这个步数更长的,而忽略了后面加入的只加1步的。

    #include <stdio.h>
    #include <string.h>
    #include <queue>
    #define N 220
    using namespace std;
    int n, m;
    char str[N][N];
    bool book[N][N];
    struct date{
    	int x;
    	int y;
    	int temp;
    };
    int bfs(int x, int y)
    {
    	int tx, ty;
    	int nex[4][2]={0,1, 0,-1, 1,0, -1,0};
    	memset(book, false, sizeof(book));
    	queue<date>q, p;
    	q.push(date{x, y, 0});
    	book[x][y]=true;
    	while(!q.empty() || !p.empty())
    	{
    		date u, v;
    		if(q.empty())
    		{
    			u=p.front();
    			p.pop();
    		}
    		else if(p.empty())
    		{
    			u=q.front();
    			q.pop();
    		}
    		else
    		{
    			u=q.front();
    			v=p.front();
    			if(u.temp>v.temp)
    			{
    				u=v;
    				p.pop();
    			}
    			else
    				q.pop();
    		}	
    		if(str[u.x][u.y]=='a')
    			return u.temp;
    			
    		for(int i=0; i<4; i++)
    		{
    			tx=u.x+nex[i][0];
    			ty=u.y+nex[i][1];
    			if(tx>=n || ty>=m || tx<0 || ty<0)
    				continue;
    			if(str[tx][ty]=='#')
    				continue;
    			if(str[tx][ty]=='.' && book[tx][ty]==false)
    			{
    				book[tx][ty]=true;
    				q.push({tx, ty, u.temp+1});	
    			}
    			if(str[tx][ty]=='x' && book[tx][ty]==false)
    			{
    				book[tx][ty]=true;
    				p.push({tx, ty, u.temp+2});
    			}
    			if(str[tx][ty]=='a')
    				return u.temp+1;
    		}
    	}
    	return -1;
    }
    int main()
    {
    	int i, j, ans;
    	while(scanf("%d%d", &n, &m)!=EOF)
    	{
    		ans=-1;
    		for(i=0; i<n; i++)
    			scanf("%s", str[i]);
    		for(i=0; i<n; i++)
    			for(j=0; j<m; j++)
    				if(str[i][j]=='r')
    				{
    					ans=bfs(i, j);
    					break;
    				}
    		if(ans==-1)
    			printf("Poor ANGEL has to stay in the prison all his life.
    ");
    		else
    			printf("%d
    ", ans); 
    		
    	}
    	return 0;
    }
  • 相关阅读:
    BZOJ 2654: tree
    洛谷P1972[SDOI2009]HH的项链
    洛谷 P3833 [SHOI2012]魔法树
    P2167 [SDOI2009]Bill的挑战
    洛谷 P2145 [JSOI2007]祖码
    洛谷 P4170 [CQOI2007]涂色
    P2024 [NOI2001]食物链
    USACO 2012 December ZQUOJ 24122 Scrambled Letters(二分)
    USACO 2012 December ZQUOJ 24128 Wifi Setup(动态dp)
    2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852553.html
Copyright © 2011-2022 走看看