Advertisement
d_brezoev

MaxSubMatrix

Dec 15th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.MaxSubMatrix
  4. {
  5.     class MaxSubMatrix
  6.     {
  7.         static void PrintSubMatrix(ref int[,] matrix, int bestRow, int bestCol, int size)
  8.         {
  9.             Console.WriteLine("The best submatrix with size {0} is", size);
  10.             for (int i = 0; i < size; i++)
  11.             {
  12.                 for (int k = 0; k < size; k++)
  13.                 {
  14.                     Console.Write("{0,4}", matrix[bestRow + i, bestCol + k]);
  15.                 }
  16.                 Console.WriteLine();
  17.             }
  18.         }
  19.         static void PrintMatrix(int[,] matrix)
  20.         {
  21.             for (int row = 0; row < matrix.GetLength(0); row++)
  22.             {
  23.                 for (int col = 0; col < matrix.GetLength(1); col++)
  24.                 {
  25.                     Console.Write("{0,4}",matrix[row,col]);
  26.                 }
  27.                 Console.WriteLine();
  28.             }
  29.         }
  30.         static void Main(string[] args)
  31.         {
  32.             const int size = 3; // size of submatrix
  33.             Console.Write("Enter N = ");
  34.             int n = int.Parse(Console.ReadLine());
  35.             Console.Write("Enter M = ");
  36.             int m = int.Parse(Console.ReadLine());
  37.             if (n<size||m<size)
  38.             {
  39.                 Console.WriteLine("Wrong size of the matrix");
  40.                 return;
  41.             }
  42.             Console.WriteLine("The following random matrix is generated");
  43.             int[,] matrix = new int[n, m];
  44.             Random rand = new Random();
  45.             for (int row = 0; row < matrix.GetLength(0); row++)
  46.             {
  47.                 for (int col = 0; col < matrix.GetLength(1); col++)
  48.                 {
  49.                     matrix[row, col] = rand.Next(0, 21);
  50.                 }
  51.             }
  52.             PrintMatrix(matrix);
  53.             int maxSum = int.MinValue;
  54.             int currentSum = 0;
  55.             int startElementRow = 0;
  56.             int startElementCol = 0;
  57.             for (int row = 0; row < matrix.GetLength(0)-size+1; row++)
  58.             {
  59.                 for (int col = 0; col < matrix.GetLength(1)-size+1; col++)
  60.                 {
  61.                     for (int i = 0; i < size; i++)
  62.                     {
  63.                         for (int k = 0; k < size; k++)
  64.                         {
  65.                             currentSum += matrix[row + i, col + k];                                
  66.                         }
  67.                     }
  68.                     if (currentSum>maxSum)
  69.                     {
  70.                         maxSum = currentSum;
  71.                         startElementRow = row;
  72.                         startElementCol = col;
  73.                     }
  74.  
  75.                 }
  76.             }          
  77.             PrintSubMatrix(ref matrix, startElementRow, startElementCol, size);
  78.  
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement