Advertisement
AlexVanchov

Matrix wordn spiral

Mar 25th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace App1
  4. {
  5. class Program
  6. {
  7. public const int LEFT_RIGHT = 1;
  8. public const int UP_DOWN = 2;
  9. public const int RIGHT_LEFT = 3;
  10. public const int DOWN_UP = 4;
  11.  
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine("M");
  15. int m = int.Parse(Console.ReadLine());
  16. Console.WriteLine("N");
  17. int n = int.Parse(Console.ReadLine());
  18.  
  19. char[,] matrix = new char[n, m];
  20.  
  21. for (int i = 0; i < n; i++)
  22. {
  23. Console.WriteLine($"eow");
  24. char[] row = Console.ReadLine().ToCharArray();
  25. for (int j = 0; j < m; j++) matrix[i, j] = row[j];
  26. }
  27.  
  28. log(matrix, n, m);
  29.  
  30. }
  31.  
  32. public static void log(char[,] matrix, int n, int m)
  33. {
  34. if (n == 1 && m == 1)
  35. {
  36. Console.WriteLine(matrix[0, 0]);
  37. return;
  38. }
  39.  
  40. bool[,] sm = new bool[n, m];
  41. int countx = 0; int county = 0;
  42. int position = LEFT_RIGHT;
  43. string result = "";
  44. while (!sm[county, countx])
  45. {
  46. switch (position)
  47. {
  48. case LEFT_RIGHT:
  49. {
  50. for (int i = 0; i < m; i++)
  51. {
  52. sm[county, countx] = true;
  53. result += matrix[county, countx];
  54. countx++;
  55. }
  56. countx--; county++; n--; m--;
  57. break;
  58. }
  59. case UP_DOWN:
  60. {
  61. for (int i = 0; i < n; i++)
  62. {
  63. sm[county, countx] = true;
  64. result += matrix[county, countx];
  65. county++;
  66. }
  67. county--; countx--;
  68. break;
  69. }
  70. case RIGHT_LEFT:
  71. {
  72. for (int i = 0; i < m; i++)
  73. {
  74. sm[county, countx] = true;
  75. result += matrix[county, countx];
  76. countx--;
  77. }
  78. countx++; county--; n--; m--;
  79. break;
  80. }
  81. case DOWN_UP:
  82. {
  83. for (int i = 0; i < n; i++)
  84. {
  85. sm[county, countx] = true;
  86. result += matrix[county, countx];
  87. county--;
  88. }
  89. county++; countx++;
  90. break;
  91. }
  92. }
  93.  
  94. if (++position > DOWN_UP) position = LEFT_RIGHT;
  95. }
  96.  
  97. Console.WriteLine(result);
  98. }
  99. }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement