Dada una array mat[][] que contiene los caracteres [1 – 9] , P y * donde P es una persona y * es un espacio vacío. Un número entero en la celda actual denota una bomba que puede destruir un objeto dentro de un radio igual al valor de la celda. La tarea es contar e imprimir el número de personas que sobrevivirán a la explosión.
Ejemplos:
Input: mat[] = { "P***", "****", "2**P", "****" } Output: 1 Only the person in the third row will survive. Note that the person in the first row is within the radius of the bomb. Input: mat[] = { "2***P*", "P*****", "******", "**1***", "******", "*****P" } Output: 2 The persons in the first and last row survive.
Enfoque: Atraviese la array carácter por carácter, si el carácter actual es un dígito (1 – 9) , cambie todos los elementos circundantes dentro del radio a * (en las ocho direcciones), lo que indica que nadie sobrevivió dentro de ese radio. Después de recorrer todos los elementos de la array, cuente el número de celdas que indican una persona, es decir , mat[i][j] = ‘P’ . Imprime el conteo al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the count of // the people that survived int countSurvivors(string mat[], int row, int col) { int i, j, count = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // If current cell is a bomb if (mat[i][j] > '0' && mat[i][j] <= '9') { int radius = mat[i][j] - '0'; // Coordinates of the sub-matrix // that will be affected by the blast int iMin = max(i - radius, 0); int iMax = min(i + radius, row - 1); int jMin = max(j - radius, 0); int jMax = min(j + radius, col - 1); for (int k = iMin; k <= iMax; k++) for (int l = jMin; l <= jMax; l++) // Set the current cell to // empty space without a person mat[k][l] = '*'; } } } // Count the remaining persons for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (mat[i][j] == 'P') count++; return count; } // Driver Code int main() { // Input matrix string mat[] = { "P***", "****", "2**P", "****" }; // Rows and columns of the matrix int row = sizeof(mat) / sizeof(mat[0]); int col = mat[0].length(); // Total people survived cout << countSurvivors(mat, row, col); }
Java
// Java implementation of the approach class GFG { // Function to return the count of // the people that survived static int countSurvivors(String mat[], int row, int col) { int i, j, count = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // If current cell is a bomb if (mat[i].charAt(j) > '0' && mat[i].charAt(j) <= '9') { int radius = mat[i].charAt(j) - '0'; // Coordinates of the sub-matrix // that will be affected by the blast int iMin = Math.max(i - radius, 0); int iMax = Math.min(i + radius, row - 1); int jMin = Math.max(j - radius, 0); int jMax = Math.min(j + radius, col - 1); for (int k = iMin; k <= iMax; k++) { // convert string nto character array char C [] = mat[k].toCharArray(); for (int l = jMin; l <= jMax; l++) { // Set the current cell to // empty space without a person C [l] = '*' ; } // convert character array back into the string String s = new String(C) ; mat[k] = s ; } } } } // Count the remaining persons for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (mat[i].charAt(j) == 'P') count++; return count; } // Driver Code public static void main(String []args) { // Input matrix String mat[] = { "P***", "****", "2**P", "****" }; // Rows and columns of the matrix int row = mat.length ; int col = mat[0].length(); // Total people survived System.out.println(countSurvivors(mat, row, col)); } } // This code is contributed by Ryuga
Python3
# Python3 implementation of the approach import math as mt # Function to return the count of # the people that survived def countSurvivors(mat, row, col): i, j, count = 0, 0, 0 for i in range(row): for j in range(col): # If current cell is a bomb if (mat[i][j] > '0' and mat[i][j] <= '9'): radius = ord(mat[i][j]) - ord('0') # Coordinates of the sub-matrix # that will be affected by the blast iMin = max(i - radius, 0) iMax = min(i + radius, row - 1) jMin = max(j - radius, 0) jMax = min(j + radius, col - 1) for k in range(iMin, iMax + 1): for l in range(jMin, jMax + 1): # Set the current cell to # empty space without a person mat[k][l] = '*' # Count the remaining persons for i in range(row): for j in range(col): if (mat[i][j] == 'P'): count += 1 return count # Driver Code # Input matrix mat = [["P", "*", "*", "*"], ["*", "*", "*", "*"], ["2", "*", "*", "P"], ["*", "*", "*", "*"]] # Rows and columns of the matrix row = len(mat) col = len(mat[0]) # Total people survived print(countSurvivors(mat, row, col)) # This code is contributed by # Mohit Kumar 29
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function to return the count of // the people that survived static int countSurvivors(String []mat, int row, int col) { int i, j, count = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // If current cell is a bomb if (mat[i][j] > '0' && mat[i][j] <= '9') { int radius = mat[i][j] - '0'; // Coordinates of the sub-matrix // that will be affected by the blast int iMin = Math.Max(i - radius, 0); int iMax = Math.Min(i + radius, row - 1); int jMin = Math.Max(j - radius, 0); int jMax = Math.Min(j + radius, col - 1); for (int k = iMin; k <= iMax; k++) { // convert string nto character array char []C = mat[k].ToCharArray(); for (int l = jMin; l <= jMax; l++) { // Set the current cell to // empty space without a person C [l] = '*'; } // convert character array back into the string String s = new String(C); mat[k] = s; } } } } // Count the remaining persons for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (mat[i][j] == 'P') count++; return count; } // Driver Code public static void Main(String []args) { // Input matrix String []mat = { "P***", "****", "2**P", "****" }; // Rows and columns of the matrix int row = mat.Length ; int col = mat[0].Length; // Total people survived Console.WriteLine(countSurvivors(mat, row, col)); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript implementation of the approach // Function to return the count of // the people that survived function countSurvivors(mat, row, col) { let i, j, count = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // If current cell is a bomb if (mat[i][j].charCodeAt() > '0'.charCodeAt() && mat[i][j].charCodeAt() <= '9'.charCodeAt()) { let radius = mat[i][j].charCodeAt() - '0'.charCodeAt(); // Coordinates of the sub-matrix // that will be affected by the blast let iMin = Math.max(i - radius, 0); let iMax = Math.min(i + radius, row - 1); let jMin = Math.max(j - radius, 0); let jMax = Math.min(j + radius, col - 1); for (let k = iMin; k <= iMax; k++) { // convert string nto character array let C = mat[k].split(''); for (let l = jMin; l <= jMax; l++) { // Set the current cell to // empty space without a person C[l] = '*' ; } // convert character array // back into the string let s = C.join(""); mat[k] = s ; } } } } // Count the remaining persons for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (mat[i][j] == 'P') count++; return count; } // Input matrix let mat = [ "P***", "****", "2**P", "****" ]; // Rows and columns of the matrix let row = mat.length; let col = mat[0].length; // Total people survived document.write(countSurvivors(mat, row, col)); </script>
1
Publicación traducida automáticamente
Artículo escrito por GirishKumarS y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA