In this HackerRank Cavity Map problem, You are given a square map as a matrix of integer strings. Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has a strictly smaller depth. Two cells are adjacent if they have a common side or edge.
Find all the cavities on the map and replace their depths with the uppercase character X.
Problem solution in Python programming.
n = int(input()) a = [list(input()) for _ in range(n)] def getval(p, off): px = p[0] + off[0] py = p[1] + off[1] pi = a[px][py] return 10 if pi == 'X' else int(pi) for i in range(1, n-1): for j in range(1, n-1): p = (i, j) if getval(p, (0, 0)) > max(getval(p, off) for off in ( (0, 1), (0, -1), (-1, 0), (1, 0))): a[i][j] = 'X' for row in a: print(''.join(row))
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); int[][] array = new int[n][n]; int[][] target = new int[n][n]; for(int i=0; i<n; i++){ String line = in.nextLine(); char[] data = line.toCharArray(); for(int j=0; j<n; j++){ array[i][j] = Character.getNumericValue(data[j]); //System.out.println(array[i][j]); } } cavityMap(array, target); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(target[i][j] == -1) System.out.print("X"); else System.out.print(array[i][j]); } System.out.println(""); } } public static void cavityMap(int[][] array, int[][] target) { int row = array.length; int col = array[0].length; for(int i=1; i<row-1; i++){ for(int j=1; j<col-1; j++){ if(isMaximum(array, i, j)){ target[i][j] = -1; // will be translate to X later } } } } public static boolean isMaximum(int[][] array, int i, int j) { int max = Math.max(Math.max(array[i-1][j], array[i+1][j]), Math.max(array[i][j-1], array[i][j+1])); if(max >= array[i][j]) return false; else return true; } }
Problem solution in C++ programming.
#include <iostream> #include <ctime> #include <fstream> #include <cmath> #include <cstring> #include <cassert> #include <cstdio> #include <algorithm> #include <iomanip> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <complex> #include <utility> #include <cctype> #include <list> #include <deque> using namespace std; #define FORALL(i,a,b) for(int i=(a);i<=(b);++i) #define FOR(i,n) for(int i=0;i<(n);++i) #define FORB(i,a,b) for(int i=(a);i>=(b);--i) typedef long long ll; typedef long double ld; typedef complex<ld> vec; typedef pair<int,int> pii; typedef map<int,int> mii; #define pb push_back #define mp make_pair #define MV 4 #define MAXN 102 char A[MAXN][MAXN]; char B[MAXN][MAXN]; int dr[] = {-1,0,1,0}; int dc[] = {0,-1,0,1}; int N; bool is_cavity(int i, int j) { if (i==0 || i==N-1) return false; if (j==0 || j==N-1) return false; FOR(mv,MV) { int i2 = i + dr[mv]; int j2 = j + dc[mv]; if (A[i2][j2] >= A[i][j]) return false; } return true; } int main() { cin >> N; FOR(i,N) scanf("%s",&A[i][0]); FOR(i,N) FOR(j,N) if (is_cavity(i,j)) B[i][j] = 'X'; else B[i][j] = A[i][j]; FOR(i,N) cout << B[i] << endl; }
Problem solution in C programming.
#include<stdio.h> int main() { int t; int i,j,n; scanf("%d",&n); char a[n+2][n+2]; char b[n+2][n+2]; for(i=0; i<n; i++) { scanf("%s",a[i]); } for(i=0; i<n ;i++) { for(j=0; j<n; j++) { if( i > 0 && i < n-1 && j > 0 && j < n-1) { char ch = a[i][j]; if(ch > a[i+1][j] && ch > a[i][j+1] && ch > a[i-1][j] && ch > a[i][j-1]) b[i][j] = 'X'; else b[i][j] = a[i][j]; } else b[i][j] = a[i][j]; } b[i][j] = 0; } for(i=0;i<n;i++) { printf("%sn",b[i]); } return 0; }
Problem solution in JavaScript programming.
function processData(input) { input = input.split('n') input.shift() for (var i = 0; i < input.length ; i++ ){ input[i] = input[i].split(""); } var output = cavityMap(input); output.forEach(function(line){ console.log( line.join("") ); }) } function cavityMap (map) { var size = map.length-1; var cavs = []; for (var i = 1; i < size; i++) { for (var j = 1; j < size; j++) { var curr = map[i][j]; if ( curr > map[i-1][j] && curr > map[i+1][j] ){ if (curr > map[i][j-1] && curr > map[i][j+1]) { cavs.push([i,j]); } } }; }; cavs.forEach(function (cavity){ map[cavity[0]][cavity[1]] = 'X'; }) return map; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });