-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-matrix.js
41 lines (36 loc) · 935 Bytes
/
01-matrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Source : https://leetcode.com/problems/01-matrix/#/description
// Author : Han Zichi
// Date : 2016-03-27
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var updateMatrix = function(matrix) {
// BFS
let q = [];
let hash = [];
let [m, n] = [matrix.length, matrix[0].length];
const dir = [[1, 0], [-1, 0], [0, 1], [0, -1]];
for (let i = 0; i < m; i++) {
hash[i] = [];
for (let j = 0; j < n; j++) {
if (matrix[i][j] === 0) {
q.push({x: i, y: j, step: 0});
hash[i][j] = 0;
}
}
}
while (q.length) {
let item = q.shift();
let {x, y, step} = item;
for (let i = 0; i < 4; i++) {
let _x = x + dir[i][0];
let _y = y + dir[i][1];
if (_x < 0 || _x >= m || _y < 0 || _y >= n) continue;
if (hash[_x][_y] !== undefined) continue;
hash[_x][_y] = step + 1;
q.push({x: _x, y: _y, step: step + 1});
}
}
return hash;
};