hdu 2612 Find a way

Find a way

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


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
 

 

Author
yifenfei
 

 

Source
 

 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:  1254 1728 1072 2579 1983 
 
做这道题的时候遇到了bug,看上去明显错误的代码a了,改了之后变成wa,于是去看了讨论组,发现有的人说Y不能走M,M也不能走Y,这样改了之后a了,可是后来发现交别人的代码可以走也能a,结果最后还是不知道自己代码错了哪里,附上能走的代码。
 
题意:Y和M要去KFC一起吃饭,@为KFC的位置,可以有多个,求出他们到达KFC耗费的最短时间(1个格子耗费11分钟)。用两次bfs(),分别求出Y和M到各KFC的最短路,相加之后,遍历出最短的总时间。
 
附上别人的代码:
 
 1 #include <iostream>
 2 #include <queue>
 3 #include <string.h>
 4 using namespace std;
 5 char a[201][201];
 6 int step[201][201];
 7 int m1[201][201];
 8 int m2[201][201];
 9 bool isw[201][201];
10 int dx[4] = {0,1,0,-1};
11 int dy[4] = {1,0,-1,0};
12 int n,m;
13 int ycurx,ycury;
14 int mcurx,mcury;
15 struct NODE{
16     int x;
17     int y;
18 };
19 int Min(int a,int b)
20 {
21     return a<b?a:b;
22 }
23 bool judge(int x,int y)
24 {
25     if( x<1 || y<1 || x>n || y>m )    //越界 
26         return 1;
27     if( isw[x][y] )    //走过了 
28         return 1;
29     if( a[x][y]=='#' )    //碰到墙 
30         return 1;
31     return 0;
32 }
33 void bfs(int curx,int cury)
34 {
35     queue <NODE> q;    //创建队列 
36     NODE cur,next;
37     cur.x = curx;
38     cur.y = cury;
39     q.push(cur);    //入队 
40     memset(isw,0,sizeof(isw));
41     isw[curx][cury] = true;
42     step[curx][cury] = 0;
43     while(!q.empty()){
44         cur = q.front();
45         q.pop();
46         for(int i=0;i<4;i++){
47             int nx = cur.x + dx[i];
48             int ny = cur.y + dy[i];
49             if( judge(nx,ny) )     
50                 continue;
51             step[nx][ny] = step[cur.x][cur.y] + 1;    //记录走到下一步的步数 
52             isw[nx][ny] = true;
53             next.x = nx;
54             next.y = ny;
55             q.push(next);
56         }
57     }
58 }
59 int main()
60 {
61     while(cin>>n>>m){
62         for(int i=1;i<=n;i++)
63             for(int j=1;j<=m;j++){
64                 cin>>a[i][j];
65                 if(a[i][j]=='Y'){    //记录Y的位置 
66                     ycurx=i;
67                     ycury=j;
68                 }
69                 else if(a[i][j]=='M'){    //记录M的位置 
70                     mcurx=i;
71                     mcury=j;
72                 }
73             }
74         int tmin = 999999999;
75         bfs(ycurx,ycury);    //以(ycurx,ycury)为起点开始广度优先搜索 
76         for(int i=1;i<=n;i++)
77             for(int j=1;j<=m;j++){
78                 m1[i][j]=step[i][j];
79             }
80         bfs(mcurx,mcury);    //以(mcurx,mcury)为起点开始广搜优先搜索 
81         for(int i=1;i<=n;i++)
82             for(int j=1;j<=m;j++){
83                 m2[i][j]=step[i][j];
84             }
85         for(int i=1;i<=n;i++)    //遍历出最短的总步数 
86             for(int j=1;j<=m;j++){
87                 step[i][j]=m1[i][j] + m2[i][j];
88                 if(a[i][j]=='@'){
89                     tmin = Min(tmin,step[i][j]);
90                 }
91             }
92         cout<<tmin*11<<endl;    //输出最短总时间 
93     }
94     return 0;
95 }

 

posted @ 2015-08-25 13:19  lucky_少哖  阅读(136)  评论(0编辑  收藏  举报