Advertisement
Danny_Berova

02.Sneaking

Feb 11th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.24 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02.Problem
  5. {
  6.     public class ProblemTwo
  7.     {
  8.         public static void Main()
  9.         {
  10.             var n = int.Parse(Console.ReadLine());
  11.             char[][] matrix = new char[n][];
  12.             var rowOfSam = 0;
  13.             var colOfSam = 0;
  14.             var rowOfNikoladze = 0;
  15.             var colOfNikoladze = 0;
  16.             bool isSamDead = false;
  17.             bool isNikoladzeDead = false;
  18.  
  19.             for (int rowMtr = 0; rowMtr < n; rowMtr++)
  20.             {
  21.                 var currentInput = Console.ReadLine().ToCharArray();
  22.                 //matrix[rowMtr] = new char[currentInput.Length];
  23.                 matrix[rowMtr] = currentInput;
  24.                 for (int colMtr = 0; colMtr < matrix[rowMtr].Length; colMtr++)
  25.                 {
  26.                     if (matrix[rowMtr][colMtr] == 'S')
  27.                     {
  28.                         rowOfSam = rowMtr;
  29.                         colOfSam = colMtr;
  30.                     }
  31.                     if (matrix[rowMtr][colMtr] == 'N')
  32.                     {
  33.                         rowOfNikoladze = rowMtr;
  34.                         colOfNikoladze = colMtr;
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             var commands = Console.ReadLine().ToCharArray();
  40.             var positionOfSam = new int[] { rowOfSam, colOfSam };
  41.  
  42.  
  43.             while (!isNikoladzeDead && !isSamDead)
  44.             {
  45.                 for (int i = 0; i < commands.Length; i++)
  46.                 {
  47.                     EnemiesMove(matrix);
  48.  
  49.                     var command = commands[i];
  50.                     var current = matrix[positionOfSam[0]].ToArray();
  51.  
  52.                     if (current.Contains('b'))
  53.                     {
  54.                         var currentRow = string.Join("", matrix[positionOfSam[0]].ToArray());
  55.                         if (currentRow.IndexOf('b') < currentRow.IndexOf('S'))
  56.                         {
  57.                             isSamDead = true;
  58.                             matrix[positionOfSam[0]][positionOfSam[1]] = 'X';
  59.                             break;
  60.                         }
  61.                     }
  62.                     if (current.Contains('d'))
  63.                     {
  64.                         var currentRow = string.Join("", matrix[positionOfSam[0]].ToArray());
  65.                         if (currentRow.IndexOf('d') > currentRow.IndexOf('S'))
  66.                         {
  67.                             isSamDead = true;
  68.                             matrix[positionOfSam[0]][positionOfSam[1]] = 'X';
  69.                             break;
  70.                         }
  71.                     }
  72.                     positionOfSam = SamMoves(matrix, positionOfSam, command);
  73.  
  74.  
  75.  
  76.                     if (positionOfSam[0] == rowOfNikoladze)
  77.                     {
  78.                         isNikoladzeDead = true;
  79.                         matrix[rowOfNikoladze][colOfNikoladze] = 'X';
  80.                         break;
  81.                     }
  82.  
  83.                     if (isSamDead)
  84.                     {
  85.                         break;
  86.                     }
  87.  
  88.  
  89.                 }
  90.             }
  91.  
  92.             if (isSamDead)
  93.             {
  94.                 Console.WriteLine($"Sam died at {positionOfSam[0]}, {positionOfSam[1]}");
  95.             }
  96.             if (isNikoladzeDead)
  97.             {
  98.                 Console.WriteLine("Nikoladze killed!");
  99.             }
  100.  
  101.             PrintMatrix(matrix);
  102.  
  103.  
  104.         }
  105.  
  106.         private static void EnemiesMove(char[][] matrix)
  107.         {
  108.             for (int i = 0; i < matrix.Length; i++)
  109.             {
  110.                 var currentRowToStr =string.Join("", matrix[i].ToArray());
  111.                 var currentRow = matrix[i].ToArray();
  112.  
  113.  
  114.                 if (currentRowToStr.Contains('b'))
  115.                 {
  116.                     var bIndex = currentRowToStr.IndexOf('b');
  117.                     if (bIndex < currentRowToStr.Length - 1)
  118.                     {
  119.                         matrix[i][bIndex + 1] = 'b';
  120.                         matrix[i][bIndex] = '.';
  121.  
  122.                     }
  123.                     if (bIndex == currentRowToStr.Length - 1)
  124.                     {
  125.                         matrix[i][bIndex] = 'd';
  126.                     }
  127.  
  128.                 }
  129.                 if (currentRowToStr.Contains('d'))
  130.                 {
  131.                     var dIndex = currentRowToStr.IndexOf('d');
  132.                     if (dIndex >= 1)
  133.                     {
  134.                         matrix[i][dIndex - 1] = 'd';
  135.                         matrix[i][dIndex] = '.';
  136.  
  137.  
  138.                     }
  139.                     if (dIndex == 0)
  140.                     {
  141.                         matrix[i][dIndex] = 'b';
  142.  
  143.  
  144.                     }
  145.  
  146.                 }
  147.             }
  148.         }
  149.  
  150.         private static int[] SamMoves(char[][] matrix, int[] position, char command)
  151.         {
  152.             var rowOfSam = position[0];
  153.             var colOfSam = position[1];
  154.             switch (command)
  155.             {
  156.                 case 'U':
  157.                     matrix[rowOfSam][colOfSam] = '.';
  158.                     matrix[rowOfSam - 1][colOfSam] = 'S';
  159.                     rowOfSam--;
  160.                     break;
  161.                 case 'D':
  162.                     matrix[rowOfSam][colOfSam] = '.';
  163.                     matrix[rowOfSam + 1][colOfSam] = 'S';
  164.                     rowOfSam++;
  165.                     break;
  166.                 case 'L':
  167.                     matrix[rowOfSam][colOfSam] = '.';
  168.                     matrix[rowOfSam][colOfSam - 1] = 'S';
  169.                     colOfSam--;
  170.                     break;
  171.                 case 'R':
  172.                     matrix[rowOfSam][colOfSam] = '.';
  173.                     matrix[rowOfSam][colOfSam + 1] = 'S';
  174.                     colOfSam++;
  175.                     break;
  176.                 case 'W':
  177.                     break;
  178.             }
  179.  
  180.             position[0] = rowOfSam;
  181.             position[1] = colOfSam;
  182.             return position;
  183.         }
  184.  
  185.         private static void PrintMatrix(char[][] matrix)
  186.         {
  187.             for (int row = 0; row < matrix.Length; row++)
  188.             {
  189.                 for (int col = 0; col < matrix[row].Length; col++)
  190.                 {
  191.                     Console.Write(matrix[row][col]);
  192.                 }
  193.                 Console.WriteLine();
  194.             }
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement