> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/361_bomb_enemy.md).

# 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).

```cpp
#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;

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/361_bomb_enemy.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
