Advertisement
Guest User

LargestAreaOfEqualNeighborElements

a guest
Jan 11th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LargestAreaOfEqualNeighborElements
  5. {
  6.     //Keeps the current element and its count
  7.     static int[] curr = new int[2];
  8.  
  9.     //Marks steps made by the code TRUE-been here, False-not been here
  10.     static bool[,] map;
  11.     static int maxCount = 0;
  12.  
  13.     static void Main()
  14.     {
  15.         int n = int.Parse(Console.ReadLine());
  16.         int m = int.Parse(Console.ReadLine());
  17.         int[,] matrix = new int[n, m];
  18.         map = new bool[n, m];
  19.         for (int row = 0; row < n; row++)
  20.         {
  21.             for (int col = 0; col < m; col++)
  22.             {
  23.                 matrix[row, col] = int.Parse(Console.ReadLine());
  24.             }
  25.         }
  26.         for (int row = 0; row < n; row++)
  27.         {
  28.             for (int col = 0; col < m; col++)
  29.             {
  30.                 int[] cordinates = new int[2] { row, col };
  31.                 findingEqualNeighors(matrix, cordinates);
  32.                 if (maxCount < curr[1])
  33.                 {
  34.                     maxCount = curr[1];
  35.                 }
  36.             }
  37.         }
  38.         Console.WriteLine(maxCount);
  39.     }
  40.  
  41.     static void findingEqualNeighors(int[,] matrix, int[] cordinates)
  42.     {
  43.         int currElement = matrix[cordinates[0], cordinates[1]];
  44.  
  45.         //curr[0] is the current element
  46.         curr[0] = currElement;
  47.  
  48.         //curr[1] is the counter for the current element
  49.         curr[1] = 1;
  50.         pathfinding(matrix, cordinates);
  51.     }
  52.  
  53.     static void pathfinding(int[,] matrix, int[] cordinates)
  54.     {
  55.         int row = cordinates[0];
  56.         int col = cordinates[1];
  57.         map[row, col] = true;
  58.  
  59.         //Check if step up is possible
  60.         if (row - 1 >= 0)
  61.         {
  62.             if (matrix[row - 1, col] == matrix[row, col] && map[row - 1, col] == false)
  63.             {
  64.                 curr[1]++;
  65.                 cordinates[0]--;
  66.                 pathfinding(matrix, cordinates);
  67.                 cordinates[0]++;
  68.             }
  69.         }
  70.  
  71.         //Check if step down is possible
  72.         if (row + 1 < matrix.GetLength(0))
  73.         {
  74.             if (matrix[row + 1, col] == matrix[row, col] && map[row + 1, col] == false)
  75.             {
  76.                 curr[1]++;
  77.                 cordinates[0]++;
  78.                 pathfinding(matrix, cordinates);
  79.                 cordinates[0]--;
  80.             }
  81.         }
  82.  
  83.         //Check if step left is possible
  84.         if (col - 1 >= 0)
  85.         {
  86.             if (matrix[row, col - 1] == matrix[row, col] && map[row, col - 1] == false)
  87.             {
  88.                 curr[1]++;
  89.                 cordinates[1]--;
  90.                 pathfinding(matrix, cordinates);
  91.                 cordinates[1]++;
  92.             }
  93.         }
  94.  
  95.         //Check if step right is possible
  96.         if (col + 1 < matrix.GetLength(1))
  97.         {
  98.             if (matrix[row, col + 1] == matrix[row, col] && map[row, col + 1] == false)
  99.             {
  100.                 curr[1]++;
  101.                 cordinates[1]++;
  102.                 pathfinding(matrix, cordinates);
  103.                 cordinates[1]--;
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement