1. 程式人生 > 實用技巧 >Find a way HDU - 2612 (BFS)

Find a way HDU - 2612 (BFS)

Find a way

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

題意:
聖誕節要到了,坤神和瑞瑞這對基佬想一起去召喚師大峽谷開開車。百度地圖一下,發現周圍的召喚師大峽谷還不少,這對基佬糾結著,該去哪一個。。。坤神:我要去左邊的這個(因為離自己比較近 哈哈~)。。瑞瑞:我要去右邊的這個(因為離自己比較近 嘿嘿~) ........這對基佬鬧矛盾了,開車有危險了!  為了不讓他們去召喚師大峽谷坑人,riot決定讓他們去X召喚師大峽谷,保證他倆所走的路程和最短。每走一個點需要花費11分鐘,輸出他們一共花費多少時間(最短時間噢)
Input
多組測試資料

每組資料,開始一行n,m (2<=n,m<=200)

接下來是個n x m的矩陣

'Y' 表示坤神所在的初始位置

'M' 表示瑞瑞所在的初始位置

'#' 該點禁止通行

'.' 該點可通行

'@' 召喚師大峽谷

Output
每組測試資料,輸出坤神和瑞瑞到達同一個召喚師大峽谷所花費的最短時間。

思路:

bfs分別求出兩個人到各個點的最短時間存在dis陣列中,遍歷map尋找和最小的那個點。

程式碼 :

  1 #include <set>
  2 //#include <map>
  3 #include <list>
  4 #include <stack>
  5 #include <queue>
  6 #include <deque>
  7 #include <cmath>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <sstream>
 14 #include <iostream>
 15 #include <algorithm>
 16 //#include <unordered_map>
 17 #define INF 0x3f3f3f3f
 18 #define ll long long
 19 #define ull unsigned long long
 20 #define FILL(a,n,v) fill(a,a+n,v)
 21 #define Mset(a,v) memset(a,v,sizeof a)
 22 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 23 using namespace std;
 24 const int maxn=300;
 25 
 26 int dis1[maxn][maxn],dis2[maxn][maxn];
 27 int n,m;
 28 string map[maxn];
 29 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
 30 struct node
 31 {
 32     int x,y;
 33     int step;
 34     node(int x,int y)
 35     {
 36         this->x=x;
 37         this->y=y;
 38         this->step=0;
 39     }
 40 };
 41 
 42 bool vis[maxn][maxn];
 43 bool check(int x,int y)
 44 {
 45     if(!((x>=0)&&(x<n)&&(y>=0)&&(y<m))) return false;
 46     if(map[x][y]=='#') return false;
 47     if(!vis[x][y]) return false;
 48     return true;
 49 }
 50 
 51 void bfs1(int x,int y)
 52 {
 53     memset(vis,true,sizeof vis);
 54     vis[x][y]=false;
 55     node now(x,y);
 56     queue<node>q;
 57     q.push(now);
 58     while(!q.empty())
 59     {
 60         now=q.front();
 61         q.pop();
 62         if(map[now.x][now.y]=='@')
 63         {
 64             dis1[now.x][now.y]=now.step;
 65         }
 66         for(int i=0;i<4;i++)
 67         {
 68             int nx=now.x+dir[i][0];
 69             int ny=now.y+dir[i][1];
 70             if(check(nx,ny))
 71             {
 72                 vis[nx][ny]=false;
 73                 node next(nx,ny);
 74                 next.step=now.step+11;
 75                 q.push(next);
 76             }
 77         }
 78     }
 79 }
 80 
 81 void bfs2(int x,int y)
 82 {
 83     memset(vis,true,sizeof vis);
 84     vis[x][y]=false;
 85     node now(x,y);
 86     queue<node>q;
 87     q.push(now);
 88     while(!q.empty())
 89     {
 90         now=q.front();
 91         q.pop();
 92         if(map[now.x][now.y]=='@')
 93         {
 94             dis2[now.x][now.y]=now.step;
 95         }
 96         for(int i=0;i<4;i++)
 97         {
 98             int nx=now.x+dir[i][0];
 99             int ny=now.y+dir[i][1];
100             if(check(nx,ny))
101             {
102                 vis[nx][ny]=false;
103                 node next(nx,ny);
104                 next.step=now.step+11;
105                 q.push(next);
106             }
107         }
108     }
109 }
110 
111 int main()
112 {
113     while(cin>>n>>m)
114     {
115         int x1=0,y1=0,x2=0,y2=0;
116         for(int i=0;i<n;i++) cin>>map[i];
117         for(int i=0;i<n;i++)
118         {
119             for(int j=0;j<m;j++)
120             {
121                 if(map[i][j]=='Y')
122                 {
123                     x1=i;
124                     y1=j;
125                 }
126                 if(map[i][j]=='M')
127                 {
128                     x2=i;
129                     y2=j;
130                 }
131             }
132         }
133         Mset(dis1,INF);
134         Mset(dis2,INF);
135         bfs1(x1,y1);
136         bfs2(x2,y2);
137         int res=INF;
138         for(int i=0;i<n;i++)
139         {
140             for(int j=0;j<m;j++)
141             {
142                 res=min(res,dis1[i][j]+dis2[i][j]);
143             }
144         }
145         cout<<res<<endl;
146     }
147 }