We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticList<string>cavityMap(List<string>grid){intgridSize=grid.Count-1;for(inti=1;i<gridSize;i++){char[]chars=grid[i].ToCharArray();// get cellsfor(intc=1;c<gridSize;c++){if(IsCavity(grid,i,c)){// Replace cavity with Xchars[c]='X';c++;// skip the next cell since it can't be a cavity}}grid[i]=newstring(chars);// add new line back to grid}returngrid;}publicstaticboolIsCavity(List<string>grid,intline,intcell){// Get the adjacent cells. If any aren't numbers then it's// next to a cavity so it fails.if(!Int32.TryParse(grid[line-1][cell].ToString(),outintup)||!Int32.TryParse(grid[line+1][cell].ToString(),outintdown)||!Int32.TryParse(grid[line][cell-1].ToString(),outintleft)||!Int32.TryParse(grid[line][cell+1].ToString(),outintright))returnfalse;intpotCav=Int32.Parse(grid[line][cell].ToString());// Check if all adjacent cells are lower than the potential// cavity.if(up<potCav&&down<potCav&&left<potCav&&right<potCav)returntrue;returnfalse;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cavity Map
You are viewing a single comment's thread. Return to all comments →
In C#