-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLittle Elephant and Bombs.cpp
More file actions
45 lines (40 loc) · 1.39 KB
/
Copy pathLittle Elephant and Bombs.cpp
File metadata and controls
45 lines (40 loc) · 1.39 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
/*
The Little Elephant from the Zoo of Lviv currently is on the military mission.
There are N enemy buildings placed in a row and numbered from left to right strating from 0.
Each building i (except the first and the last) has exactly two adjacent buildings with indices i-1 and i+1.
The first and the last buildings have just a single adjacent building.
Some of the buildings contain bombs. When bomb explodes in some building it destroys it and all adjacent
to it buildings.
You are given the string S of length N, where Si is 1 if the i-th building contains bomb, 0 otherwise.
Find for the Little Elephant the number of buildings that will not be destroyed after all bombs explode.
Please note that all bombs explode simultaneously.
*/
#include<bits/stdc++.h>
using namespace std;
int remainingBuilding(string, int);
int main(void){
int t, n;
string str;
cin >> t;
while(t--){
cin >> n >> str;
cout << remainingBuilding(str, n) << endl;
}
return 0;
}
int remainingBuilding(string str, int n){
int arr[n], res[n];
memset(res, 0, sizeof(res));
for(int i = 0; i < str.length(); i++)
arr[i] = str[i] - '0';
for(int i = 0; i < n; i++){
if(arr[i] == 1){
res[i] = 1;
if(i - 1 >= 0)
res[i - 1] = 1;
if(i + 1 < n)
res[i + 1] = 1;
}
}
return count(res, res + n, 0);
}