Advertisement
Guest User

Untitled

a guest
Sep 27th, 2019
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _10._Radioactive_Mutant_V._Bunnies
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. int[] matrixSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11. char[,] lair = new char[matrixSize[0], matrixSize[1]];
  12. int[] playerStartCoord = new int[2];
  13. for (int i = 0; i < matrixSize[0]; i++)
  14. {
  15. string row = Console.ReadLine();
  16. for (int j = 0; j < matrixSize[1]; j++)
  17. {
  18. if (row[j] == 'P')
  19. {
  20. playerStartCoord[0] = i;
  21. playerStartCoord[1] = j;
  22. lair[i, j] = '.';
  23. }
  24. else lair[i, j] = row[j];
  25. }
  26. }
  27. string commands = Console.ReadLine();
  28. int currentXpos = playerStartCoord[1];
  29. int currentYpos = playerStartCoord[0];
  30. bool dead = false;
  31. foreach (var command in commands)
  32. {
  33. switch (command)
  34. {
  35. case 'U':
  36. currentYpos--;
  37. break;
  38. case 'D':
  39. currentYpos++;
  40. break;
  41. case 'L':
  42. currentXpos--;
  43. break;
  44. case 'R':
  45. currentXpos++;
  46. break;
  47. }
  48. if (CheckForEscaping(currentXpos, currentYpos, lair))
  49. {
  50. if (currentXpos < 0) currentXpos = 0;
  51. if (currentXpos == lair.GetLength(1)) currentXpos --;
  52. if (currentYpos < 0) currentYpos = 0;
  53. if (currentYpos == lair.GetLength(0)) currentYpos--;
  54. BunniesSpread(lair);
  55. break;
  56. }
  57. BunniesSpread(lair);
  58. if (CheckForBunniesReachPlayer(currentXpos,currentYpos, lair))
  59. {
  60. dead = true;
  61. break;
  62. }
  63. }
  64. PrintMatrix(lair);
  65. if (dead) Console.WriteLine($"dead: {currentYpos} {currentXpos}");
  66. else Console.WriteLine($"won: {currentYpos} {currentXpos}");
  67. }
  68.  
  69. static bool CheckForEscaping(int Xpos, int Ypos, char [,] matrix)
  70. {
  71. if (Xpos < 0 || Xpos >= matrix.GetLength(1) || Ypos < 0 || Ypos >= matrix.GetLength(0)) return true;
  72. return false;
  73. }
  74.  
  75. static void BunniesSpread(char[,] matrix)
  76. {
  77. for (int i = 0; i < matrix.GetLength(0); i++)
  78. {
  79. for (int j = 0; j < matrix.GetLength(1); j++)
  80. {
  81. if (matrix[i, j] == 'B')
  82. {
  83. if (!CheckForEscaping(j - 1, i, matrix) && matrix[i,j-1] == '.') matrix[i, j - 1] = 'N';
  84. if (!CheckForEscaping(j + 1, i, matrix) && matrix[i, j + 1] == '.') matrix[i, j + 1] = 'N';
  85. if (!CheckForEscaping(j, i + 1, matrix) && matrix[i+1, j] == '.') matrix[i + 1, j] = 'N';
  86. if (!CheckForEscaping(j, i - 1, matrix) && matrix[i-1, j] == '.') matrix[i - 1, j] = 'N';
  87. }
  88. }
  89. }
  90. for (int i = 0; i < matrix.GetLength(0); i++)
  91. {
  92. for (int j = 0; j < matrix.GetLength(1); j++)
  93. {
  94. if (matrix[i, j] == 'N') matrix[i, j] = 'B';
  95. }
  96. }
  97. }
  98.  
  99. static bool CheckForBunniesReachPlayer(int Xpos, int Ypos, char[,] matrix)
  100. {
  101. if (matrix[Ypos, Xpos] == 'B') return true;
  102. else return false;
  103. }
  104.  
  105. static void PrintMatrix(char[,] matrix)
  106. {
  107. for (int row = 0; row < matrix.GetLength(0); row++)
  108. {
  109. for (int col = 0; col < matrix.GetLength(1); col++)
  110. {
  111. Console.Write(matrix[row, col]);
  112. }
  113. Console.WriteLine();
  114. }
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement