-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDesertification.cpp
64 lines (62 loc) · 2.2 KB
/
Desertification.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
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<list>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<sstream>
#include<cstring>
using namespace std;
class Desertification {
public:
int desertArea(vector <string> island, int T) {
int desertCells = 0;
for (int i = 0; i < island.size(); ++i) {
for (int j = 0; j < island[i].size(); ++j) {
if (island[i][j] == 'D')
++desertCells;
}
}
if (desertCells > 0) {
int t = 0;
while (t < T && desertCells < island.size() * island[0].size()) {
for (int i = 0; i < island.size(); ++i) {
for (int j = 0; j < island[i].size(); ++j) {
if (island[i][j] == 'F') {
if (i-1 >= 0 && island[i-1][j] == 'D') {
island[i][j] = 'd';
++desertCells;
} else if (i+1 < island.size() && island[i+1][j] == 'D') {
island[i][j] = 'd';
++desertCells;
} else if (j-1 >= 0 && island[i][j-1] == 'D') {
island[i][j] = 'd';
++desertCells;
} else if (j+1 < island[i][j+1] && island[i][j+1] == 'D') {
island[i][j] = 'd';
++desertCells;
}
}
}
}
for (int i = 0; i < island.size(); ++i) {
for (int j = 0; j < island[i].size(); ++j) {
if (island[i][j] == 'd')
island[i][j] = 'D';
}
}
++t;
}
}
return desertCells;
}
};