Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ScroogeMcDuck
- {
- internal class Program
- {
- static int[][] voltMaze;
- static void Main()
- {
- int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- int row = input[0];
- voltMaze = new int[row][];
- int startRow = 0;
- int startCol = 0;
- for (int r = 0; r < row; r++)
- {
- int[] collumns = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- if (collumns.Contains(0))
- {
- for (int i = 0; i < collumns.Length; i++)
- {
- if (collumns[i] == 0)
- {
- startRow = r;
- startCol = i;
- }
- }
- }
- voltMaze[r] = collumns;
- }
- Console.WriteLine(GoTrueTheMaze(startRow, startCol));
- }
- private static int GoTrueTheMaze(int row, int col)
- {
- DirectionToFollow left = GetTheCoins(row, col - 1);
- DirectionToFollow right = GetTheCoins(row, col + 1);
- DirectionToFollow up = GetTheCoins(row - 1, col);
- DirectionToFollow down = GetTheCoins(row + 1, col);
- DirectionToFollow current = left;
- if (current.Coins < right.Coins)
- {
- current = right;
- }
- if (current.Coins < up.Coins)
- {
- current = up;
- }
- if (current.Coins < down.Coins)
- {
- current = down;
- }
- if (current.Coins == 0)
- {
- return 0;
- }
- voltMaze[row][col]--;
- return 1 + GoTrueTheMaze(current.Row, current.Col);
- }
- private static DirectionToFollow GetTheCoins(int row, int col)
- {
- if (IsInMaze(row, col))
- {
- return new DirectionToFollow(row, col, voltMaze[row][col]);
- }
- return new DirectionToFollow(row, col);
- }
- private static bool IsInMaze(int row, int col)
- {
- return row >= 0
- && col >= 0
- && row < voltMaze.Length
- && col < voltMaze[0].Length;
- }
- }
- public class DirectionToFollow
- {
- private const int noCoins = 0;
- public DirectionToFollow(int row, int col, int coins)
- {
- Row = row;
- Col = col;
- Coins = coins;
- }
- public DirectionToFollow(int row, int col)
- : this(row, col, noCoins)
- {}
- public int Row { get; set; }
- public int Col { get; set; }
- public int Coins { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement