Advertisement
stak441

MultidimensionalArrays - problem 7*

Jan 18th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _07.EqualNeighbors
  7. {
  8.     class Program
  9.     {
  10.         static int tempCount = 1;
  11.         static bool[,] visited;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             int[,] matrix = {
  16.                                 {1, 3, 2, 2, 2, 4},
  17.                                 {3, 3, 3, 2, 2, 4},
  18.                                 {4, 3, 1, 2, 3, 3},
  19.                                 {4, 3, 1, 3, 3, 1},
  20.                                 {4, 3, 3, 3, 1, 1}
  21.                             };
  22.             visited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  23.             int bestCount = 0;
  24.             int bestNumber = 0;
  25.  
  26.  
  27.             for (int i = 0; i < matrix.GetLength(0); i++)
  28.             {
  29.                 for (int j = 0; j < matrix.GetLength(1); j++)
  30.                 {
  31.                     tempCount = 1;
  32.                     CountLength(matrix, visited, i, j);
  33.                     if (tempCount > bestCount)
  34.                     {
  35.                         bestCount = tempCount;
  36.                         bestNumber = matrix[i, j];
  37.                     }
  38.                    
  39.                 }
  40.             }
  41.             Console.WriteLine("The longest sequence is: {0}, the number is: {1}", bestCount, bestNumber);
  42.                    
  43.         }
  44.  
  45.         static void CountLength(int[,] matrix, bool[,] visited, int startRow, int startCol)
  46.         {
  47.             int number = matrix[startRow, startCol];
  48.             visited[startRow, startCol] = true;
  49.             int i = startRow;
  50.             int j = startCol;
  51.  
  52.             if (matrix[startRow, startCol] != number)
  53.             {
  54.                 return;
  55.             }
  56.            
  57.                 if (CellIsValid(matrix, visited, number, i - 1, j))     //=== check UP
  58.                 {
  59.                     tempCount++;
  60.                     CountLength(matrix, visited, i - 1, j);
  61.                 }
  62.                 if (CellIsValid(matrix, visited, number, i, j + 1))     //=== check RIGHT
  63.                 {
  64.                     tempCount++;
  65.                     CountLength(matrix, visited, i, j + 1);
  66.                 }
  67.                 if (CellIsValid(matrix, visited, number, i + 1, j))     //=== check DOWN
  68.                 {
  69.                     tempCount++;
  70.                     CountLength(matrix, visited, i + 1, j);
  71.                 }
  72.                 if (CellIsValid(matrix, visited, number, i, j - 1))     //=== check LEFT
  73.                 {
  74.                     tempCount++;
  75.                     CountLength(matrix, visited, i, j - 1);
  76.                 }
  77.         }
  78.  
  79.         private static bool CellIsValid(int[,] matrix, bool[,] visited, int number, int row, int col)
  80.         {
  81.             if (row >= 0 && row < matrix.GetLength(0) && col >=0 && col < matrix.GetLength(1))       //check if position is inside the matrix
  82.             {
  83.                 if (visited[row, col] == false && matrix[row, col] == number)            //check if the cell has been visited or if the number is different
  84.                 {
  85.                     return true;                      
  86.                 }
  87.                 else
  88.                 {
  89.                     return false;
  90.                 }
  91.             }
  92.             else
  93.             {
  94.                 return false;
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement