Advertisement
vencinachev

SpiralTraverseMatrix

Dec 16th, 2020
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Spiral
  5. {
  6.     class Program
  7.     {
  8.         static bool IsFree(int[,] b, int i, int j)
  9.         {
  10.             int n = b.GetLength(0);
  11.             int m = b.GetLength(1);
  12.             if (i < 0 || i >= n || j < 0 || j >= m)
  13.             {
  14.                 return false;
  15.             }
  16.             else if (b[i, j] != 0)
  17.             {
  18.                 return false;
  19.             }
  20.             return true;
  21.         }
  22.         static List<int> SpiralTraverse(int[,] a)
  23.         {
  24.             List<int> nums = new List<int>();
  25.             int[,] b = new int[a.GetLength(0), a.GetLength(1)];
  26.    
  27.             int i = 0;
  28.             int j = 0;
  29.             int rounds = Math.Min(a.GetLength(0), a.GetLength(1)) / 2;
  30.             for (int k = 0; k <= rounds; k++)
  31.             {
  32.                 while(IsFree(b, i, j))
  33.                 {
  34.                     nums.Add(a[i, j]);
  35.                     b[i, j] = 1;
  36.                     j++;            // right
  37.                 }
  38.                 i++;
  39.                 j--;
  40.                 while (IsFree(b, i, j))
  41.                 {
  42.                     nums.Add(a[i, j]);
  43.                     b[i, j] = 1;
  44.                     i++;          // down
  45.                 }
  46.                 i--;
  47.                 j--;
  48.                 while (IsFree(b, i, j))
  49.                 {
  50.                     nums.Add(a[i, j]);
  51.                     b[i, j] = 1;
  52.                     j--;           // left
  53.                 }
  54.                 i--;
  55.                 j++;
  56.                 while (IsFree(b, i, j))
  57.                 {
  58.                     nums.Add(a[i, j]);
  59.                     b[i, j] = 1;
  60.                     i--;         // up
  61.                 }
  62.                 i++;
  63.                 j++;
  64.             }
  65.             return nums;
  66.         }
  67.         static void Main(string[] args)
  68.         {
  69.             int[,] matrix =   { { 1, 2, 3, 4, 5 },
  70.                                 { 6, 7, 8, 9, 10 },
  71.                                 { 11, 12, 13, 14, 15 },
  72.                                 { 16, 17, 18, 19, 20 },
  73.                                 { 21, 22, 23, 24, 25 }};
  74.  
  75.             List<int> spiral = SpiralTraverse(matrix);
  76.             foreach (var item in spiral)
  77.             {
  78.                 Console.Write($"{item} ");
  79.             }
  80.             Console.WriteLine();
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement