Advertisement
TeMePyT

Untitled

Jan 12th, 2019
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net;
  6.  
  7. namespace Miner
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             string[] directions = Console.ReadLine().Split(' ').ToArray();
  15.             int row = 0;
  16.             int col = 0;
  17.             string[][] field = new string[n][];
  18.  
  19.             for (int i = 0; i < n; i++)
  20.             {
  21.  
  22.                 field[i] = Console.ReadLine().Split(' ').ToArray();
  23.                 if (field[i].Contains("s"))
  24.                 {
  25.                     row = i;
  26.                     col = Array.IndexOf(field[i], "s");
  27.                 }
  28.             }
  29.             HashSet<Tuple<int, int>> coalLocations = new HashSet<Tuple<int, int>>();
  30.             GetLocations(coalLocations, field);
  31.             Tuple<int, int> eLocation = GetLocationOfe(field);
  32.  
  33.             for (int i = 0; i < directions.Length; i++)
  34.             {
  35.                 string direction = directions[i];
  36.                 switch (direction)
  37.                 {
  38.                     case "left":
  39.                         if (col > 0)
  40.                         {
  41.                             col--;
  42.                         }
  43.                         break;
  44.                     case "right":
  45.                         if (col + 1 < field[row].Length)
  46.                         {
  47.                             col++;
  48.                         }
  49.                         break;
  50.                     case "up":
  51.                         if (row > 0)
  52.                         {
  53.                             row--;
  54.                         }
  55.                         break;
  56.                     case "down":
  57.                         if (row + 1 < field.Length)
  58.                         {
  59.                             row++;
  60.                         }
  61.                         break;
  62.                 }
  63.                 if (eLocation.Equals(new Tuple<int, int>(row, col)))
  64.                 {
  65.                     Console.WriteLine($"Game over! ({row}, {col})");
  66.                     return;
  67.                 }
  68.                 if (coalLocations.Contains(new Tuple<int, int>(row, col)))
  69.                 {
  70.                     coalLocations.Remove(new Tuple<int, int>(row, col));
  71.                     if (coalLocations.Count == 0)
  72.                     {
  73.                         Console.WriteLine($"You collected all coals! ({row}, {col})");
  74.                         return;
  75.                     }
  76.                 }
  77.  
  78.             }
  79.             Console.WriteLine($"{coalLocations.Count} coals left. ({row}, {col})");
  80.         }
  81.  
  82.         private static Tuple<int, int> GetLocationOfe(string[][] field)
  83.         {
  84.             int row = 0;
  85.             int col = 0;
  86.             for (int i = 0; i < field.Length; i++)
  87.             {
  88.                 for (int j = 0; j < field[i].Length; j++)
  89.                 {
  90.                     if (field[i][j] == "e")
  91.                     {
  92.                         row = i;
  93.                         col = j;
  94.                     }
  95.                 }
  96.             }
  97.             return new Tuple<int, int>(row, col);
  98.         }
  99.  
  100.  
  101.         private static HashSet<Tuple<int, int>> GetLocations(HashSet<Tuple<int, int>> coalLocations, string[][] field)
  102.         {
  103.             for (int i = 0; i < field.Length; i++)
  104.             {
  105.                 for (int j = 0; j < field[i].Length; j++)
  106.                 {
  107.                     if (field[i][j] == "c")
  108.                     {
  109.                         coalLocations.Add(new Tuple<int, int>(i, j));
  110.                     }
  111.                 }
  112.             }
  113.  
  114.             return coalLocations;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement