Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P05.SnakeMoves
  6. {
  7.     class StartUp
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] dimensions = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  12.             int rows = dimensions[0];
  13.             int cols = dimensions[1];
  14.             var matrix = new char[rows, cols];
  15.             string input = Console.ReadLine();
  16.             var snakeQueue = new Queue<char>(input);
  17.             char oddSymbol = ' ';
  18.             char evenSymbol = ' ';
  19.             int counter = 0;
  20.             for (int row = 0; row < rows; row++)
  21.             {
  22.                 counter = 0;
  23.                 if (row % 2 == 1)
  24.                 {
  25.                     oddSymbol = ' ';
  26.                     if (snakeQueue.Any() && row > 0)
  27.                     {
  28.                         for (int col = cols - 1; col >= 0; col--)
  29.                         {
  30.                             oddSymbol = snakeQueue.Dequeue();
  31.                             matrix[row, col] = oddSymbol;
  32.                             counter++;
  33.                             if (!snakeQueue.Any())
  34.                             {
  35.                                 snakeQueue = new Queue<char>(input);
  36.                                 break;
  37.                             }
  38.                         }
  39.                     }
  40.                     for (int col = cols - 1 - counter; col >= 0; col--)
  41.                     {
  42.                         oddSymbol = snakeQueue.Dequeue();
  43.                         matrix[row, col] = oddSymbol;
  44.                     }
  45.                 }
  46.                 else
  47.                 {
  48.                     evenSymbol = ' ';
  49.                     if (snakeQueue.Any() && row > 0)
  50.                     {
  51.                         for (int col = 0; col < cols; col++)
  52.                         {
  53.                             evenSymbol = snakeQueue.Dequeue();
  54.                             matrix[row, col] = evenSymbol;
  55.                             counter++;
  56.                             if (!snakeQueue.Any())
  57.                             {
  58.                                 snakeQueue = new Queue<char>(input);
  59.                                 break;
  60.                             }
  61.                         }
  62.                     }
  63.                     for (int col = counter; col < cols; col++)
  64.                     {
  65.                         evenSymbol = snakeQueue.Dequeue();
  66.                         matrix[row, col] = evenSymbol;
  67.                     }
  68.                 }
  69.             }
  70.             PrintMatrix(rows, cols, matrix);
  71.         }
  72.  
  73.         private static void PrintMatrix(int rows, int cols, char[,] matrix)
  74.         {
  75.             for (int row = 0; row < rows; row++)
  76.             {
  77.                 for (int col = 0; col < cols; col++)
  78.                 {
  79.                     Console.Write(matrix[row, col]);
  80.                 }
  81.                 Console.WriteLine();
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement