码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 2612 Find a way(BFS)

时间:2018-04-13 18:04:44      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:find   try   final   ota   赋值   contain   int   思路   people   

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 }

 

HDU - 2612 Find a way(BFS)

标签:find   try   final   ota   赋值   contain   int   思路   people   

原文地址:https://www.cnblogs.com/TWS-YIFEI/p/8821428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!