Find a way

tech2026-09-20  1

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

标准的BFS,和前面的一到题类似,依次进行两次BFS即可 但是本题需要注意的是: 可能有的KFC无法到达!!! 若在最后求时间时不判断,结果就是0!

代码:

#include<iostream> #include <algorithm> #include<cstdio> #include<queue> #include <cstring> using namespace std; char map[205][205]; int vis[206][205]; int tim1[205][205] ,tim2[206][206] ;//分别是两个人所花费的时间 int dx[4] = {1, -1 ,0 ,0}; int dy[4] = {0 , 0, 1,-1}; int yx, yy ,mx , my; int n ,m ,cnt; struct node{ int x, y ,t; }kfc[10006]; void bfs(){ queue<node>que; node s , nxt; s.x = yx, s.y = yy ,s.t = 0; vis[yx][yy] = 1; que.push(s); while( !que.empty() ){ s = que.front() , que.pop(); for(int i = 0 ;i < 4; i++){ int nx = s.x + dx[i] ,ny = s.y + dy[i]; if( nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '#' && !vis[nx][ny]){ vis[nx][ny] = 1; nxt.x = nx, nxt.y = ny , nxt.t = s.t + 1, que.push(nxt); if( map[nx][ny] == '@') tim1[nx][ny] = s.t + 1; } } } } void bfs2(){ queue<node>que; node s , nxt; s.x = mx, s.y = my ,s.t = 0; vis[mx][my] = 1; que.push(s); while( !que.empty() ){ s = que.front() , que.pop(); for(int i = 0 ;i < 4; i++){ int nx = s.x + dx[i] ,ny = s.y + dy[i]; if( nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '#' && !vis[nx][ny]){ vis[nx][ny] = 1; nxt.x = nx, nxt.y = ny , nxt.t = s.t + 1, que.push(nxt); if( map[nx][ny] == '@') tim2[nx][ny] = s.t + 1; } } } } int main(){ while( scanf("%d%d", &n, &m) != EOF){ cnt = 0; for(int i = 0; i < n ;i++){ for(int j = 0; j < m; j++){ scanf(" %c",&map[i][j] ); if(map[i][j] == 'Y'){ yx = i, yy = j; } else if(map[i][j] == 'M'){ mx = i , my = j; } else if(map[i][j] == '@'){ kfc[cnt].x = i, kfc[cnt].y = j ,cnt++; } } } bfs(); memset(vis,0,sizeof(vis)); bfs2(); memset(vis,0,sizeof(vis)); int ans = 99999999; for(int i = 0;i < cnt; i++){ if( tim1[kfc[i].x][kfc[i].y] && tim2[kfc[i].x][kfc[i].y])//判断是否可以到达 ans = min( tim1[kfc[i].x][kfc[i].y] + tim2[kfc[i].x][kfc[i].y] , ans); } printf("%d\n",11*ans); memset(tim1,0,sizeof(tim1) ); memset(tim2,0,sizeof(tim2) ); } return 0; }
最新回复(0)