Rescue

原创 2018年04月15日 20:38:15
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

ps:这道题,说实话,第一时间想到的肯定是用广搜做,但是用深搜做了之后发现有一个有意思的现象,题目中的friend是一个复数,也就说,不止一个朋友,那么,如果用深搜从'r’开始找‘a’,就会超时(也算是一个坑点吧),这个时候,我们就可以用你逆向思维了,用‘a’去找‘r’,碰到第一个‘r’的过程中的最短路径就是我们要得到的答案。

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int n,m,ii,jj,vis[250][250],ok,ans;
char s[250][250];
void dfs(int x,int y,int step)
{
    if(x<0||x>n-1||y<0||y>m-1||vis[x][y]==1||s[x][y]=='#') return ;
    if(step>=ans)
        return ;//剪枝
    if(s[x][y]=='r')
    {
        ok=1;
        ans=min(step,ans);
        return ;
    }
    if(s[x][y]=='x') step++;
    vis[x][y]=1;
    dfs(x+1,y,step+1);
    dfs(x-1,y,step+1);
    dfs(x,y+1,step+1);
    dfs(x,y-1,step+1);
    vis[x][y]=0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
            scanf("%s",s[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(s[i][j]=='a')
                {
                    ii=i;
                    jj=j;
                    break;
                }
            }
        }
        ok=0;
        ans=999999999;
        dfs(ii,jj,0);
        if(ok)
            printf("%d\n",ans);
        else printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
}

电脑启动进入rescue mode的解决方案

问题windwos系统升级以后出现如下错误:error: no such partition. Entering rescue mode... grub rescue>开机即进入这个模式,无法进入系统...
  • u010896929
  • u010896929
  • 2016-10-28 18:19:05
  • 4601

Ubuntu14.04启动出现grub rescue

Ubuntu14.04启动出现grub rescue
  • u011762313
  • u011762313
  • 2015-10-07 09:38:01
  • 1666

关于Windows 开机出现grub rescue>问题的解决方法

在Windows10之后又装了一个ubuntu的双系统,启动引导应该是在ubuntu的盘符上,后来我格式化了ubuntu的盘符,再开机的时候出现了 error:no such partition ...
  • qq_16094215
  • qq_16094215
  • 2017-08-23 20:26:04
  • 471

error: no such device: xxx xxx Entering rescue mode... grub rescue >

本文目的:本人这几天在安装双系统,win7 + ubuntu,对Linux和系统层不是很熟悉的我,也确实碰到了挺多问题的。本文主要针对“系统引导”方面的问题做一些整理。具体问题描述如下: 1、本人的P...
  • Houchaoqun_XMU
  • Houchaoqun_XMU
  • 2017-05-19 00:47:36
  • 2183

no such device Entering rescue mode grub rescue

电脑开始出现这个 no such device 电脑开机出现这个界面。 首先说下我怎么会出现这个界面的吧,我是装了双系统,win10 和Ubuntu 。我的Ubuntu装好后,鼠标没用,所以我就在...
  • zhou906767220
  • zhou906767220
  • 2017-03-21 22:39:39
  • 1666

Ruby最佳实践--不要rescue Exception,rescue StandardError

重构前显式地捕获Exception将会捕获甚至会捕获非正常可修复的错误比如SyntaxError, LoadError, and Interrupt。begin foo rescue Except...
  • liuk10
  • liuk10
  • 2016-04-07 14:31:52
  • 840

引导出错进入grub rescue导致的无法正常开机

引导出错进入grub rescue导致的无法正常开机系统升级、双系统安装、格式化第二系统(ubuntu)磁盘等可能导致标签(空格分隔):win-问题产生 (双系统)格式化ubuntu系统所在...
  • hust_sheng
  • hust_sheng
  • 2015-11-20 11:52:42
  • 1027

openSUSE grub rescue 修复经历 opensuse trying manual resume 修复经历

所以这算是一天中遇到的两个不幸的问题,好在是可以在一天之内把这些问题解决了。 openSUSE Grub Rescue 这个问题就相对比较好解决了 列出分区 ls 选择分区,设置分区 set...
  • gmszone
  • gmszone
  • 2013-11-21 14:21:49
  • 4553

在win7下格式化ubuntu分区,开机出现grub rescue解决办法

电脑是双系统,
  • shaguakaimen123
  • shaguakaimen123
  • 2014-10-07 23:19:01
  • 3398

linux修复开机出现grub rescue模式

搜了点网上的资料、发现都是修复MBR分区的、我的电脑是GPT分区+UEFI、虽然不同、但也大同小异、 出现grub rescue模式一般是硬盘分区出现变动、导致旧的grub找不到启动文件而出现的、 今...
  • Listener_ri
  • Listener_ri
  • 2015-01-20 14:48:37
  • 2691
收藏助手
不良信息举报
您举报文章:Rescue
举报原因:
原因补充:

(最多只允许输入30个字)