Advertisement
vlad0

Multidimensional Array Homework - 3

Jan 14th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03.BestSequenceOfStrings
  7. {
  8.     class Program
  9.     {
  10.         static int bestSequence = 0;
  11.         static string bestElement = String.Empty;
  12.         static int rowsCount;
  13.         static int colsCount;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             string[,] matrix =
  18.             {
  19.               {"ha","ho","hA","hO","xxx","he"},
  20.               {"fo","hAa","hA","xxx","hS","he"},
  21.               {"xxx","ho","xxx","xx","hS","he"},
  22.               {"xxx","xxx","xx","ha","hS","he"},
  23.               {"xxx","ho","ha","xx","ha","hE"},
  24.             };
  25.            
  26.             rowsCount = matrix.GetLength(0);
  27.             colsCount = matrix.GetLength(1);
  28.  
  29.             for (int i = 0; i < rowsCount; i++)
  30.             {
  31.                 for (int j = 0; j < colsCount; j++)
  32.                 {
  33.                     CheckMatrix(matrix, i, j);
  34.                 }
  35.             }
  36.  
  37.             Console.WriteLine("BestSequence: {0}", bestSequence);
  38.             Console.WriteLine("BestElement: {0}", bestElement);
  39.         }
  40.  
  41.         private static void CheckMatrix(string[,] matrix, int rows, int cols)
  42.         {
  43.             int startingRows = rows;
  44.             int startingCols = cols;
  45.             string searchingString = matrix[rows, cols];
  46.             //1-right rows is constant cols+1
  47.             //2-down rows+1 cols is constant
  48.             //3-diagonalDown starting row from zero to .Length rows+1 cols+1
  49.             //4-diagonalDown starting row from .Length to zerp rows-1 cols+1
  50.  
  51.             //right
  52.             int counter = 0;
  53.             while (cols + 1<= colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
  54.             {
  55.                 cols++;
  56.                 counter++;
  57.                
  58.             }
  59.             CheckBest(counter,searchingString);
  60.             //down
  61.             counter = 0;
  62.             rows = startingRows;
  63.             cols = startingCols;
  64.             while (rows +1 <= rowsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
  65.             {
  66.                 rows++;
  67.                 counter++;
  68.             }
  69.  
  70.             CheckBest(counter, searchingString);
  71.  
  72.             //diagonalDown
  73.             counter = 0;
  74.             rows = startingRows;
  75.             cols = startingCols;
  76.             while (rows + 1 <= rowsCount && cols+1 <=colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
  77.             {
  78.                 rows++;
  79.                 cols++;
  80.                 counter++;
  81.             }
  82.             CheckBest(counter, searchingString);
  83.             //diagonalUP
  84.             counter = 0;
  85.             rows = startingRows;
  86.             cols = startingCols;
  87.             while (rows >= 0 && cols + 1 <= colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
  88.             {
  89.                 rows--;
  90.                 cols++;
  91.                 counter++;
  92.             }
  93.             CheckBest(counter, searchingString);
  94.         }
  95.  
  96.         private static void CheckBest(int counter, string element)
  97.         {
  98.             if (counter>=bestSequence)
  99.             {
  100.                 bestSequence = counter;
  101.                 bestElement = element;
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement