题目链接:Find a way

bfs水题。

  1 /*
  2   只有两个人啊。分别以两个人为起点bfs,计算出每个人到每个KFC 的时间。两个人都能到达的KFC的较大时间的最小值、就是ans。好水。T_T
  3  */
  4 
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <iostream>
  8 #include <string>
  9 #include <queue>
 10 #define maxn 1000000
 11 using namespace std;
 12 
 13 char mp[210][210];
 14 int vis[210][210];
 15 int step[210][210], step1[210][210], step2[210][210];
 16 int n, m;
 17 
 18 struct Node{
 19     int x, y;
 20 }now, temp, a, b;
 21 
 22 queue<Node>que;
 23 
 24 int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
 25 
 26 bool check(Node a) {
 27     if (a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && !vis[temp.x][temp.y] && mp[temp.x][temp.y] != '#')
 28         return true;
 29     return false;
 30 }
 31 
 32 void bfs1(Node t) {
 33     while(!que.empty()) {
 34         que.pop();
 35     }
 36     memset(vis, 0, sizeof(vis));
 37     que.push(t);
 38     vis[t.x][t.y] = 1;
 39     step1[t.x][t.y] = 0;
 40 
 41     while(!que.empty()) {
 42         now = que.front();
 43         que.pop();
 44         for (int i=0; i<4; ++i) {
 45             temp.x = now.x + dir[i][0];
 46             temp.y = now.y + dir[i][1];
 47             if (check(temp)) {
 48                 vis[temp.x][temp.y] = 1;
 49                 step1[temp.x][temp.y] = step1[now.x][now.y] + 1;
 50                 que.push(temp);
 51             }
 52         }
 53     }
 54 }
 55 
 56 void bfs2(Node t) {
 57      while(!que.empty()) {
 58         que.pop();
 59     }
 60     memset(vis, 0, sizeof(vis));
 61     que.push(t);
 62     vis[t.x][t.y] = 1;
 63     step2[t.x][t.y] = 0;
 64 
 65     while(!que.empty()) {
 66         now = que.front();
 67         que.pop();
 68         for (int i=0; i<4; ++i) {
 69             temp.x = now.x + dir[i][0];
 70             temp.y = now.y + dir[i][1];
 71             if (check(temp)) {
 72                 vis[temp.x][temp.y] = 1;
 73                 step2[temp.x][temp.y] = step2[now.x][now.y] + 1;
 74                 que.push(temp);
 75             }
 76         }
 77     }
 78 }
 79 
 80 int main() {
 81     while(cin >> n >> m) {
 82         memset(vis, 0, sizeof(vis));
 83         for (int i=0; i<n; ++i) {
 84             for (int j=0; j<m; ++j) {
 85                 step[i][j] = maxn;
 86                 step1[i][j] = maxn;
 87                 step2[i][j] = maxn;
 88             }
 89         }
 90         for (int i=0; i<n; ++i) {
 91             for (int j=0; j<m; ++j) {
 92                 cin >> mp[i][j];
 93                 if (mp[i][j] == 'M') {
 94                     a.x = i, a.y = j;
 95                 }
 96                 else if (mp[i][j] == 'Y') {
 97                     b.x = i, b.y = j;
 98                 }
 99             }
100         }
101 
102         bfs1(a);
103         bfs2(b);
104         int ans = maxn;
105         for (int i=0; i<n; ++i) {
106             for (int j=0; j<m; ++j) {
107                 if (mp[i][j] == '@') {
108                     step[i][j] = step1[i][j] + step2[i][j];
109                 }
110                 ans = min(ans, step[i][j]);
111             }
112         }
113         cout << ans*11 << endl;
114     }
115     return 0;
116 }
View Code

 

posted on 2016-01-29 21:01  小小八  阅读(211)  评论(0编辑  收藏  举报