Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Task4
- {
- class Program
- {
- private static bool dfs(int i, int j, int[,] g)
- {
- if (i < 0 || i >= g.GetLength(0) || j < 0 || j >= g.GetLength(0) || g[i, j] == 0)
- return false;
- g[i, j] = 0;
- dfs(i - 1, j, g);
- dfs(i + 1, j, g);
- dfs(i, j - 1, g);
- dfs(i, j + 1, g);
- return true;
- }
- private static int numIslands(int[,] grid)
- {
- int[,] g = grid;
- int n = grid.GetLength(0);
- if (n == 0)
- return 0;
- int m = grid.GetLength(1);
- if (m == 0)
- return 0;
- int count = 0;
- for (int i = 0; i < n; ++i)
- for (int j = 0; j < m; ++j)
- if (dfs(i, j, g))
- ++count;
- return count;
- }
- static void Main(string[] args)
- {
- string input = Console.In.ReadToEnd();
- string[] strings = input.Split('\n');
- char[] MyChar = { '\r', ']', '[', ' ' };
- int n = strings.Length - 3;
- int m = strings[1].Split(',').Length - 1;
- int[,] matrix = new int[n, m];
- for (int i = 1; i < strings.Length - 2; ++i)
- {
- string[] str = strings[i].Split(',');
- str[0] = str[0].TrimStart(MyChar);
- str[str.Length - 2] = str[str.Length - 2].TrimEnd(MyChar);
- for (int j = 0; j < str.Length - 1; ++j)
- matrix[i - 1, j] = int.Parse(str[j]);
- }
- Console.WriteLine(numIslands(matrix));
- //for (int i = 0; i < n; i++)
- //{
- // for (int j = 0; j < m; j++)
- // Console.Write($"{matrix[i, j]} ");
- // Console.WriteLine();
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement