Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Coding Challenge - Body
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var matrixXY = Int32.Parse(Console.ReadLine()); int[,] matrix = new int[matrixXY,matrixXY]; ReadMatrix(ref matrix,matrixXY); var waterBodySizes = FindSortedBodiesOfWater(ref matrix,matrixXY); foreach(var size in waterBodySizes) { Console.WriteLine(size); } } private static void ReadMatrix(ref int[,] matrix,int size) { for(int i = 0; i < size; ++i) { var items = Console.ReadLine().Split(' '); for(int j = 0; j < items.Length; ++j) { var num = Int32.Parse(items[j]); matrix[i,j] = num; } } } private static List<int> FindSortedBodiesOfWater(ref int[,] map, int size) { List<int> sizesOfWaterBodies = new List<int>(); for(int i = 0; i < size; ++i) { for(int j = 0; j < size; ++j) { int waterBodySize = GetBodyOfWaterSize(ref map, size, i, j); if(waterBodySize > 0) { sizesOfWaterBodies.Add(waterBodySize); } } } sizesOfWaterBodies.Sort(); return sizesOfWaterBodies; } private static int GetBodyOfWaterSize(ref int[,] map, int sizeOfMap, int x, int y) { int sizeOfWater = 0; if(map[x,y] == 0) { map[x,y] = -1; ++sizeOfWater; // north if(y - 1 >= 0) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap,x, y-1); // northeast if(x + 1 < sizeOfMap) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x + 1, y - 1); } // northwest if(x - 1 >= 0) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x - 1, y - 1); } } // east if(x + 1 < sizeOfMap) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x+1,y); } // south if(y + 1 < sizeOfMap) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x, y + 1); // southeast if(x + 1 < sizeOfMap) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x + 1, y + 1); } // southwest if( x - 1 >= 0) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x - 1, y + 1); } } // west if(x - 1 >= 0) { sizeOfWater += GetBodyOfWaterSize(ref map, sizeOfMap, x - 1, y); } } return sizeOfWater; } } }
5 1 2 1 1 0 0 1 1 0 1 0 0 1 1 1 1 1 0 1 1 0 1 1 1 0
Show compiler warnings
[
-
]
Show input
Compilation time: 0,14 sec, absolute running time: 0,08 sec, cpu time: 0,08 sec, average memory usage: 12 Mb, average nr of threads: 3
fork mode
|
history
1 1 2 4