hdoj---Rescue
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 60 Accepted Submission(s) : 22
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.)
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
13#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 210
using namespace std;
char a[N][N];
int b[N][N];
int n,m,ax,ay;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
struct zz
{
int x,y,ans;
friend bool operator<(zz x,zz y)
{
return x.ans>y.ans;/*按照时间从小到大排序*/
}
};
void bfs(int x,int y)
{
memset(b,0,sizeof(b));
priority_queue<zz>q;
zz f1,f2;
f1.x=x;f1.y=y;
f1.ans=0;/*第一部一定要让ans=0*/
q.push(f1);
b[x][y]=1;
while(!q.empty())
{
f1=q.top() ;
q.pop();
if(a[f1.x][f1.y]=='r')
{
printf("%d\n",f1.ans);
return ;
}
for(int i=0;i<4;i++)
{
f2.x=f1.x+dx[i];
f2.y=f1.y+dy[i];
if(f2.x>0&&f2.x<=n&&f2.y>0&&f2.y<=m&&!b[f2.x][f2.y]&&a[f2.x][f2.y]!='#')
{/*没有越界不是墙壁没有被用过的点执行下一步操作*/
b[f2.x][f2.y]=1;
if(a[f2.x][f2.y]=='x')
f2.ans=f1.ans+2;/*遇到x时间加1*/
else
f2.ans=f1.ans+1;
q.push(f2);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");/*当遍历一遍之后还未找到人r*/
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%s",a[i]+1);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]=='a')
{
ax=i;ay=j;/*找到a的坐标*/
}
}
}
bfs(ax,ay);
}
return 0;
}
hdoj---Rescue的更多相关文章
- hdoj 1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 【HDOJ】4057 Rescue the Rabbit
挺有意思的一道题目,解法是AC自动机+DP.AC自动机建立fail指针时,一定要注意结点的属性也需要传递.AC自动机结合了trie和kmp的优点.需要注意的是,每个模式串仅计算一次,否则这题很难解. ...
- 【HDOJ】1242 Rescue
BFS+优先级队列. #include <iostream> #include <cstdio> #include <cstring> #include <q ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- BFS HDOJ 1242 Rescue
题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...
- 搜索专题: HDU1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)
本节我们讨论 Suspend/Resume 和 Rescue/Unrescue 这两组操作. Suspend/Resume 有时需要长时间暂停 instance,可以通过 Suspend 操作将 in ...
随机推荐
- HTML5 postMessage 和 localStorage 实现窗口间通信
LocalStorage(不能跨域) 基本思想:通过localStorage的标准事件storage来实现跨页面通信,即页面A通过写入特定数据触发页面B的storage事件,页面B响应之后再写入数据通 ...
- Windows下错误码全解析
windows系统下,调用函数出错时.可以调用GetLastError函数返回错误码.但是GetLastError函数返回值是DWORD类型,是一个整数.如果想要知道函数调用的真正错误原因,就需要对这 ...
- CSS框架Bootstrap
作为一个软件开发人员,经常接触和使用框架是再平常的事情不过了.但是这些框架基本都是和语言相关的,比如WEB框架SpringMVC,JavaEE框架Spring,ORM框架Hibernate,还有Jav ...
- 使用File类操作文件或目录的属性
在学I/O流之前,我先总结一下使用File类操作文件或目录的属性. package com.File; import java.io.File; import java.io.IOException; ...
- Architecture:话说科学家/工程师/设计师/商人
从使命.目的.行为的不同,可以归类人群到科学家.工程师.设计师.商人等等.使命分别是:1.携带当下社会的财富对未来探索,希望引发变革:2.掌握工程全貌.完成整个工程的圣经周期:3.在工程的设计层面做文 ...
- 从输入url到页面展示出来经历了哪些过程
本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址. 此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...
- struct-计算机学习日志
STRUCT实验目的模拟缓冲区溢出的情况.代码总览#include <stdio.h>#include <stdlib.h>typedef struct { int a[2]; ...
- 【剑指Offer】36、两个链表的第一个公共结点
题目描述: 输入两个链表,找出它们的第一个公共结点. 解题思路: 本题首先可以很直观的想到蛮力法,即对链表1(假设长度为m)的每一个结点,遍历链表2(假设长度为n),找有没有与其相同的 ...
- C#第十五节课
函数复习 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...
- swift UITableViewCell 中的单选控制样式
我昨天在网上找了一晚上的资料,但是大多都是OC得语法,swift资料实在是太少了,使得我这个刚入门swift的彩笔好不吃力,后面一直各种翻阅资料,终于让我找到了 visibleCells 这个方法,直 ...