HDU - 2612 Find a way(BFS)

原创 2018年04月16日 16:50:54


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. 

InputThe 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 
OutputFor 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

思路:题意就是两个人选择一个接头地点,两个人去见面,然后要求两个人到达那个地点的路程之和最小。

可以分别计算两个人到达每个kfc的距离,用广度搜索计算,然后从两人都能到达的kfc中选取路程最小的那个。

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 typedef struct{
 9     int i,j;
10     int step;
11 }loca;
12 
13 int n,m;
14 char mm[205][205];//地图
15 int flag[205][205];//此位置是否待过
16 int step[205][205];//到达此处的步数,只有@时才赋值
17 int isarrival[205][205];//如果此处两个人都到达才可以
18 int yifeni=0,yifenj=0;//yifen位置
19 int meri=0,merj=0;//mer位置
20 queue<loca> q;
21 loca now,nextt;
22 int add[2][4]={-1,1,0,0,
23                 0,0,-1,1};
24 
25 void go(int si,int sj){
26     now.i=si;
27     now.j=sj;
28     now.step=0;
29     flag[si][sj]=1;
30     q.push(now);
31     while(!q.empty()){
32         now=q.front(); q.pop();
33         if(mm[now.i][now.j]=='@'){
34             step[now.i][now.j]+=now.step;
35             isarrival[now.i][now.j]++;
36         }
37         flag[now.i][now.j]=1;
38         for(int i=0;i<4;i++){
39             nextt.i=now.i+add[0][i];
40             nextt.j=now.j+add[1][i];
41             nextt.step=now.step+1;
42             if(nextt.i<0||nextt.i>=n||nextt.j<0||nextt.j>m){
43                 continue;
44             }
45             if(flag[nextt.i][nextt.j]==0 && mm[nextt.i][nextt.j]!='#'){
46                 q.push(nextt);
47                 flag[nextt.i][nextt.j]=1;
48             }
49         }
50     }
51 }
52 
53 int main()
54 {
55     while(~scanf("%d %d",&n,&m)){
56         memset(step,0,sizeof(step));
57         memset(isarrival,0,sizeof(isarrival));
58         memset(flag,0,sizeof(flag));
59         for(int i=0;i<n;i++){
60             getchar();
61             for(int j=0;j<m;j++){
62                 scanf("%c",&mm[i][j]);
63                 if(mm[i][j]=='Y'){
64                     yifeni=i; yifenj=j;
65                 }
66                 if(mm[i][j]=='M'){
67                     meri=i; merj=j;
68                 }
69             }
70         }
71         go(yifeni,yifenj);
72         memset(flag,0,sizeof(flag));
73         go(meri,merj);
74 
75         int minn=0x3f3f3f3f;
76         for(int i=0;i<n;i++){
77             for(int j=0;j<m;j++){
78                 if(step[i][j]!=0&&isarrival[i][j]==2){
79                     int temp=step[i][j];
80                     if(temp<minn){
81                         minn=temp;
82                     }
83                 }
84             }
85         }
86         printf("%d\n",minn*11);
87     }
88     return 0;
89 }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27179557/article/details/79962890

CSA课程:A段架构师的职责

传授CSA等级的架构设计思想、方法与技术。让想深度领会架构设计幕后的思想者,和盼望早日成为CSA者,都能孰悉A段架构师的角色。
  • 2015年09月22日 14:37

HDU 2612 find a way

跑两遍BFS,开数组记录到达KFC步数。
  • sky_miange
  • sky_miange
  • 2015-02-06 11:05:16
  • 1027

HDU-2612-Find a way

//这题坑了我一晚上,用C++交不行,结果用C或者G++却过来,哎。。。。这题让我明白,如果用BFS,要保存到达“@”点的步数,bfs一直搜索,直到没发走才停。让后在主函数中进行输出比较。。。 AC代...
  • zyx520ytt
  • zyx520ytt
  • 2015-07-23 20:54:14
  • 305

HDU:2612 Find a way(双BFS+打表)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • zugofn
  • zugofn
  • 2016-07-30 00:48:15
  • 614

杭电 HDU ACM 2612 Find a way (简单两路广搜)

欢迎参加——每周六晚的BestCoder(有米!) Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/...
  • lsgqjh
  • lsgqjh
  • 2015-07-31 09:47:27
  • 1724

hdoj 2612 Find a way简单广搜

刚开始看到这道题居然想的是用肯德基店作为广搜的首节点, 然后TLE 后来想想要是好多家肯德基那么每一家就广搜一次,不超时就怪了 然后才发现用Y和M两遍广搜标记好距离就可以了,然后看每一家肯德基店二...
  • waterkokoro
  • waterkokoro
  • 2015-07-23 15:28:21
  • 335

杭电2612 Find a way(BFS)

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
  • u013634213
  • u013634213
  • 2014-12-29 18:22:43
  • 434

BFS基础题 杭电2612 Find a way 1252 Hike on a Graph

BFS与DFS简单对比DFS适合将所有的全都遍历一遍,就像之前的博客中的两道DFS基础题,比如最多可以走几步,所有的连通区域个数,这些都需要全部遍历完才能知道。而BFS,比如求最短的距离,可以直接宽度...
  • IBelieve2016
  • IBelieve2016
  • 2017-07-04 09:53:09
  • 256

HDU 2612 Find a way【第一次双BFS】

Find a way Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...
  • qq_24653023
  • qq_24653023
  • 2016-07-18 20:53:48
  • 8171

[ACM] hdu Find a way

Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
  • sr19930829
  • sr19930829
  • 2014-01-18 14:57:10
  • 2709
收藏助手
不良信息举报
您举报文章:HDU - 2612 Find a way(BFS)
举报原因:
原因补充:

(最多只允许输入30个字)