Advertisement
social1986

Untitled

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