链接:

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. XSS获取cookie并利用

    获取cookie利用代码cookie.asp <html> <title>xx</title> <body> <%testfile = Serve ...

  2. sql在外键存在的情况下删除表

    SQL Server 批量 停用/启用 外键约束 今天百度知道上面,看到这样一个要求: 现在有一个库,有很多张表想要删除一张表的记录的时候,由于外键关联太多,所以,没法删除相应的记录,谁能帮忙写个存储 ...

  3. H.265:高清视频的最佳选择

    H.265技术经过2年发展,我们认为H.265高清监控产品市场化才真正开始.那么H.265的到来,大家又对该技术真正了解多少呢?下面就让我们一起来了解该技术.... HEVC/H.265标准于2013 ...

  4. 给iOS开发新手送点福利,简述UITextField的属性和用法

    UITextField属性 0.     enablesReturnKeyAutomatically 默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的. ...

  5. C# 子窗体关闭父窗体的简单方法

    当在一个窗体中调用另一个窗体时,涉及到子窗体关闭的同时,父窗体同时关闭. 例如: 在窗体1中,单击按钮调用窗体2,通过this传递 private void button1_Click(object ...

  6. 由python的math.log想到的问题

    result = math.log(243,3) print(result) 输出5.0 print("%f"%result) 还是输出5.0 看出问题了吗?对,没错.int(5. ...

  7. jeesite快速开发平台(二)----环境搭建

    转自:https://blog.csdn.net/u011781521/article/details/54880465

  8. mac下的一个类似“_kbhit()”实现

    #include <sys/select.h> #include <termios.h> #include <sys/ioctl.h> int _kbhit() { ...

  9. VS2008项目移植到Linux

    不少人都遇到过这种情况:在Windows下用Visual Studio工具开发的程序需要移植到Linux系统中,做成Linux版本的,但程序比较大,在Linux上又离不开Make,手动编写Makefi ...

  10. JavaWeb--过滤器Filter (一)

    过滤器是在服务器上运行的,并且位于请求和响应中间起过滤功能的程序.其工作原理如下图所示:   在与过滤器相关联俄Servlet或JSP运行前,过滤器先执行.一个过滤器可以一个或多个Servlet或JS ...