Advertisement
Guest User

Matrix

a guest
Jan 8th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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 SpiralMatrix
  8. {
  9. class SpiralMatrix
  10. {
  11. static void Main(string[] args)
  12. {
  13. int sizeOfMatrix = int.Parse(Console.ReadLine());
  14. string keyword = Console.ReadLine();
  15.  
  16. char[,] matrix = new char[sizeOfMatrix, sizeOfMatrix];
  17. int counter = 0;
  18.  
  19. int lastCol = sizeOfMatrix;
  20. int startPosition1 = 1;
  21.  
  22. int lastRow = sizeOfMatrix;
  23. int startPosition2 = sizeOfMatrix - 2;
  24.  
  25. int firstCol = 1;
  26. int startPosition3 = sizeOfMatrix - 2;
  27.  
  28.  
  29. for (int row = 0; row < sizeOfMatrix; row++)
  30. {
  31. for (int col = 0; col < sizeOfMatrix; col++)
  32. {
  33. if (counter == keyword.Length)
  34. {
  35. counter = 0;
  36. }
  37. matrix[row, col] = keyword[counter];
  38. counter++;
  39.  
  40. if (col == lastCol - 1)
  41. {
  42. for (int i = startPosition1; i < lastCol; i++)
  43. {
  44. if (counter == keyword.Length)
  45. {
  46. counter = 0;
  47. }
  48. matrix[i, col] = keyword[counter];
  49. counter++;
  50.  
  51. if (i == lastRow - 1)
  52. {
  53. for (int j = startPosition2; j >= 0 ; j--)
  54. {
  55. if (counter == keyword.Length)
  56. {
  57. counter = 0;
  58. }
  59. matrix[i, j] = keyword[counter];
  60. counter++;
  61.  
  62. if (j == 0)
  63. {
  64. for (int k = startPosition3; k >= firstCol; k--)
  65. {
  66. if (counter == keyword.Length)
  67. {
  68. counter = 0;
  69. }
  70. matrix[k, j] = keyword[counter];
  71. counter++;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. lastCol--;
  78. startPosition1++;
  79. lastRow--;
  80. startPosition2++;
  81. firstCol--;
  82. startPosition3++;
  83. }
  84. }
  85. }
  86. Console.WriteLine();
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement