-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path66.cpp
More file actions
36 lines (31 loc) · 985 Bytes
/
66.cpp
File metadata and controls
36 lines (31 loc) · 985 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
int getSum(int n) {
int sum = 0;
while (n) {
sum += n % 10;
n /= 10;
}
return sum;
}
int moving(int threshold, int i, int j, int rows, int cols, vector<vector<int> > &flag) {
if (i < 0 || i >= rows || j < 0 || j >= cols || flag[i][j] == 1 || getSum(i) + getSum(j) > threshold)
return 0;
flag[i][j] = 1;
return moving(threshold, i - 1, j, rows, cols, flag) + moving(threshold, i + 1, j, rows, cols, flag) +
moving(threshold, i, j - 1, rows, cols, flag) + moving(threshold, i, j + 1, rows, cols, flag) + 1;
}
int movingCount(int threshold, int rows, int cols) {
vector<vector<int> > flag(rows);
for (int i = 0; i < rows; i++)
flag[i].resize(cols, 0);
return moving(threshold, 0, 0, rows, cols, flag);
}
int main() {
ios::sync_with_stdio(false);
int m, n, k;
cin >> m >> n >> k;
cout << movingCount(k, m, n);
return 0;
}