Advertisement
Guest User

Stack

a guest
Jul 22nd, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. class LargestArea
  5. {
  6.     static void Main()
  7.     {
  8.         int[,] matrix =
  9.         {
  10.             {1,3,2,2,2,4},
  11.             {3,3,3,2,4,4},
  12.             {4,3,1,2,3,3},
  13.             {4,3,1,3,3,1},
  14.             {4,3,3,3,1,1}
  15.         };
  16.         bool[,] visited=new bool[matrix.GetLength(0),matrix.GetLength(1)];
  17.  
  18.         int largestArea = 0;
  19.         Stack stack=new Stack();
  20.  
  21.         for (int i = 0; i < matrix.GetLength(0); i++)
  22.         {
  23.             for (int j = 0; j < matrix.GetLength(1); j++)
  24.             {
  25.  
  26.                 if (visited[i, j])
  27.                 {
  28.                     continue;
  29.                 }
  30.                 ArrayElement current = new ArrayElement(i,j);
  31.                 stack.Push(current);
  32.                 int currentArea=0;
  33.                 int value = matrix[i, j];
  34.                 while (stack.Count != 0)
  35.                 {
  36.                     ArrayElement peek = (ArrayElement)stack.Peek();
  37.                     int currentRow=peek.row, currentCol=peek.column;
  38.                     currentArea++;
  39.                     visited[currentRow, currentCol] = true;
  40.                     stack.Pop();
  41.                     //UpperLeft Diagonal
  42.                     if (currentRow - 1 >= 0 && currentCol - 1 >= 0 && !visited[currentRow - 1, currentCol - 1] && matrix[currentRow - 1, currentCol - 1] == value)
  43.                     {
  44.                         stack.Push(new ArrayElement(currentRow - 1, currentCol - 1));
  45.                         visited[currentRow - 1, currentCol - 1] = true;
  46.                     }
  47.                     //Upper
  48.                     if (currentRow - 1 >= 0 && !visited[currentRow - 1, currentCol] && matrix[currentRow - 1, currentCol] == value)
  49.                     {
  50.                         stack.Push(new ArrayElement(currentRow - 1, currentCol));
  51.                         visited[currentRow - 1, currentCol] = true;
  52.                     }
  53.                     //UpperRight Diagonal
  54.                     if (currentRow - 1 >= 0 && currentCol + 1 <= 5 && !visited[currentRow - 1, currentCol + 1] && matrix[currentRow - 1, currentCol + 1] == value)
  55.                     {
  56.                         stack.Push(new ArrayElement(currentRow - 1, currentCol + 1));
  57.                         visited[currentRow - 1, currentCol + 1] = true;
  58.                     }
  59.                     //Left
  60.                     if (currentCol - 1 >= 0 && !visited[currentRow, currentCol - 1] && matrix[currentRow, currentCol - 1] == value)
  61.                     {
  62.                         stack.Push(new ArrayElement(currentRow, currentCol - 1));
  63.                         visited[currentRow , currentCol - 1] = true;
  64.                     }
  65.                     //Right
  66.                     if (currentCol + 1 <= 5 && !visited[currentRow, currentCol + 1] && matrix[currentRow, currentCol + 1] == value)
  67.                     {
  68.                         stack.Push(new ArrayElement(currentRow, currentCol + 1));
  69.                         visited[currentRow , currentCol + 1] = true;
  70.                     }
  71.                     //LowerLeft Diagonal
  72.                     if (currentRow + 1 <= 4 && currentCol - 1 >= 0 && !visited[currentRow + 1, currentCol - 1] && matrix[currentRow + 1, currentCol - 1] == value)
  73.                     {
  74.                         stack.Push(new ArrayElement(currentRow + 1, currentCol - 1));
  75.                         visited[currentRow + 1, currentCol - 1] = true;
  76.                     }
  77.                     //Lower
  78.                     if (currentRow + 1 <= 4 && !visited[currentRow + 1, currentCol] && matrix[currentRow + 1, currentCol] == value)
  79.                     {
  80.                         stack.Push(new ArrayElement(currentRow + 1, currentCol));
  81.                         visited[currentRow + 1, currentCol] = true;
  82.                     }
  83.                     //LowerRight Diagonal
  84.                     if (currentRow + 1 <= 4 && currentCol + 1 <= 5 && !visited[currentRow + 1, currentCol + 1] && matrix[currentRow + 1, currentCol + 1] == value)
  85.                     {
  86.                         stack.Push(new ArrayElement(currentRow + 1, currentCol + 1));
  87.                         visited[currentRow + 1, currentCol + 1] = true;
  88.                     }
  89.  
  90.                    
  91.  
  92.                 }
  93.                 if (currentArea>largestArea)
  94.                 {
  95.                     largestArea = currentArea;
  96.                 }
  97.             }
  98.         }
  99.         Console.WriteLine("The largest Area is {0}!" ,largestArea);
  100.     }
  101.     class ArrayElement
  102.     {
  103.         public int row, column;
  104.         public ArrayElement(int i,int j)
  105.         {
  106.             row=i;
  107.             column=j;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement