Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace islands
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- // 0 - sea, 1 - unknown teritory
- int[,] sea = new int[,]
- {
- { 1, 0, 1, 0, 0, 0, 1, },
- { 0, 0, 1, 1, 1, 0, 1, },
- { 1, 1, 1, 0, 0, 0, 1, },
- { 0, 0, 1, 1, 0, 1, 1, },
- { 0, 0, 0, 0, 0, 1, 1, },
- { 1, 1, 0, 0, 1, 1, 1, },
- { 0, 1, 0, 0, 0, 0, 0, },
- };
- //biggestIsland description
- int sizeBI = 0;
- int signBI = 0;
- //marking first island with number(2)
- int sign = 2;
- for (int row = 0; row < sea.GetLength(0); row++)
- {
- for (int column = 0; column < sea.GetLength(1); column++)
- {
- if (sea[row, column] == 1)
- {
- int islandSize = 0;
- MarkIsland(sea, row, column, sign, ref islandSize);
- if (islandSize > sizeBI)
- {
- sizeBI = islandSize;
- signBI = sign;
- }
- sign++;
- }
- }
- }
- Console.WriteLine($"Biggest island is {signBI}, with size: {sizeBI}");
- OutputIsland(sea);
- }
- private static void MarkIsland(int[,] sea, int row, int column, int currentSign, ref int islandSize)
- {
- if (ValidField(sea, row, column) && FoundIsland(sea, row, column))
- {
- sea[row, column] = currentSign;
- islandSize++;
- MarkIsland(sea, row - 1, column, currentSign, ref islandSize);//up
- MarkIsland(sea, row + 1, column, currentSign, ref islandSize);//down
- MarkIsland(sea, row, column - 1, currentSign, ref islandSize);//left
- MarkIsland(sea, row, column + 1, currentSign, ref islandSize);//right
- }
- }
- private static bool FoundIsland(int[,] sea, int row, int column)
- {
- return sea[row, column] == 1;
- }
- private static bool ValidField(int[,] sea, int row, int column)
- {
- return row >= 0 && row < sea.GetLength(0) && column >= 0 && column < sea.GetLength(1);
- }
- private static void OutputIsland(int[,] sea)
- {
- for (int row = 0; row < sea.GetLength(0); row++)
- {
- for (int column = 0; column < sea.GetLength(1); column++)
- {
- Console.Write($"{sea[row, column],7}");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement