Advertisement
Guest User

Untitled

a guest
Oct 6th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LadyBugs
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string[] indices = Console.ReadLine().Split(' ');
  12. int rows = int.Parse(indices[0]);
  13. int cols = int.Parse(indices[1]);
  14.  
  15. int playerRow = 0;
  16. int playerCol = 0;
  17.  
  18. char[,] matrix = new char[rows, cols];
  19. for (int row = 0; row < rows; row++)
  20. {
  21. string line = Console.ReadLine();
  22. for (int col = 0; col < cols; col++)
  23. {
  24. matrix[row, col] = line[col];
  25. if (line[col] == 'P')
  26. {
  27. playerRow = row;
  28. playerCol = col;
  29. matrix[row, col] = '.';
  30. }
  31. }
  32. }
  33.  
  34. string commands = Console.ReadLine();
  35. for (int i = 0; i < commands.Length; i++)
  36. {
  37. string prev = playerRow + " " + playerCol;
  38. switch (commands[i])
  39. {
  40. case 'L':
  41. playerCol--; break;
  42. case 'U':
  43. playerRow--; break;
  44. case 'R':
  45. playerCol++; break;
  46. case 'D':
  47. playerRow++; break;
  48. }
  49.  
  50. char[,] updatedMatrix = (char[,]) matrix.Clone();
  51. for (int row = 0; row < matrix.GetLength(0); row++)
  52. {
  53. for (int col = 0; col < matrix.GetLength(1); col++)
  54. {
  55. if (matrix[row, col] == 'B')
  56. {
  57. if (isInside(row - 1, col, matrix))
  58. {
  59. updatedMatrix[row - 1, col] = 'B';
  60. }
  61. if (isInside(row + 1, col, matrix))
  62. {
  63. updatedMatrix[row + 1, col] = 'B';
  64. }
  65. if (isInside(row, col - 1, matrix))
  66. {
  67. updatedMatrix[row, col - 1] = 'B';
  68. }
  69. if (isInside(row - 1, col + 1, matrix))
  70. {
  71. updatedMatrix[row, col + 1] = 'B';
  72. }
  73. }
  74. }
  75. }
  76. matrix = updatedMatrix;
  77.  
  78. if (!isInside(playerRow, playerCol, matrix))
  79. {
  80. printMatrix(matrix);
  81. Console.WriteLine("won: " + prev);
  82. return;
  83. }
  84.  
  85. if (matrix[playerRow, playerCol] == 'B')
  86. {
  87. printMatrix(matrix);
  88. Console.WriteLine("dead: " + playerRow + " " + playerCol);
  89. return;
  90. }
  91. }
  92. }
  93.  
  94. private static void printMatrix(char[,] matrix)
  95. {
  96. for (int row = 0; row < matrix.GetLength(0); row++)
  97. {
  98. for (int col = 0; col < matrix.GetLength(1); col++)
  99. {
  100. Console.Write(matrix[row, col]);
  101. }
  102. Console.WriteLine();
  103. }
  104. }
  105.  
  106. private static bool isInside(int row, int col, char[,] matrix)
  107. {
  108. return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix.GetLength(1);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement