Skip to content

Instantly share code, notes, and snippets.

@al1b al1b/CavityMap.cs
Created Sep 23, 2016

Embed
What would you like to do?
/*
Author: Ali Bahraminezhad
Description: You are given a square map of size . 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 strictly smaller depth.
Two cells are adjacent if they have a common side (edge).
https://www.hackerrank.com/challenges/cavity-map
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
var grid = new string[n];
for (var i = 0; i < n; i++)
grid[i] = Console.ReadLine();
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
var depth = grid[i][j];
if (i == 0 || j == 0 || i == n - 1 || j == n - 1)
Console.Write(grid[i][j]);
else if (depth > grid[i][j - 1] && depth > grid[i][j + 1] && depth > grid[i - 1][j] && depth > grid[i + 1][j])
Console.Write('X');
else
Console.Write(grid[i][j]);
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.