Rescue

Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1407 Accepted Submission(s): 548
 
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
题目大意:给出地图,天使的位置用a表示(终点),r表示天使的朋友(起点,不一定只有一个),x是守卫,.是路,#是墙
可以往四个方向走,每走一步耗费1个体力,碰见守卫再+1,问你天使见到朋友需要多长时间。
bfs 吧终点起点倒过来,这样起点唯一,相当于找最近的一个r。
#include<queue>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define N 1234
int n, m, a, b, x, y, cnt, ans;
struct node
{
    int x, y, time;
    bool operator < (const node & t)const
    {
        return time > t.time;   
    }
}st;
priority_queue<node>q; 
char mat[N][N];
int v[N][N];
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
int bfs()
{
    memset(v ,0 , sizeof(v));
    while(!q.empty()) q.pop();
    q.push(st);
    v[st.x][st.y] = true;
    while(!q.empty())
    {
        st = q.top();  
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            node t = st;
            t.x += dx[i], t.y +=dy[i];
            if(v[t.x][t.y] || mat[t.x][t.y]=='#')
                     continue;
            if(t.x<1 || t.y<1 || t.x>n || t.y>m)
                     continue;
            if(mat[t.x][t.y] == '.')t.time++;
            if(mat[t.x][t.y] == 'x')t.time+=2;
            if(mat[t.x][t.y] == 'r') return t.time+1;
            q.push(t);
            v[t.x][t.y] = true;
            }
    }
    return -1;
}
int main()
{
    while(cin>>n>>m)
    {
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            scanf(" %c", &mat[i][j]);
            if(mat[i][j] == 'a')
                st.x = i, st.y = j, st.time = 0;
        }
        ans = bfs();
        if(ans == -1)puts("Poor ANGEL has to stay in the prison all his life.");
        else cout<<ans<<endl;
    }
    return 0;
}

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible Rescue是一种用于处理故障和恢复的方法。可以使用Ansible Rescue来重新安装操作系统、修复配置文件、恢复数据等。在Ansible中,可以通过编写一个playbook来定义Rescue任务,该playbook包含了需要执行的一系列任务。在执行Rescue任务时,Ansible会评估任务的返回代码,以确定任务是成功还是失败。当任务失败时,Ansible会立即中止play的其余部分,并跳过所有后续任务。此外,还可以使用Ansible的fail模块来强制任务失败,以便进行特定的处理。例如,可以编写一个任务来运行用户创建脚本,并在脚本输出中检查是否缺少密码,如果缺少密码,则使用fail模块报告脚本失败。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [install-arch-linux-server:提供一个命令,通过运行的 Ubuntu live 在服务器上格式化和安装 arch linux](https://download.csdn.net/download/weixin_42157567/19894698)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ansible实施处理程序和处理失败任务](https://blog.csdn.net/weixin_58130165/article/details/119143078)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值