-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path542.01-matrix.cpp
125 lines (123 loc) · 2.42 KB
/
542.01-matrix.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* @lc app=leetcode id=542 lang=cpp
*
* [542] 01 Matrix
*
* https://leetcode.com/problems/01-matrix/description/
*
* algorithms
* Medium (34.68%)
* Total Accepted: 36.9K
* Total Submissions: 106.4K
* Testcase Example: '[[0,0,0],[0,1,0],[0,0,0]]'
*
*
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for
* each cell.
*
* The distance between two adjacent cells is 1.
*
* Example 1:
* Input:
*
* 0 0 0
* 0 1 0
* 0 0 0
*
* Output:
*
* 0 0 0
* 0 1 0
* 0 0 0
*
*
*
* Example 2:
* Input:
*
* 0 0 0
* 0 1 0
* 1 1 1
*
* Output:
*
* 0 0 0
* 0 1 0
* 1 2 1
*
*
*
* Note:
*
* The number of elements of the given matrix will not exceed 10,000.
* There are at least one 0 in the given matrix.
* The cells are adjacent in only four directions: up, down, left and right.
*
*
*
*/
#include <vector>
#include <queue>
#include <set>
using namespace std;
class Solution
{
private:
queue<pair<int, int>> q;
vector<vector<int>> *mx;
int height;
int width;
public:
vector<vector<int>> updateMatrix(vector<vector<int>> &matrix)
{
mx = &(matrix);
height = matrix.size();
width = matrix[0].size();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (matrix[i][j] == 1)
matrix[i][j] = shortDis(i, j);
}
}
return matrix;
}
int shortDis(int r, int c)
{
queue<pair<int, int>> q;
set<pair<int, int>> s;
if ((*mx)[r][c] == 0)
return 0;
q.push(pair(r, c));
int dis = 0;
while (!q.empty())
{
for (int i = 0, e = q.size(); i < e; i++)
{
auto p = q.front();
q.pop();
if ((*mx)[p.first][p.second] == 0)
return dis;
p.first--;
if (p.first >= 0)
q.push(p);
p.first++;
p.first++;
if (p.first < height)
q.push(p);
p.first--;
p.second--;
if (p.second >= 0)
q.push(p);
p.second++;
p.second++;
if (p.second < width)
q.push(p);
p.second--;
}
dis++;
}
return INT_MAX;
}
};