zoukankan      html  css  js  c++  java
  • Careercup

    2014-05-06 00:17

    题目链接

    原题:

    Given a 2-D matrix represents the room, obstacle and guard like the following (0 is room, B->obstacle, G-> Guard): 
    0 0 0 
    B G G 
    B 0 0 
    
    calculate the steps from a room to nearest Guard and set the matrix, like this 
    2 1 1 
    B G G 
    B 1 1 
    Write the algorithm, with optimal solution.

    题目:有一个二维矩阵表示的迷宫,其中G表示保安人员,B表示不可穿越的墙,其他位置均为空地。如果每次可以向东南西北四个方向移动一格,请计算出每个空格离最近的保安有多远。题目给出了一个示例。

    解法:既然矩阵里可能有多个保安,我的思路是以各个保安为中心,进行广度优先搜索,搜索的过程中随时更新每个空格位置的最短距离,保证全部搜索完之后所有的结果都是最小的。单次BFS的时间代价是O(n^2)级别的。

    代码:

     1 // http://www.careercup.com/question?id=4716965625069568
     2 #include <queue>
     3 #include <vector>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8     void solve(vector<vector<int> > &matrix) {
     9         // -1 for guard
    10         // -2 for blockade
    11         // 0 for room
    12         // > 0 for distance
    13         
    14         n = (int)matrix.size();
    15         if (n == 0) {
    16             return;
    17         }
    18         m = (int)matrix[0].size();
    19         if (m == 0) {
    20             return;
    21         }
    22         
    23         int i, j;
    24         
    25         for (i = 0; i < n; ++i) {
    26             for (j = 0; j < m; ++j) {
    27                 if (matrix[i][j] == -1) {
    28                     doBFS(matrix, i, j);
    29                 }
    30             }
    31         }
    32     };
    33 private:
    34     int n, m;
    35     
    36     bool inBound(int x, int y) {
    37         return x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1;
    38     }
    39     
    40     void doBFS(vector<vector<int> > &matrix, int x, int y) {
    41         queue<int> q;
    42         static int dd[4][2] = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
    43         int tmp;
    44         int xx, yy;
    45         int i;
    46         int dist;
    47         
    48         q.push(x * m + y);
    49         while (!q.empty()) {
    50             tmp = q.front();
    51             q.pop();
    52             x = tmp / m;
    53             y = tmp % m;
    54             dist = matrix[x][y] > 0 ? matrix[x][y] : 0;
    55             for (i = 0; i < 4; ++i) {
    56                 xx = x + dd[i][0];
    57                 yy = y + dd[i][1];
    58                 if (!inBound(xx, yy) || matrix[xx][yy] < 0 || 
    59                     (matrix[xx][yy] > 0 && matrix[xx][yy] <= dist + 1)) {
    60                     // out of boundary
    61                     // a guard or a blockade
    62                     // the distance is no shorter
    63                     continue;
    64                 }
    65                 matrix[xx][yy] = dist + 1;
    66                 q.push(xx * m + yy);
    67             }
    68         }
    69     }
    70 };
  • 相关阅读:
    LeetCode 50: Pow(x, n)
    Codility 1: equilibrium
    LeetCode 49: Anagrams
    crackme160--(5)
    crackme160--(4)
    魔兽显血改键工具的脱壳和修改
    crackme160--(3)
    crackme160--(1)
    python-装饰器
    编程语言C++01-C++入门学习路线
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3710623.html
Copyright © 2011-2022 走看看