题目:http://acm.hdu.edu.cn/showproblem.php?pid=1242

大意:迷宫搜索,'#'代表墙,'.'代表路,'x'代表守卫,每移动1格用1秒,杀死守卫用1秒,angel('a'表示)的朋友(用'r'表示),要去救她,问最短时间为多少?

分析:迷宫搜索,dfs

总结:注意读题,之前做的时候以为只有一个朋友,其实有多个,所以用'a'去找'r'比较方便。注意输入,之前输入也有错误。

代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
#define N 205
#define INF 0x7ffffff int n,m,minx;
int ex,ey;
char map[N][N];
bool vis[N][N];
int dir[][]= {{-,},{,},{,},{,-}}; bool inside(int x,int y)
{
if(x>=&&x<m&&y>=&&y<n)
return ;
return ;
} void dfs(int x,int y,int time)
{
if(map[x][y]=='r')
{
if(time<minx)
minx=time;
return;
}
if(map[x][y]=='#')
return;
if(time>minx)
return;
if(!inside(x,y))
return;
if(vis[x][y])
return;
vis[x][y]=;
for(int i=; i<; i++)
{
int mx=x+dir[i][];
int my=y+dir[i][];
if(map[mx][my]=='x')
dfs(mx,my,time+);
else
dfs(mx,my,time+);
}
vis[x][y]=;
} int main()
{
int sx,sy;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,,sizeof(vis));
minx=INF;
for(int i=;i<m;i++)
scanf("%s",map[i]);
for(int i=; i<m; i++)
for(int j=; j<n; j++)
if(map[i][j]=='a')
{
sx=i;
sy=j;
}
//for(int i=0; i<m; i++)
// printf("%s\n",map[i]);
dfs(sx,sy,);
if(minx==INF)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",minx);
}
return ;
}

HDU_1242_Rescue的更多相关文章

随机推荐

  1. iOS开发 - NSScanner的使用方法

    NSScanner这个类,用于在字符串中扫描指定的字符. 能够在创建NSScanner时指定它的string属性.然后scanner会依照要求从头到尾地扫描这个字符串中的每一个字符.扫描动作会使扫描仪 ...

  2. Wget下载多个链接

    需要wget下载多个文件链接时,可以采用如下方法: 1. 将链接存入文件url.list中: 2. wget -bc -i url.list -o [log_file] -P [target_dir] ...

  3. python爬虫【第1篇】

    一.文件读写 1.打开文件 # 以读文件模式代开new.txt f=open(r"c:\new.txt",“r”) f=open("c:\new.txt",“r ...

  4. easyUI 对话框的关闭事件

    有一个easyUI的dialog: <div id="dlg_Add" class="easyui-dialog" style=" width: ...

  5. 使用qemu

    1 获取qemu启动linux kernel的log qemu-system-x86_64 -nographic -kernel xxx -initrd xxx -append "conso ...

  6. 简易 DBUtil 封装

    Dao包结构图: 1.首先连接数据库 package com.util.db; import java.sql.Connection; import java.sql.DriverManager; i ...

  7. 解决Eclipse alt+/不出来提示的问题

    1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the proposal ...

  8. android 6.0编译时出现ERROR:Security problem ,see jack server log【转】

    本文转载自:http://blog.csdn.net/a567890k/article/details/52956798 最近编译Android6.0时经常出现以下错误 临时解决方法: Buildin ...

  9. presentModalViewController和dismissModalViewControllerAnimated的使用总结

    在实际开发中,如果要弹出视图: 我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法. presentModal ...

  10. [Codeforces 1058E] Vasya and Good Sequences

    [题目链接] https://codeforces.com/contest/1058/problem/E [算法] 显然 , 我们只需考虑序列中每个数的二进制表示下1的个数即可. 不妨令Ai表示第i个 ...