stefan1919

Spiral Algorithm

Aug 28th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int size = int.Parse(Console.ReadLine());
  14.             string word = Console.ReadLine();
  15.             char[,] charMatrix = new char[size, size];
  16.            
  17.             //matrix
  18.             int x = 0;
  19.             int y = 0;
  20.            
  21.             int currentChar = 0;
  22.             while (size > 0)
  23.             {
  24.                 for (int i = y; i <= y + size - 1; i++)
  25.                 {
  26.                     charMatrix[x, i] = word[currentChar % word.Length];
  27.                     currentChar++;
  28.                 }
  29.                 for (int j = x + 1; j <= x + size - 1; j++)
  30.                 {
  31.                     charMatrix[j, y + size - 1] = word[currentChar % word.Length];
  32.                     currentChar++;
  33.                 }
  34.                 for (int i = y + size - 2; i >= y; i--)
  35.                 {
  36.                     charMatrix[x + size - 1, i] = word [currentChar % word.Length];
  37.                     currentChar++;
  38.                 }
  39.                 for (int i = x + size - 2; i >= x + 1; i--)
  40.                 {
  41.                     charMatrix[i, y] = word [currentChar % word.Length];
  42.                     currentChar++;
  43.                 }
  44.                 x = x + 1;
  45.                 y = y + 1;
  46.                 size = size - 2;
  47.             }
  48.             for (int i = 0; i < charMatrix.GetLength(0); i++)
  49.             {
  50.                 for (int j = 0; j < charMatrix.GetLength(1); j++)
  51.                 {
  52.                     Console.Write(charMatrix[i,j] + " ");
  53.                 }
  54.                 Console.WriteLine();
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment