361 Bomb Enemy

We have a 2D grid. Each cell is either a wall, an enemy or empty.

For example (0-empty, X-enemy, Y-wall):
0 X 0 0
X 0 Y X
0 X 0 0

You have one bomb and you want to kill as many as possible enemies with it. The bomb will kill all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.

Given such a grid, return the maximum enemies you can kill with one bomb. Note that you can only put the bomb at empty cell.

In the example, if you put a bomb at (1,1) you will kill 3 enemies which is the best you can get. You can not kill the guy behind the wall at (1,3).

#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