Problem Descriptionpass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S. inputthe input contains multiple test cases.
Each test case include, first, 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 this both Yifenfei and Merceki to arrival one of the KFC. Sure there is always has a KFC that can let them meet. Sample Input4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...# Sample Output668866 Idea: A question of pleasure than all do not explain the topic is good, let me re-understand the effectiveness of BFS. In fact, the idea is very easy, is to traverse the next two people to each ' @ ' distance, save it and then see which of the shortest line, the key problem is
data structure, that is, the storage and operation of the method. At first, my idea was to map the distance from each point to the walking person to the coordinates of the point, but I stuck in the distance, but I always wanted to use DFS to ask for, do not have enough problems = = Later on the Internet to see the problem, It is found that using BFS to traverse the distance from all points in the graph to the starting point in Y or M, the key formula is
Dis[dx][dy] = dis[temp.x][temp.y]+1;
The first time I saw this formula, the reaction was DP (I haven't done DP in a long time).
It turns out this is the essence of BFS.
#include <iostream> #include <cstring> #include <queue> #define INF 0x7fffffffusing namespace Std;int n , M;char g[207][207];int dir[4][2] = {{ -1,0},{0,-1},{0,1},{1,0}};struct pos{int x, y;}; int Dis_1[207][207];int dis_2[207][207];bool judge (int x,int y) {if (x>=1&& x<=n && y>=1 && Amp Y<=m && g[x][y]!= ' # ') return True;else return false;} void BFs (int a,int b,int vis[207][207],int dis[207][207]) {vis[a][b] = 1;queue<pos> Q;pos s;s.x = A;s.y = B;q.push (s) ; while (!q.empty ()) {pos temp = Q.front (), Q.pop (); for (int i = 0;i <= 3;i++) {int dx = Temp.x+dir[i][0];int dy = Temp.y+dir I [1];if (!vis[dx][dy] && judge (Dx,dy)) {Vis[dx][dy] = 1;dis[dx][dy] = Dis[temp.x][temp.y]+1;pos n;n.x = dx;n.y = dy; Q.push (n);}}} int main () {while (cin>>n>>m) {int x1,y1,x2,y2;int vis_1[207][207];int vis_2[207][207];for (int i = 1;i <= n ; i++) cin>>g[i]+1;for (int i = 1;i <= n;i++) for (int j = 1;j <= m;j++) {if (g[i][j] = = ' Y ') {x1 = i;y1 = J;} if (g[i][j] = = ' M ') {x2 = I;y2 = j;}} memset (dis_1,0,sizeof (dis_1)); Memset (Dis_2,0,sizeof (dis_2)); Memset (Vis_1,0,sizeof (vis_1)); Memset (vis_2,0, sizeof (vis_2)), BFS (X1,y1,vis_1,dis_1), BFS (x2,y2,vis_2,dis_2), int ans = inf;for (int i = 1;i <= n;i++) for (int j = 1;j < = m;j++) if (g[i][j] = = ' @ ' && vis_1[i][j] && vis_2[i][j]) ans = dis_1[i][j]+dis_2[i][j]<ans?dis_1[i][ J]+dis_2[i][j]:ans;cout<<ans*11<<endl;} return 0;}
HDU-2612 Find A-by