链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6718    Accepted Submission(s): 2236

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 
Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
 
Sample Output
66
88
66

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; #define INF 0x3f3f3f3f
#define N 210 struct node
{
int x, y, step;
}; int n, m, a[N][N], dir[][]={{-,},{,},{,-},{,}};
char G[N][N];
int vis[N][N]; void BFS(node s, int num)
{
node p, q; queue<node>Q;
Q.push(s);
memset(vis, , sizeof(vis));
vis[s.x][s.y] = ; while(Q.size())
{
p = Q.front(), Q.pop(); if(G[p.x][p.y]=='@')
{
if(num==)
a[p.x][p.y] = p.step;
if(num==)
a[p.x][p.y] += p.step;
} for(int i=; i<; i++)
{
q.x = p.x + dir[i][];
q.y = p.y + dir[i][];
q.step = p.step + ; if(!vis[q.x][q.y] && q.x>= && q.x<n && q.y>= && q.y<m && G[q.x][q.y]!='#')
{ vis[q.x][q.y] = ;
Q.push(q);
}
}
}
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
int i, j;
node Y, M; memset(a, , sizeof(a)); for(i=; i<n; i++)
{
scanf("%s", G[i]);
for(j=; j<m; j++)
{
if(G[i][j]=='Y')
Y.x=i, Y.y=j, Y.step=;
if(G[i][j]=='M')
M.x=i, M.y=j, M.step=;
}
} BFS(Y, );
BFS(M, ); int ans=INF; for(i=; i<n; i++)
for(j=; j<m; j++)
if(G[i][j]=='@' && vis[i][j])
ans = min(ans, a[i][j]); printf("%d\n", ans*); }
return ;
}

(广搜) Find a way -- hdu -- 2612的更多相关文章

  1. hdu 2612:Find a way(经典BFS广搜题)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  3. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  4. Combine String HDU - 5707 dp or 广搜

    Combine String HDU - 5707 题目大意:给你三个串a,b,c,问a和b是不是恰好能组成c,也就是a,b是不是c的两个互补的子序列. 根据题意就可以知道对于c的第一个就应该是a第一 ...

  5. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  6. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  7. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 1253 (简单三维广搜) 胜利大逃亡

    奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...

  9. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

  10. hdu 1175 连连看 (广搜,注意解题思维,简单)

    题目 解析见代码 #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数, ...

随机推荐

  1. 转HTTP协议 --- Cookie

    转自:http://www.cnblogs.com/TankXiao/archive/2013/04/15/2848906.html Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish ...

  2. 【linux】head&&tail

    命令:     head[-n][文件名]                                    tail[-n][-f][文件名] 形式:     -n  行数 (显示前几行)    ...

  3. 在vc++上简单搭建环境(包括文件引用)

    1,triplet_head.h 文件 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVER_FLOW -2 ...

  4. 黄聪:360浏览器、chrome开发扩展插件教程(2)为html添加行为

    转载:http://www.cnblogs.com/walkingp/archive/2011/04/02/2002668.html 上一节我们已经讲了Chrome扩展的基础知识,并构建了基础的htm ...

  5. 【ZZ】各类程序开发语言概述 | 菜鸟教程

    http://www.runoob.com/w3cnote/programming-intro.html 各类程序开发语言概述, 点击查看大图:

  6. Android开发——JVM、Dalvik以及ART的区别【转帖】

    转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52354964 0. 前言 Dalvik是Google公司自己设计用于Android平 ...

  7. leetcode434

    public class Solution { public int CountSegments(string s) { s = s.Trim(); ) { ; } else { ; ; ; i &l ...

  8. Python运维开发基础05-语法基础

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...

  9. S 联系人新增及更新

    一.联系人新增 [Public] ConnectString=host="siebel://10.10.1.139:2321/SBA_82/SMObjMgr_chs ConnectUserN ...

  10. unity平行光太亮?物体发白?可能你使用了2个或多个平行光

    unity平行光太亮?物体发白?可能你使用了2个或多个平行光 今天做项目时就遇到了这个问题,光亮得让物体发白 发现加载的场景 里面有个 平行光,删了就好了 要是感觉还是太亮,就把主平行光的Intens ...