-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathB.cpp
More file actions
executable file
·55 lines (44 loc) · 1.1 KB
/
Copy pathB.cpp
File metadata and controls
executable file
·55 lines (44 loc) · 1.1 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int l_shaped_plots(int R, int C, vector<vector<int>>& grid) {
int cnt = 0;
for (int flip_row: {0, 1}) {
for (int flip_col: {0, 1}) {
vector<int> row(R), col(C);
// count the number of L shapes
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
if (grid[i][j])
row[i]++, col[j]++;
else
row[i] = col[j] = 0;
cnt += max(0, min(row[i], col[j] / 2) - 1);
cnt += max(0, min(row[i] / 2, col[j]) - 1);
}
// flip horizontally
for (int i = 0; i < R; i++)
reverse(grid[i].begin(), grid[i].end());
}
// flip vertically
for (int i = 0; i < R / 2; i++)
for (int j = 0; j < C; j++)
swap(grid[i][j], grid[R - i - 1][j]);
}
return cnt;
}
int main() {
int T;
cin >> T;
for (int x = 1; x <= T; x++) {
int R, C;
cin >> R >> C;
vector<vector<int>> grid(R, vector<int>(C));
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
cin >> grid[i][j];
cout << "Case #" << x << ": " << l_shaped_plots(R, C, grid) << endl;
}
return 0;
}