Advertisement
GrandtherAzaMarks

Find the maximum area of figure of 1 in matrix

Apr 30th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace DictionaryLKSH
  4. {
  5.     class MainClass
  6.     {
  7.         static void fun(int[][] field, int Cx, int Cy, ref bool[,] checker, ref int answer)
  8.         {
  9.             answer++;
  10.             checker[Cx, Cy] = false;
  11.             if (checker[Cx, Cy] == false && checker[Cx + 1, Cy] == false && checker[Cx - 1, Cy] == false && checker[Cx, Cy + 1] == false && checker[Cx, Cy - 1] == false)
  12.                 return;
  13.             else
  14.             {
  15.                 if (checker[Cx + 1, Cy])
  16.                 {
  17.                     checker[Cx + 1, Cy] = false;
  18.                     fun(field, Cx + 1, Cy, ref checker, ref answer);
  19.                 }
  20.  
  21.                 if (checker[Cx - 1, Cy])
  22.                 {
  23.                     checker[Cx - 1, Cy] = false;
  24.                     fun(field, Cx - 1, Cy, ref checker, ref answer);
  25.                 }
  26.  
  27.                 if (checker[Cx, Cy + 1])
  28.                 {
  29.                     checker[Cx, Cy + 1] = false;
  30.                     fun(field, Cx, Cy + 1, ref checker, ref answer);
  31.                 }
  32.  
  33.                 if (checker[Cx, Cy - 1])
  34.                 {
  35.                     checker[Cx, Cy - 1] = false;
  36.                     fun(field, Cx, Cy - 1, ref checker, ref answer);
  37.                 }
  38.             }
  39.         }
  40.  
  41.         public static void Main(string[] args)
  42.         {
  43.  
  44.  
  45.             int[] TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
  46.             int n = TempArray[0] + 2, m = TempArray[1] + 2;
  47.             int[][] Field = new int[n][];
  48.             int answer = 0;
  49.             int realAnswer = 0;
  50.             for (int i = 0; i < n; i++)
  51.                 Field[i] = new int[m];
  52.             bool[,] checker = new bool[n, m];
  53.             for (int i = 0; i < n; i++)//wall of 2
  54.                 for (int j = 0; j < m; j++)
  55.                 {
  56.                     if (i == 0 || i == n - 1 || j == 0 || j == m - 1) Field[i][j] = 2;
  57.                     else Field[i][j] = 0;
  58.                 }
  59.             for (int i = 1; i < n - 1; i++)//fill the mass
  60.             {
  61.                 TempArray = Console.ReadLine().Split(' ').Select(e => Convert.ToInt32(e)).ToArray();
  62.                 for (int j = 1; j < m - 1; j++)
  63.                 {
  64.                     Field[i][j] = TempArray[j - 1];
  65.                 }
  66.             }
  67.             for (int i = 0; i < n; i++)//hcecker
  68.                 for (int j = 0; j < m; j++)
  69.                     if (Field[i][j] == 1) checker[i, j] = true;//shoulde be checked
  70.                     else checker[i, j] = false;//checked
  71.  
  72.             for (int i = 1; i < n - 1; i++)//search
  73.             {
  74.                 for (int j = 1; j < m; j++)
  75.                 {
  76.                     if (checker[i, j])
  77.                     {
  78.                         fun(Field, i, j, ref checker, ref answer);
  79.                         if (answer > realAnswer)
  80.                         {
  81.                             realAnswer = answer;
  82.                         }
  83.                         answer = 0;
  84.                     }
  85.                 }
  86.             }
  87.             Console.WriteLine(realAnswer);
  88.         }
  89.     }
  90. }
  91. /*
  92. 6 10
  93. 1 1 0 0 0 0 0 1 1 0
  94. 0 1 1 1 0 1 0 1 1 0
  95. 0 1 1 0 1 0 1 0 1 0
  96. 0 1 0 1 0 1 0 1 1 0
  97. 0 1 1 1 0 0 1 1 1 0
  98. 0 0 0 0 0 0 0 0 0 0
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement