361 Bomb Enemy
For example (0-empty, X-enemy, Y-wall):
0 X 0 0
X 0 Y X
0 X 0 0#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
class BombEnemy {
public:
BombEnemy(vector<vector<char>> &map) {
count = 0;
theGrid = map;
killsArray.reserve(theGrid.size() * theGrid.at(0).size());
};
// brute force
int getMaxKills() {
int explore;
for (int pos_4y = 0; pos_4y < theGrid.size(); pos_4y++) {
for (int pos_4x = 0; pos_4x < theGrid.at(pos_4y).size(); pos_4x++) {
// must be on empty cell
if (theGrid.at(pos_4y).at(pos_4x) == 'Y' || theGrid.at(pos_4y).at(pos_4x) == 'X')
continue;
// explore right starting from (posX, posY)
explore = pos_4x;
for (explore; explore < theGrid.at(pos_4y).size(); explore++) {
cout << theGrid.at(pos_4y).at(explore) << " ";
if (theGrid.at(pos_4y).at(explore) == 'Y') {
break;
}
else if (theGrid.at(pos_4y).at(explore) == 'X') {
count++;
}
}
cout << endl;
// explore down
explore = pos_4y;
for (explore; explore < theGrid.size(); explore++) {
cout << theGrid.at(explore).at(pos_4x) << " ";
if (theGrid.at(explore).at(pos_4x) == 'Y') {
break;
}
else if (theGrid.at(explore).at(pos_4x) == 'X') {
count++;
}
}
cout << endl;
// explore left
explore = pos_4x;
for (explore; explore >= 0; explore--) {
cout << theGrid.at(pos_4y).at(explore) << " ";
if (theGrid.at(pos_4y).at(explore) == 'Y') {
break;
}
else if (theGrid.at(pos_4y).at(explore) == 'X') {
count++;
}
}
cout << endl;
// explore top
explore = pos_4y;
for (explore; explore >= 0; explore--) {
cout << theGrid.at(explore).at(pos_4x) << " ";
if (theGrid.at(explore).at(pos_4x) == 'Y') {
break;
}
else if (theGrid.at(explore).at(pos_4x) == 'X') {
count++;
}
}
cout << endl << endl;
killsArray.push_back(count);
count = 0;
}
cout << endl << endl;
}
return *max_element(killsArray.begin(), killsArray.end());
}
private:
int count;
vector<vector<char>> theGrid;
vector<char> killsArray;
};
int main() {
vector<vector<char>> ok = {
{ '0', 'X', '0', '0' },
{ 'X', '0', 'Y', 'X' },
{ '0', 'X', '0', '0' },
{ '0', 'X', '0', '0' },
{ '0', 'X', '0', 'Y' },
{ '0', '0', '0', '0' },
{ 'Y', 'X', 'X', '0' },
{ '0', 'X', '0', 'Y' },
};
// should return
BombEnemy *test = new BombEnemy(ok);
int result = test->getMaxKills();
cout << result;
}Last updated