Advertisement
Spaskich

Untitled

Oct 11th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 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 RadioactiveBunnies
  9. {
  10.     static void Main()
  11.     {
  12.         int[] dimensions = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  13.         int n = dimensions[0];
  14.         int m = dimensions[1];
  15.         char[,] board = new char[n, m];
  16.  
  17.         bool isDead = false;
  18.         bool shouldContinue = true;
  19.         int[] endIndex = new int[2];
  20.         int playerRow = 0, playerCol = 0;
  21.  
  22.         for (int i = 0; i < n; i++)
  23.         {
  24.             char[] cells = Console.ReadLine().ToCharArray();
  25.             for (int j = 0; j < m; j++)
  26.             {
  27.                 board[i, j] = cells[j];
  28.                 if (cells[j] == 'P')
  29.                 {
  30.                     playerRow = i;
  31.                     playerCol = j;
  32.                 }
  33.             }
  34.         }
  35.         char[] directions = Console.ReadLine().ToCharArray();
  36.         foreach (var direction in directions)
  37.         {
  38.             while (shouldContinue)
  39.             {
  40.                 switch (direction)
  41.                 {
  42.                     case 'U':
  43.                         if (playerRow - 1 == -1)
  44.                         {
  45.                             Console.WriteLine("won {0} {1}", playerRow, playerCol);
  46.                             shouldContinue = false;
  47.                             break;
  48.                         }
  49.                         playerRow = Math.Max(0, playerRow - 1);
  50.                         PrintBoard(board);
  51.  
  52.                         if (board[playerRow, playerCol] == 'B')
  53.                         {
  54.                             isDead = true;
  55.                             shouldContinue = false;
  56.                         }
  57.                         break;
  58.                     case 'D':
  59.                         if (playerRow + 1 == n)
  60.                         {
  61.                             Console.WriteLine("won {0} {1}", playerRow, playerCol);
  62.                             shouldContinue = false;
  63.                             break;
  64.                         }
  65.                         playerRow = Math.Min(n - 1, playerRow + 1);
  66.                         if (playerRow == n)
  67.                         {
  68.                             Console.WriteLine("won {0} {1}", playerRow, playerCol);
  69.                         }
  70.                         if (board[playerRow, playerCol] == 'B')
  71.                         {
  72.                             isDead = true;
  73.                             shouldContinue = false;
  74.                         }
  75.                         break;
  76.                     case 'L':
  77.                         if (playerCol - 1 == -1)
  78.                         {
  79.                             Console.WriteLine("won {0} {1}", playerRow, playerCol);
  80.                             shouldContinue = false;
  81.                             break;
  82.                         }
  83.                         playerCol = Math.Max(0, playerCol - 1);
  84.                        
  85.                         if (board[playerRow, playerCol] == 'B')
  86.                         {
  87.                             isDead = true;
  88.                             shouldContinue = false;
  89.                         }
  90.                         break;
  91.                     case 'R':
  92.                         if (playerCol + 1 == m)
  93.                         {
  94.                             Console.WriteLine("won {0} {1}", playerRow, playerCol);
  95.                             shouldContinue = false;
  96.                             break;
  97.                         }
  98.                         playerCol = Math.Min(m - 1, playerCol + 1);
  99.                         if (board[playerRow, playerCol] == 'B')
  100.                         {
  101.                             isDead = true;
  102.                             shouldContinue = false;
  103.                         }
  104.                         break;
  105.                 }
  106.  
  107.                 for (int i = 0; i < board.GetLength(0); i++)
  108.                 {
  109.                     for (int j = 0; j < board.GetLength(1); j++)
  110.                     {
  111.                         if (board[i, j] == 'B')
  112.                         {
  113.                             int startRow = Math.Max(0, i - 1);
  114.                             int startCol = Math.Max(0, j - 1);
  115.                             int endRow = Math.Min(board.GetLength(0) - 1, i + 1);
  116.                             int endCol = Math.Min(board.GetLength(1) - 1, j + 1);
  117.  
  118.                             for (int k = startRow; k <= endRow; k++)
  119.                             {
  120.                                 for (int l = startCol; l <= endCol; l++)
  121.                                 {
  122.                                     board[k, l] = 'B';
  123.                                 }
  124.                             }
  125.                         }
  126.                     }
  127.                 }
  128.  
  129.                 if (board[playerRow, playerCol] == 'B')
  130.                 {
  131.                     isDead = true;
  132.                     shouldContinue = false;
  133.                 }
  134.  
  135.                 if (isDead == true)
  136.                 {
  137.                     PrintBoard(board);
  138.                     Console.WriteLine("won: {0} {1}", playerRow, playerCol);
  139.                     break;
  140.                 }
  141.             }
  142.         }
  143.        
  144.  
  145.        
  146.     }
  147.  
  148.     private static void PrintBoard(char[,] board)
  149.     {
  150.         for (int i = 0; i < board.GetLength(0); i++)
  151.         {
  152.             for (int j = 0; j < board.GetLength(1); j++)
  153.             {
  154.                 Console.Write("{0} ", board[i, j]);
  155.             }
  156.             Console.WriteLine();
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement