HDU2612(KB1-N)
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12795 Accepted Submission(s): 4105
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.
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
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
1 //2017-02-28 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 7 using namespace std; 8 9 struct node{ 10 int x, y, step; 11 void setNode(int x, int y, int step){ 12 this->x = x; 13 this->y = y; 14 this->step = step; 15 } 16 }; 17 char grid[205][205]; 18 bool book[205][205]; 19 int Ydis[205][205], Mdis[205][205]; 20 int n, m; 21 int dx[4] = {0, 1, 0, -1}; 22 int dy[4] = {1, 0, -1, 0}; 23 const int inf = 0x3f3f3f3f; 24 25 void bfs(int sx, int sy) 26 { 27 node tmp; 28 tmp.setNode(sx, sy, 0); 29 queue<node> q; 30 q.push(tmp); 31 memset(book, 0, sizeof(book)); 32 book[sx][sy] = 1; 33 int x, y, step; 34 while(!q.empty()) 35 { 36 x = q.front().x; 37 y = q.front().y; 38 step = q.front().step; 39 q.pop(); 40 if(grid[x][y] == '@'){ 41 if(grid[sx][sy] == 'Y')Ydis[x][y] = step; 42 else Mdis[x][y] = step; 43 } 44 for(int i = 0; i < 4; i++) 45 { 46 int nx = x + dx[i]; 47 int ny = y + dy[i]; 48 if(nx>=0&&nx<n&&ny>=0&&ny<m&&!book[nx][ny]&&grid[nx][ny]!='#'){ 49 book[nx][ny] = 1; 50 tmp.setNode(nx, ny, step+1); 51 q.push(tmp); 52 } 53 } 54 } 55 } 56 57 int main() 58 { 59 while(cin>>n>>m) 60 { 61 for(int i = 0; i < n; i++) 62 for(int j = 0; j < m; j++) 63 cin>>grid[i][j]; 64 memset(Ydis, inf, sizeof(Ydis)); 65 memset(Mdis, inf, sizeof(Mdis)); 66 for(int i = 0; i < n; i++) 67 for(int j = 0; j < m; j++) 68 if(grid[i][j] == 'Y' || grid[i][j] == 'M') 69 bfs(i, j); 70 int ans = inf; 71 for(int i = 0; i < n; i++) 72 for(int j = 0; j < m; j++) 73 if(grid[i][j] == '@' && ans > Ydis[i][j]+Mdis[i][j]) 74 ans = Ydis[i][j] + Mdis[i][j]; 75 cout<<11*ans<<endl; 76 } 77 78 return 0; 79 }