Advertisement
Guest User

Hops

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class Program
  9. {
  10.     static void Main()
  11.     {
  12.         int[] carrots = Console.ReadLine()
  13.             .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
  14.             .Select(x => int.Parse(x)).ToArray();
  15.  
  16.         int n = int.Parse(Console.ReadLine());
  17.  
  18.         int max = int.MinValue;
  19.  
  20.         int fieldLength = carrots.Length;
  21.  
  22.         for (int i = 0; i < n; i++)
  23.         {
  24.            
  25.             int position = 0;
  26.  
  27.             HashSet<int> visitedPosition = new HashSet<int>();
  28.             visitedPosition.Add(0);
  29.             int sum = carrots[position];
  30.            
  31.  
  32.  
  33.             int[] directions = Console.ReadLine()
  34.             .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
  35.             .Select(x => int.Parse(x)).ToArray();
  36.  
  37.             int directionsCount = directions.Length;
  38.             int tries = 0;
  39.  
  40.             while (true)
  41.             {
  42.                 int choosenDirection = tries % directionsCount;
  43.  
  44.                 position += directions[choosenDirection];
  45.  
  46.                 if (position < 0 || position >= fieldLength || visitedPosition.Contains(position))
  47.                 {
  48.                     break;
  49.                 }
  50.                
  51.                 sum += carrots[position];
  52.                 visitedPosition.Add(position);
  53.                 tries++;
  54.  
  55.  
  56.             }
  57.  
  58.             if (max < sum)
  59.             {
  60.                 max = sum;
  61.             };
  62.         }
  63.  
  64.         Console.WriteLine(max);
  65.  
  66.  
  67.  
  68.  
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement