链接:

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

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. #define INF 0x3f3f3f3f
  11. #define N 210
  12.  
  13. struct node
  14. {
  15. int x, y, step;
  16. };
  17.  
  18. int n, m, a[N][N], dir[][]={{-,},{,},{,-},{,}};
  19. char G[N][N];
  20. int vis[N][N];
  21.  
  22. void BFS(node s, int num)
  23. {
  24. node p, q;
  25.  
  26. queue<node>Q;
  27. Q.push(s);
  28. memset(vis, , sizeof(vis));
  29. vis[s.x][s.y] = ;
  30.  
  31. while(Q.size())
  32. {
  33. p = Q.front(), Q.pop();
  34.  
  35. if(G[p.x][p.y]=='@')
  36. {
  37. if(num==)
  38. a[p.x][p.y] = p.step;
  39. if(num==)
  40. a[p.x][p.y] += p.step;
  41. }
  42.  
  43. for(int i=; i<; i++)
  44. {
  45. q.x = p.x + dir[i][];
  46. q.y = p.y + dir[i][];
  47. q.step = p.step + ;
  48.  
  49. if(!vis[q.x][q.y] && q.x>= && q.x<n && q.y>= && q.y<m && G[q.x][q.y]!='#')
  50. {
  51.  
  52. vis[q.x][q.y] = ;
  53. Q.push(q);
  54. }
  55. }
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. while(scanf("%d%d", &n, &m)!=EOF)
  62. {
  63. int i, j;
  64. node Y, M;
  65.  
  66. memset(a, , sizeof(a));
  67.  
  68. for(i=; i<n; i++)
  69. {
  70. scanf("%s", G[i]);
  71. for(j=; j<m; j++)
  72. {
  73. if(G[i][j]=='Y')
  74. Y.x=i, Y.y=j, Y.step=;
  75. if(G[i][j]=='M')
  76. M.x=i, M.y=j, M.step=;
  77. }
  78. }
  79.  
  80. BFS(Y, );
  81. BFS(M, );
  82.  
  83. int ans=INF;
  84.  
  85. for(i=; i<n; i++)
  86. for(j=; j<m; j++)
  87. if(G[i][j]=='@' && vis[i][j])
  88. ans = min(ans, a[i][j]);
  89.  
  90. printf("%d\n", ans*);
  91.  
  92. }
  93. return ;
  94. }

(广搜) 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. open_input_file函数调用结构图(转)

    open_input_file函数调用结构图(有些重复的函数调用就略掉了,大致是按流程往下的). 函数大致说明: AVFormatContext *avformat_alloc_context(voi ...

  2. MyEclipse 代码里的中文字太小设置方法

    General>Appearance>Colors and Fonts>Basic>Text Font >Edit 把脚本字符改成“中欧字符”就可以了

  3. SQL Server中动态列转行

    http://www.cnblogs.com/gaizai/p/3753296.html 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现 ...

  4. Oracle归档日志与非归档日志的切换及路径设置

    --==================== -- Oracle 归档日志 --==================== Oracle可以将联机日志文件保存到多个不同的位置,将联机日志转换为归档日志的 ...

  5. MySQL 数据库查询练习

    -- ---------------------------- -- mysql练习sql脚本 -- ---------------------------- create database db10 ...

  6. php 文件缓存类

    //文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...

  7. 20165233 学习基础和C语言基础调查

    学习基础与<做中学>阅读心得 读<做中学>有感 娄老师通过在学习工作中总结出的"做中学"的方式,将其运用到减肥.五笔训练.乒乓球训练以及英文单词背诵的过程中 ...

  8. 现象级AR营销助力“口碑双十二”,蚂蚁特工在全国数万商户掀起“AR捉四宝”

    领取阅读奖励金 今年双十二,全国人民吃喝玩乐放飞自我,嗨出了新纪元.除了见证你国人民的财力,这个“双十二”还诞生了教科书级的“AR营销”.无论是在口碑商户门口,还是在各大购物广场,都能看到举着手机,正 ...

  9. <form> 表单提交 return 阻止内容为空事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 关于document.cookie的使用

    设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次 ...