int maxKilledEnemies(vector<vector<char>>& grid) { // start coding at 1:32 if (!grid.size() || !grid[0].size()) return 0; vector<vector<int>> inRow(grid.size(), vector<int>(grid[0].size(), 0)); vector<vector<int>> inCol(grid.size(), vector<int>(grid[0].size(), 0)); for (int i = 0; i < grid.size(); i++) { unordered_map<int, int> temp; int wall = -1; temp[wall] = 0; for (int j = 0; j < grid[0].size(); j++) { if (grid[i][j] == 'W') { wall = j; continue; } if (grid[i][j] == '0') continue; if (grid[i][j] == 'E') temp[wall]++; } for (auto t : temp) { for (int k = t.first + 1; k < grid[0].size(); k++) inRow[i][k] = t.second; if (t.first >= 0) inRow[i][t.first] = 0; } } for (int j = 0; j < grid[0].size(); j++) { unordered_map<int, int> temp; int wall = -1; temp[wall] = 0; for (int i = 0; i < grid.size(); i++) { if (grid[i][j] == 'W') { wall = i; continue; } if (grid[i][j] == '0') continue; if (grid[i][j] == 'E') temp[wall]++; } for (auto t : temp) { for (int k = t.first + 1; k < grid.size(); k++) inCol[k][j] = t.second; if (t.first >= 0) inCol[t.first][j] = 0; } } int ans = 0; for (int i = 0; i < grid.size(); i++) for (int j = 0; j < grid[0].size(); j++) { if (grid[i][j] == 'E') continue; ans = max(ans, inRow[i][j] + inCol[i][j]); } return ans; }
//check if numberToCheck is valid for the cell bool Puzzle::isValid(int currentRow, int currentColumn, int numberToCheck) { return (!inRow(currentRow, numberToCheck) && !inColumn(currentColumn, numberToCheck) && !inRegion(currentRow, currentColumn, numberToCheck)); }