找到朋友的最短时间

Sample Input
7 8
#.#####. //#不能走 a起点 x守卫 r朋友
#.a#..r. //r可能不止一个
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output
13

bfs+优先队列

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int n, m;
char map[][];
int sx, sy;
bool flag; struct node
{
int x , y , step ;
bool operator <(const node &t) const
{
return step > t.step ;
}
}; int dx[] = {,,,-} ;
int dy[] = {,-,,} ; void bfs()
{
node now , t ;
int i , fx ,fy ;
priority_queue<node> q ;
now.x = sx ;
now.y = sy ;
now.step = ;
q.push(now) ;
map[sx][sy] = '#' ;
while(!q.empty())
{
now = q.top() ;
q.pop() ;
for (i = ; i < ; i++)
{
fx = now.x + dx[i] ;
fy = now.y + dy[i] ;
if (fx< || fy< || fx >=n || fy >=m ||map[fx][fy] == '#')
continue ;
if (map[fx][fy] == 'r')
{
printf("%d\n" , now.step+) ;
flag = ;
return ;
}
if (map[fx][fy] == 'x')
{
t.x = fx ;
t.y = fy ;
t.step = now.step + ;
q.push(t) ;
}
else
{
t.x = fx ;
t.y = fy ;
t.step = now.step + ;
q.push(t) ;
}
map[fx][fy] = '#' ; }
} } int main()
{
// freopen("in.txt","r",stdin) ;
while (scanf("%d %d" , &n , &m) !=EOF)
{
int i , j ;
for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j] ;
if (map[i][j] == 'a')
{
sx = i ;
sy = j ;
}
}
}
flag = ;
bfs() ;
if (!flag)
printf("Poor ANGEL has to stay in the prison all his life.\n") ;
} return ;
}

dfs

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; char map[][] ;
bool visit[][] ; int n , m ;
int MIN ; void dfs(int x , int y , int sum)
{
if (x< || y< || x>= n || y>= m)
return ;
if (map[x][y] == '#')
return ;
if (sum >= MIN)
return ;
if (visit[x][y] == )
return ;
if (map[x][y] == 'r')
{
if (sum < MIN)
MIN = sum ;
return ;
}
if (map[x][y] == 'x')
sum++ ;
visit[x][y] = ;
dfs(x+,y,sum+) ;
dfs(x-,y,sum+) ;
dfs(x,y+,sum+) ;
dfs(x,y-,sum+) ;
visit[x][y] = ;
} int main()
{
// freopen("in.txt","r",stdin) ; while (scanf("%d %d" , &n , &m) !=EOF)
{
if (n == && m == )
break ;
memset(visit , ,sizeof(visit)) ;
int i , j ;
int bx , by ;
int sum = ; for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j];
if (map[i][j] == 'a')
{
bx = i ;
by = j ; }
} } MIN = INT_MAX ;
dfs(bx,by,sum) ;
if (MIN != INT_MAX)
printf("%d\n" , MIN) ;
else
printf("Poor ANGEL has to stay in the prison all his life.\n") ; } return ;
}

hdu 1242 找到朋友最短的时间 (BFS+优先队列)的更多相关文章

  1. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  2. HDU - 4198 Quick out of the Harbour (BFS+优先队列)

    Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...

  3. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  4. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  5. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  6. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  7. F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  8. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. FBI阅人术——用最短的时间了解一个人

    FBI阅人术--用最短的时间了解一个人 和陌生人第一次见面时,要如何在一开始谈话的几分钟内,了解这个人?如何和对方拉近距离?如何找到对方喜爱的话题?如何让对方愿意开口? 这都得依靠细心而入微的观察力, ...

随机推荐

  1. CodeForces - 907A Masha and Bears

    A. Masha and Bears time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. java中CRUD(增删查改)底层代码的实现

    java中CRUD(增删查改)底层代码的实现: package com.station.dao; import com.station.model.Product; import java.sql.* ...

  3. Zabbix LLD 设置过滤条件,不自动监控某些item

    1.需求描述        默认情况下Zabbix 自带模板 "Template OS Linux" 中网络接口LLD自动发现除还回接口外的所有接口,当这并不一定是我们想要的结果. ...

  4. JMS学习(七)-ActiveMQ消息的持久存储方式之KahaDB存储

    一,介绍 自ActiveMQ5.4以来,KahaDB成为了ActiveMQ默认的持久化存储方式.相比于原来的AMQ存储方式,官方宣称KahaDB使用了更少的文件描述符,并且提供了更快的存储恢复机制. ...

  5. C#复杂类型序列化

    [Serializable] public class CardItemInfo { private int lineWidth;//线宽 private CardItemInfo childCard ...

  6. Linux - 操作系统信号

    linux操作系统提供的信号 kill -l # 查看linux提供的信号 trap # shell使用 trap 捕捉退出信号 # 发送信号一般有两种原因: # (被动式) 内核检测到一个系统事件. ...

  7. Redis 主从模式

    系统:Centos6.6x64安装目录:/usr/local/主:192.168.100.103从:192.168.100.104 ,下载安装: 安装依赖: # yum install gcc tcl ...

  8. W3C规范

    连接:https://www.w3cschool.cn/xuexiw3c/xuexiw3c-standards.html W3C 代码标准规范 由 路飞 创建, 最后一次修改 2017-01-03 W ...

  9. [C++]数组与指针[二维数组与指针]

  10. [C++]线性链表之顺序表<二>

    /*   @content 线性链表之顺序表   @date 2017-3-21 1:06   @author Johnny Zen  */ /* 线性表     顺序表     链式表[带头指针/不 ...