Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Scrooge_McDuck
  5. {
  6.     class Program
  7.     {
  8.  
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             int[] firstNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  13.             int N = firstNumbers[0];
  14.             int M = firstNumbers[1];
  15.  
  16.             int[,] matrix = new int[N + 2, M + 2];
  17.             //  ----------->
  18.             // |           x N      
  19.             // |
  20.             // \  z M
  21.  
  22.             //вписване тип
  23.             //
  24.             // 0 0 0 0 0 0
  25.             // 0 3 3 3 3 0
  26.             // 0 3 ж 3 3 0
  27.             // 0 3 3 3 3 0
  28.             // 0 0 0 0 0 0
  29.  
  30.             for (int z = 1; z <= N; z++)
  31.             {
  32.                 int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  33.                 for (int x = 1; x <= M ; x++)
  34.                 {
  35.                     matrix[z, x] = arr[x - 1];
  36.                 }
  37.             }
  38.  
  39.             //определяне кординатитена 0-лата в матрицата с вкючена рамка от 0-ли.
  40.  
  41.            
  42.             int cordZ = 0;
  43.             int cordX = 0;
  44.  
  45.             for (int z = 1; z <= N; z++)
  46.             {
  47.                 for (int x = 1; x <= M ; x++)
  48.                 {
  49.                     if (matrix[z, x] == 0)
  50.                     {
  51.                         cordZ = z;
  52.                         cordX = x;
  53.                         break;
  54.                     }
  55.                 }
  56.             }
  57.  
  58.             int coins = 0;
  59.             while (true)
  60.             {
  61.                 //кординати
  62.                 int Left = matrix[cordZ, cordX - 1];
  63.                 int Right = matrix[cordZ, cordX + 1];
  64.                 int Down = matrix[cordZ + 1, cordX];
  65.                 int Up = matrix[cordZ - 1, cordX];
  66.  
  67.                 if (Left == 0 && Right == 0 && Up == 0 && Down == 0)
  68.                 {
  69.                     break;
  70.                 }
  71.                 else
  72.                 {
  73.                     int nextPosition = Math.Max(Up, Math.Max(Down, Math.Max(Left, Right)));
  74.  
  75.  
  76.                     if (nextPosition == Left)
  77.                     {
  78.                         cordX--;
  79.                     }
  80.                     else if (nextPosition == Right)
  81.                     {
  82.                         cordX++;
  83.                     }
  84.                     else if (nextPosition == Up)
  85.                     {
  86.                         cordZ--;
  87.                     }
  88.                     else if (nextPosition == Down)
  89.                     {
  90.                         cordZ++;
  91.                     }
  92.                     matrix[cordZ, cordX]--;
  93.                     coins++;
  94.                 }
  95.             }
  96.             Console.WriteLine(coins);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement