Advertisement
MeliDragon

Untitled

May 8th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ScroogeMcDuck
  7. {
  8. internal class Program
  9. {
  10. static int[][] voltMaze;
  11. static void Main()
  12. {
  13. int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14. int row = input[0];
  15. voltMaze = new int[row][];
  16. int startRow = 0;
  17. int startCol = 0;
  18.  
  19. for (int r = 0; r < row; r++)
  20. {
  21. int[] collumns = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  22. if (collumns.Contains(0))
  23. {
  24. for (int i = 0; i < collumns.Length; i++)
  25. {
  26. if (collumns[i] == 0)
  27. {
  28. startRow = r;
  29. startCol = i;
  30. }
  31. }
  32. }
  33. voltMaze[r] = collumns;
  34. }
  35.  
  36. Console.WriteLine(GoTrueTheMaze(startRow, startCol));
  37. }
  38.  
  39. private static int GoTrueTheMaze(int row, int col)
  40. {
  41. DirectionToFollow left = GetTheCoins(row, col - 1);
  42. DirectionToFollow right = GetTheCoins(row, col + 1);
  43. DirectionToFollow up = GetTheCoins(row - 1, col);
  44. DirectionToFollow down = GetTheCoins(row + 1, col);
  45.  
  46. DirectionToFollow current = left;
  47. if (current.Coins < right.Coins)
  48. {
  49. current = right;
  50. }
  51. if (current.Coins < up.Coins)
  52. {
  53. current = up;
  54. }
  55. if (current.Coins < down.Coins)
  56. {
  57. current = down;
  58. }
  59.  
  60. if (current.Coins == 0)
  61. {
  62. return 0;
  63. }
  64.  
  65. voltMaze[row][col]--;
  66.  
  67. return 1 + GoTrueTheMaze(current.Row, current.Col);
  68.  
  69. }
  70.  
  71. private static DirectionToFollow GetTheCoins(int row, int col)
  72. {
  73. if (IsInMaze(row, col))
  74. {
  75. return new DirectionToFollow(row, col, voltMaze[row][col]);
  76. }
  77. return new DirectionToFollow(row, col);
  78. }
  79.  
  80. private static bool IsInMaze(int row, int col)
  81. {
  82. return row >= 0
  83. && col >= 0
  84. && row < voltMaze.Length
  85. && col < voltMaze[0].Length;
  86. }
  87. }
  88.  
  89. public class DirectionToFollow
  90. {
  91. private const int noCoins = 0;
  92. public DirectionToFollow(int row, int col, int coins)
  93. {
  94. Row = row;
  95. Col = col;
  96. Coins = coins;
  97. }
  98.  
  99. public DirectionToFollow(int row, int col)
  100. : this(row, col, noCoins)
  101. {}
  102.  
  103. public int Row { get; set; }
  104. public int Col { get; set; }
  105. public int Coins { get; set; }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement