Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. function Main()
  2. {
  3.     Dictionary<string, int> static result = new Dictionary<string, int>()
  4.     {
  5.         { "L", 0 },
  6.         { "P", 0 },
  7.         { "C", 0 },
  8.         { "harmed", 0 }
  9.     };
  10.  
  11.     int rowsCount = int.Parse(Console.ReadLine);
  12.     sting[][] static matrix = new string[rowsCount][];
  13.  
  14.     for (int i = 0; i < matrix.Length; i++)
  15.     {
  16.         matrix[i] = Console.ReadLine().Split(' ').ToArray();
  17.     }
  18.  
  19.     string const endCommand = "End";
  20.  
  21.     while (true)
  22.     {
  23.         string[] nextCommand = Console.ReadLine().Split();
  24.  
  25.         if (nextCommand[0] == endCommand)
  26.         {
  27.             // print
  28.             for (int i = 0; i < matrix.Length; i++)
  29.             {
  30.                 Console.WriteLine(string.Join(" ", matrix[i]));
  31.             }
  32.            
  33.             Console.WriteLine($"Carrots: {result['C']}");
  34.  
  35.             Console.WriteLine($"Potatos: {result['P']}");
  36.  
  37.             Console.WriteLine($"Lettuce: {result['C']}");
  38.  
  39.             Console.WriteLine($"Harmed vegetables: {result['harmed']}");
  40.  
  41.             break;
  42.         }
  43.  
  44.         int row = int.Parse(nextCommand[1]);
  45.         int col = int.Parse(nextCommand[2]);
  46.  
  47.         if (!InBorders(row, col)) continue;
  48.  
  49.         if (nextCommand[0] == "Harvest")
  50.         {
  51.             string el = matrix[row][col];
  52.  
  53.             if (el != ' ')
  54.             {
  55.                 result[el]++;
  56.                 matrix[row][col] = ' ';
  57.             }
  58.         }
  59.         else if (nextCommand[0] == "Mole")
  60.         {
  61.             Eat(r, c);
  62.  
  63.             string direction = nextCommand[3];
  64.             switch(direction)
  65.             {
  66.                 case "Up": Up(row, col); break;
  67.                 case "Down": Down(row, col); break;
  68.                 case "Left": Left(row, col); break;
  69.                 case "Right": Right(row, col); break;
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. function InBorders(int r, int c)
  76. {
  77.     return r >= 0 && r < matrix.Length &&
  78.             c >= 0 && c < matrix[r].Length;
  79. }
  80.  
  81. function Up(int r, int c) {
  82.     while(r >= 0) {
  83.         r-=2;
  84.  
  85.         if (InBorders(r, c))
  86.         {
  87.             Eat(r, c);
  88.         }
  89.     }
  90. }
  91.  
  92. function Down(int r, int c) {
  93.     while(r < matrix.Length) {
  94.         r+=2;
  95.  
  96.         if (InBorders(r, c))
  97.         {
  98.             Eat(r, c);
  99.         }
  100.     }
  101. }
  102.  
  103. function Left(int r, int c) {
  104.     while(c >= 0) {
  105.         c-=2;
  106.  
  107.         Eat(r, c);
  108.     }
  109. }
  110.  
  111. function Right(int r, int c) {
  112.     while(c < matrix[r].Length) {
  113.         c+=2;
  114.  
  115.         Eat(r, c);
  116.     }
  117. }
  118.  
  119. function Eat(int r, int c) {
  120.     string el = matrix[row][col];
  121.  
  122.     if (el != ' ')
  123.     {
  124.         result["harmed"]++;
  125.         matrix[row][col] = ' ';
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement