-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1349.cpp
49 lines (46 loc) · 1.4 KB
/
1349.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
class Solution
{
public:
int maxStudents(vector<vector<char>>& seats)
{
r = seats.size(), c = seats[0].size();
match = vector<vector<int>>(r * c);
int res = 0, cnt = 0;
int vis[r * c] = {};
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j += 2)
{
if (seats[i][j] != '.') continue;
memset(vis, 0, sizeof vis);
vector<int> node = {i, j};
if (find(node, vis, seats)) res++;
}
}
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
if (seats[i][j] == '.') cnt++;
return cnt - res;
}
private:
int d[6][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 1}, {0, 1}, {1, 1}};
int r, c;
vector<vector<int>> match;
bool find(vector<int>& node, int* vis, vector<vector<char>>& seats)
{
for (auto& it : d)
{
int nx = it[0] + node[0], ny = it[1] + node[1];
if (nx >= 0 && nx < r && ny >= 0&& ny < c && !vis[nx * c + ny] && seats[nx][ny] == '.')
{
vis[nx * c + ny] = 1;
if (match[nx * c + ny].empty() || find(match[nx * c + ny], vis, seats))
{
match[nx * c + ny] = node;
return true;
}
}
}
return false;
}
};