-
Notifications
You must be signed in to change notification settings - Fork 172
/
spaceship bomb.cpp
285 lines (252 loc) · 6.24 KB
/
spaceship bomb.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
https://www.careercup.com/question?id=5652067409461248
https://www.geeksforgeeks.org/samsung-interview-experience-set-28-campus/
http://ideone.com/JXMl4L
https://ide.geeksforgeeks.org/tiyLThcuSN -> Zero
https://ide.geeksforgeeks.org/3Ks1tpOkwn
*https://code.hackerearth.com/ea07cfD?key=1cb190b158c79639d66d35f7dfa8ef7a -> One
Similr Problem - https://ide.codingblocks.com/s/95965
You’ll be given a grid as below:
0 1 0 2 0
0 2 2 2 1
0 2 1 1 1
1 0 1 0 0
0 0 1 2 2
1 1 0 0 1
x x S x x
In the grid above,
1: This cell has a coin.
2: This cell has an enemy.
0: It contains nothing.
The highlighted(yellow) zone is the control zone. S is a spaceship that we need to control so that we can get
maximum coins.
Now, S’s initial position will be at the center and we can only move it right or left by one cell or do not move.
At each time, the non-highlighted part of the grid will move down by one unit.
We can also use a bomb but only once. If we use that, all the enemies in the 5×5 region above the control zone
will be killed.
If we use a bomb at the very beginning, the grid will look like this:
0 1 0 2 0
0 0 0 0 1
0 0 1 1 1
1 0 1 0 0
0 0 1 0 0
1 1 0 0 1
x x S x x
As soon as, the spaceship encounters an enemy or the entire grid has come down, the game ends.
For example,
At the very first instance, if we want to collect a coin we should move left( coins=1). This is because when the
grid comes down by 1 unit we have a coin on the second position and by moving left we can collect that coin.
Next, we should move right to collect another coin (coins=2).
After this, remain at the same position (coins=4).
This is the current situation after collecting 4 coins.
0 1 0 2 0 0 1 0 0 0
0 2 2 2 1 -->after using 0 0 0 0 1
x x S x x -->bomb x x S x x
Now, we can use the bomb to get out of this situation. After this, we can collect at most 1 coin. So maximum coins=5.
*/
#include<bits/stdc++.h>
using namespace std;
void updateMatrix(int row,char ** matrix){
if(row<0){
return;
}
int upLimit=max(0,row-4);
for(int i=row;i>=upLimit;i--){
for(int j=0;j<=4;j++){
if(matrix[i][j]=='2'){
matrix[i][j]='0';
}
}
}
}
int findMaxPoints(int row,int col,int bombs,char ** matrix){
if(row<=0 || col<0 || col>=5){
return 0;
}
int answer=0;
if(row>0 && matrix[row-1][col]!='2'){
answer=max(answer,(matrix[row-1][col]=='1'?1:0)+findMaxPoints(row-1,col,bombs,matrix));
}
if(col>0 && row>0 && matrix[row-1][col-1]!='2'){
answer=max(answer,(matrix[row-1][col-1]=='1'?1:0)+findMaxPoints(row-1,col-1,bombs,matrix));
}
if(col<4 && row>0 && matrix[row-1][col+1]!='2'){
answer=max(answer,(matrix[row-1][col+1]=='1'?1:0)+findMaxPoints(row-1,col+1,bombs,matrix));
}
if(answer==0 && bombs>0){
updateMatrix(row-1,matrix);
answer=findMaxPoints(row,col,bombs-1,matrix);
}
return answer;
}
int main(){
int t, row;
cin >> t;
int tNo = 0;
while(t--){
cin >> row;
char ** matrix=new char*[row + 2];
for(int i=0; i<row; i++){
matrix[i]=new char[5];
for(int j=0;j<5;j++){
cin>>matrix[i][j];
}
}
tNo++;
cout<< "#" << tNo << " : " << findMaxPoints(row,2,1,matrix) << endl;
}
return 0;
}
/*
No rech top
#include <iostream>
using namespace std;
int det[5][5];
int mat[13][5];
void detonate(int r)
{
for(int i=r;i>r-5 && i>=0;i--)
{
for(int j=0;j<5;j++)
{
det[r-i][j]=0;
if(mat[i][j]==2)
{
det[r-i][j]=2;
mat[i][j]=0;
}
}
}
}
void undet(int r)
{
for(int i=r;i>r-5 && i>=0;i--)
for(int j=0;j<5;j++)
{
if( det[r-i][j]==2)
mat[i][j]=2;
}
}
void func(int n,int pos,int c,int &max)
{
if(pos>4||pos<0||c<0)
return;
if(mat[n][pos]==2)
c-=1;
else if(mat[n][pos]==1)
c+=1;
if(n==0)
{
if(c>max)
max=c;
return;
}
else
{
func(n-1,pos+1,c,max);
func(n-1,pos-1,c,max);
func(n-1,pos,c,max);
}
}
int main()
{
int t;
cin>>t;
int count=1;
while(t--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<5;j++)
cin>>mat[i][j];
int max=-1,c;
for(int j=0;j<5;j++)
mat[n][j]=0;
mat[n][2]=3;
for(int j=n;j>=5;j--)
{
c=-1;
detonate(j-1);
func(n,2,0,c);
if(c>max)
max=c;
undet(j-1);
}
if(max<0)
max=-1;
cout<<"#"<<count<<" "<<max<<endl;
count++;
}
}
*/
/*
#include <iostream>
using namespace std;
int det[5][5];
int mat[13][5];
void detonate(int r){
for(int i=r;i>r-5 && i>=0;i--){
for(int j=0;j<5;j++){
det[r-i][j]=0;
if(mat[i][j]==2){
det[r-i][j]=2;
mat[i][j]=0;
}
}
}
}
void undet(int r){
for(int i=r;i>r-5 && i>=0;i--)
for(int j=0;j<5;j++){
if( det[r-i][j]==2)
mat[i][j]=2;
}
}
void func(int n,int pos,int c,int &max){
if(pos>4||pos<0||c<0)
return;
if(mat[n][pos]==2)
c-=1;
else if(mat[n][pos]==1)
c+=1;
if(n==0){
if(c>max)
max=c;
return;
}
else{
func(n-1,pos+1,c,max);
func(n-1,pos-1,c,max);
func(n-1,pos,c,max);
}
}
int main(){
int t;
cin>>t;
int count=1;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<5;j++)
cin>>mat[i][j];
for(int j=0;j<5;j++)
mat[n][j]=0;
mat[n][2]=3;
int max=-1,c;
for(int j=n;j>=5;j--){
c=-1;
detonate(j-1);
func(n,2,0,c);
if(c>max)
max=c;
undet(j-1);
}
if(max<0)
max=-1;
cout<<"#"<<count<<" "<<max<<endl;
count++;
}
}
*/