-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0542-01-Matrix.cs
More file actions
70 lines (54 loc) · 1.67 KB
/
0542-01-Matrix.cs
File metadata and controls
70 lines (54 loc) · 1.67 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0542._01_Matrix
{
public class _0542_01_Matrix
{
private int[][] res = null;
private int row;
private int col;
public int[][] UpdateMatrix(int[][] mat)
{
if (mat.Length == 0) return mat;
res = mat;
row = mat.Length;
col = mat[0].Length;
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (res[i][j] == 1 && !hasNeighborZero(i, j))
res[i][j] = int.MaxValue;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (res[i][j] != 1) continue;
DFS(i, j, 1);
}
}
return res;
}
private void DFS(int x, int y, int val)
{
if (x < 0 || x >= row || y < 0 || y >= col || res[x][y] < val) return;
res[x][y] = val;
val++;
DFS(x - 1, y, val); // top
DFS(x + 1, y, val); // bottom
DFS(x, y - 1, val); // left
DFS(x, y + 1, val); // right
}
private bool hasNeighborZero(int x, int y)
{
// top
if (x > 0 && res[x - 1][y] == 0) return true;
// bottom
if (x < row - 1 && res[x + 1][y] == 0) return true;
// left
if (y > 0 && res[x][y - 1] == 0) return true;
// right
if (y < col - 1 && res[x][y + 1] == 0) return true;
return false;
}
}
}