forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
176 lines (134 loc) · 4.57 KB
/
solution.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Approach~
n=0: Initial grid
n=1: After one second, Bomberman does nothing. Thus return the same initial grid.
n=2: Bomerman fills the whole grid with bombs.
n=3: Initial grid bombs(n=0) detonate & detonate time for bomb set at n=2 decreases by 1.
n=4: Again fill the whole grid.
n=5: Bomb set at n=2 detonates & detonate time for bomb set at n=4 decreases by 1.
n=6: Again fill the whole grid.
n=7: Bomb set at n=4 detonates & detonate time for bomb set at n=6 decreases by 1.
.
.
.
So we see at n=2,4,6,8... i.e n%2==0, The whole grid is covered with bombs(grid_0 in code)
We also see the grid we get at n=3 repeats after every 4 second. Thus at {n|n%4==3} i.e n=3,7,11... the grids obtained are same(grid_3 in code)
Similarly grids at {n|n%4==1} i.e n=4,9,13 are all same(grid_5 in code)
*/
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
vector<string> bomberMan(int n, vector<string> grid) {
int r=grid.size(); //No.of rows in the grid
int c;
for(c=0;grid[0][c]!='\0';c++); //No.of columns in the grid i.e length of a string stored in 'c'
vector<string> grid_0; //grid at n=2,4,6...
vector<string> grid_3; //grid at n=3
vector<string> grid_5; //grid at n=5
string temp=""; //variable to store 'c' length string containing all Os
for(int i=0;i<c;i++){
temp+='O';
}
//Fill grid_0 with all Os
for(int i=0;i<r;i++){
grid_0.push_back("");
grid_3.push_back("");
grid_5.push_back("");
grid_0[i]=temp;
grid_3[i]=temp;
grid_5[i]=temp;
}
//after one second, Bomberman does nothing(No change)
if(n==1)
return grid;
//at t=2,4,6... Bomerman fills the whole grid with bombs
else if(n%2==0){
return grid_0;
}
else{
//calculate grid_3
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(grid[i][j]=='O'){
grid_3[i][j]='.';
if(i-1 >=0)
grid_3[i-1][j]='.';
if(i+1<r)
grid_3[i+1][j]='.';
if(j-1 >=0)
grid_3[i][j-1]='.';
if(j+1<c)
grid_3[i][j+1]='.';
}
}
}
if(n%4==3){ //3,7,11...
return grid_3;
}
else{ //n%4==1, i.e 5,9,13...
//calculate grid_5
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(grid_3[i][j]=='O'){
grid_5[i][j]='.';
if(i-1 >=0)
grid_5[i-1][j]='.';
if(i+1<r)
grid_5[i+1][j]='.';
if(j-1 >=0)
grid_5[i][j-1]='.';
if(j+1<c)
grid_5[i][j+1]='.';
}
}
}
return grid_5;
}
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string rcn_temp;
getline(cin, rcn_temp);
vector<string> rcn = split_string(rcn_temp);
int r = stoi(rcn[0]);
int c = stoi(rcn[1]);
int n = stoi(rcn[2]);
vector<string> grid(r);
for (int i = 0; i < r; i++) {
string grid_item;
getline(cin, grid_item);
grid[i] = grid_item;
}
vector<string> result = bomberMan(n, grid);
for (int i = 0; i < result.size(); i++) {
fout << result[i];
if (i != result.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}