鱼C论坛

 找回密码
 立即注册
查看: 2586|回复: 6

hdu 1242

[复制链接]
发表于 2017-5-14 20:24:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[400][400];
int visit[400][400], step[400][400];
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int n, m,ax,ay;
int i, j,z;
bool t;
struct node
{
        int x, y;
};
node a, r, v1, v2,v0;
queue<node>h;
bool search()
{
        h.push(r);
        step[r.x][r.y] = 0;
        visit[r.x][r.y] = 1;
        while (h.empty()!=true)
        {
                v1 = h.front();
                h.pop();
                for ( i = 0; i < 4; i++)
                {
                        v2.x = v1.x + dir[i][0];
                        v2.y = v1.y + dir[i][1];
                        if (v2.x>=0&&v2.y>=0)
                        {
                               
                                if (v2.x == a.x&&v2.y == a.y)
                                {
                                        ax = v1.x; ay = v1.y;
                                        return true;
                                }
                                if (visit[v2.x][v2.y] == 2&&v1.x==v0.x&&v1.y==v0.y)
                                {
                                        h.push(v2);
                                        visit[v2.x][v2.y] = 1;
                                        step[v2.x][v2.y] = step[v2.x][v2.y] + 1;
                                }
                                if (v2.x < n&&v2.y < m&& map[v2.x][v2.y] != '#'&&visit[v2.x][v2.y] == 0)
                                {
                                       
                                        if (map[v2.x][v2.y] == 'x')
                                        {
                                                h.push(v1);
                                                visit[v2.x][v2.y] = 2;
                                                v0 = v1;
                                                step[v2.x][v2.y] = step[v1.x][v1.y] + 1;
                                        }
                                        if(map[v2.x][v2.y] == '.')
                                        {
                                                h.push(v2);
                                            visit[v2.x][v2.y] = 1;
                                                step[v2.x][v2.y] = step[v1.x][v1.y] + 1;
                                        }
                                }
                        }
                }
        }
        return false;
}
int main()
{
        while (scanf("%d%d", &n, &m) != EOF)
        {
                memset(map, 0, sizeof(map));
                memset(visit, 0, sizeof(visit));
                memset(step, 0, sizeof(step));
                for (i = 0; i < n; i++)
                {
                        scanf("%s", map[i]);
                        for (j = 0; j < m; j++)
                        {
                               
                                if (map[i][j] == 'a')
                                {
                                        a.x = i; a.y = j;

                                }
                                if (map[i][j] == 'r')
                                {
                                        r.x = i; r.y = j;
                                }
                        }
                }
                t = search();
                while (h.empty() != true)
                {
                        h.pop();
                }
                if (t == true)
                {
                        printf("%d\n", step[ax][ay]+1);
                }
                if (t == false)
                {
                        printf("Poor ANGEL has to stay in the prison all his life.\n");
                }
        }
        return 0;
}

过了样例,但一直WA,不知道为什么。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-14 22:07:44 | 显示全部楼层
这是一道ACM竞赛题吧?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-14 22:08:52 | 显示全部楼层
你看看时间有没有超时。在ACM中,时间才是最吼的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-14 22:45:13 | 显示全部楼层
只能观看一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-15 10:56:54 | 显示全部楼层
超凡天赐 发表于 2017-5-14 22:08
你看看时间有没有超时。在ACM中,时间才是最吼的。

超时会显示TLE的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-15 13:05:07 | 显示全部楼层

你这个用的什么算法,我看看我会不会?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-16 13:56:46 | 显示全部楼层
你这个不好调试啊,有没有调试网站?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-26 10:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表