Advertisement
Teodor92

MultiArray.3.MaxStringArea.cs

Jan 22nd, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class StringMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         // direction arrays
  9.         // TODO Merge in one array
  10.         int[] dirRow = { 1, 0, 1, -1 };
  11.         int[] dirCol = { 0, 1, 1, 1 };
  12.         // The string array
  13.         string[,] matrixStrings =
  14.                 {
  15.                     {"ha", "asd", "s", "s", "s","ha"},
  16.                     {"g", "ha", "s", "s", "","ha"},
  17.                     {"", "g", "s", "s", "","ha"},
  18.                     {"s", "s", "", "s", "","ha"},
  19.                     {"s", "", "s", "s", "ha","ha"},
  20.                     {"s", "s", "s", "s", "ha","ha"},
  21.                     {"s", "", "s", "s", "ha","ha"},
  22.                     {"s", "", "s", "s", "ha","ha"},
  23.                 };
  24.         int BestLen = 0;
  25.         string BestString = "";
  26.         int numRows = matrixStrings.GetLength(0);
  27.         int numCols = matrixStrings.GetLength(1);
  28.         for (int row = 0; row < numRows; row++)
  29.         {
  30.             for (int col = 0; col < numCols; col++)
  31.             {
  32.                 string currentStr = matrixStrings[row, col];
  33.                 int currentLen = 1;
  34.                 // direction change
  35.                 for (int direction = 0; direction < 4; direction++)
  36.                 {
  37.                     int currentRow = row;
  38.                     int currentCol = col;
  39.                     while (true)
  40.                     {
  41.                         currentCol += dirCol[direction];
  42.                         currentRow += dirRow[direction];
  43.                         // Out of array na different string checks
  44.                         // TODO - make a seperate method
  45.                         if (currentCol < 0 ||
  46.                             currentCol >= numCols ||
  47.                             currentRow < 0 ||
  48.                             currentRow >= numRows ||
  49.                             currentStr != matrixStrings[currentRow, currentCol])
  50.                         {
  51.                             break;
  52.                         }
  53.                         currentLen++;
  54.                         // best len check
  55.                         if (currentLen >= BestLen)
  56.                         {
  57.                             BestLen = currentLen;
  58.                             BestString = currentStr;
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         Console.WriteLine("The longest sequence of strings in the array is - \"{0}\", with length - {1}", BestString, BestLen);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement