Advertisement
Aliendreamer

зайците 70/100

Jan 31st, 2018
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. namespace Matrixes8
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Stack<KeyValuePair<int,int>> player=new Stack<KeyValuePair<int, int>>();
  14.             HashSet<KeyValuePair<int,int>>bunnies=new HashSet<KeyValuePair<int, int>>();
  15.             KeyValuePair<int, int> position= new KeyValuePair<int, int>();
  16.             KeyValuePair<int, int> newPosition=new KeyValuePair<int, int>();
  17.             int[] nums = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.             int row = nums[0];
  19.             int col = nums[1];
  20.             char[,] matrix = new char[row, col];
  21.            
  22.             for (int rows = 0;rows < row; rows++)
  23.             {
  24.                 char[] dungeonlairs = Console.ReadLine().ToCharArray();
  25.  
  26.                 for (int cols= 0;cols <col; cols++)
  27.                 {
  28.                     matrix[rows, cols] = dungeonlairs[cols];
  29.  
  30.                     if (dungeonlairs[cols] == 'B')
  31.                     {
  32.  
  33.                         bunnies.Add(new KeyValuePair<int, int>(rows, cols));
  34.                     }
  35.                     else if (dungeonlairs[cols] == 'P')
  36.                     {
  37.                         player.Push(new KeyValuePair<int, int>(rows,cols));
  38.                     }
  39.                 }
  40.             }
  41.             char[] input = Console.ReadLine().ToCharArray();
  42.             Queue<char>commands=new Queue<char>(input);
  43.             // do tuk raboti perfektno pulni matricata i mestata masivite idealno ostava da napravim igrata :D
  44.             bool dead = false;
  45.             while (commands.Count>0)
  46.             {
  47.                 char cmd = commands.Dequeue();
  48.                 switch (cmd)
  49.                 {
  50.                     case 'U':
  51.                         position = player.Peek();
  52.                         newPosition = new KeyValuePair<int, int>(position.Key - 1, position.Value);
  53.                        
  54.                         break;
  55.                     case 'D':
  56.                         position = player.Peek();
  57.                         newPosition = new KeyValuePair<int, int>(position.Key + 1, position.Value);
  58.                        
  59.                         break;
  60.                     case 'L':
  61.                         position = player.Peek();
  62.                         newPosition = new KeyValuePair<int, int>(position.Key, position.Value - 1);
  63.                        
  64.                         break;
  65.  
  66.                     case 'R':
  67.                         position = player.Peek();
  68.                         newPosition = new KeyValuePair<int, int>(position.Key, position.Value + 1);
  69.                        
  70.                         break;
  71.                    default:
  72.                        Console.WriteLine("Not correct input");
  73.                    break;
  74.                 }
  75.  
  76.                 if (cmd=='U' && newPosition.Key>row)
  77.                 {
  78.                     player.Push(position);
  79.                 }
  80.                 else if(cmd == 'U' && newPosition.Key <0)
  81.                 {
  82.                   player.Push(position);
  83.                 }
  84.                 else if (cmd == 'D' && newPosition.Key > row-1)
  85.                 {
  86.                     player.Push(position);
  87.                 }
  88.                 else if (cmd == 'L' && newPosition.Value< 0)
  89.                 {
  90.                     player.Push(position);
  91.                 }
  92.                 else if(cmd == 'R' && newPosition.Value >col-1)
  93.                 {
  94.                     player.Push(position);
  95.                 }
  96.                 else
  97.                 {
  98.                     player.Push(newPosition);
  99.                     matrix[position.Key,position.Value] = '.';
  100.                 }
  101.  
  102.  
  103.                 BunniesSpawn(matrix, bunnies, row, col);
  104.                 KeyValuePair<int, int> lastPosition = player.Peek();
  105.                 AddNewBunnies(bunnies, matrix);
  106.                 if (bunnies.Contains(lastPosition))
  107.                 {
  108.                     dead = true;
  109.                     break;
  110.                 }
  111.                
  112.             }    
  113.             PrintResult(player,matrix,dead);
  114.  
  115.         }
  116.  
  117.         public static void PrintResult(Stack<KeyValuePair<int, int>> player, char[,] matrix,bool dead)
  118.         {
  119.             KeyValuePair<int, int> last = player.Peek();
  120.            
  121.             if (dead)
  122.             {              
  123.                 for (int i = 0; i < matrix.GetLength(0); i++)
  124.                 {
  125.                     StringBuilder write = new StringBuilder();
  126.                     for (int j = 0; j < matrix.GetLength(1); j++)
  127.                     {
  128.                         write.Append(matrix[i, j]);
  129.                     }
  130.  
  131.                 Console.WriteLine(write);
  132.                 }
  133.                 Console.WriteLine($"dead: {last.Key} {last.Value}");
  134.             }
  135.             else
  136.             {  
  137.                 for(int i=0;i<matrix.GetLength(0);i++)
  138.                 {
  139.                     StringBuilder write = new StringBuilder();
  140.                     for (int j = 0; j <matrix.GetLength(1); j++)
  141.                     {
  142.                         write.Append(matrix[i, j]);
  143.                     }
  144.                 Console.WriteLine(write);
  145.                 }
  146.                 Console.WriteLine($"won: {last.Key} {last.Value}");
  147.             }
  148.  
  149.  
  150.         }
  151.  
  152.         public static void BunniesSpawn(char[,] matrix, HashSet<KeyValuePair<int, int>> bunnies, int row, int col)
  153.         {
  154.             for (int i = 0; i < row; i++)
  155.             {
  156.                 for (int k = 0; k < col; k++)
  157.                 {
  158.                     if (matrix[i, k] == 'B')
  159.                     {
  160.  
  161.                         KeyValuePair<int, int> bunnyUp = new KeyValuePair<int, int>((i - 1), k);
  162.                         if (IsInsideMatrix(matrix,bunnyUp.Key,bunnyUp.Value))
  163.                         {
  164.                           bunnies.Add(bunnyUp);
  165.                         }
  166.  
  167.                         KeyValuePair<int, int> bunnyDown = new KeyValuePair<int, int>((i + 1), k);
  168.                         if (IsInsideMatrix(matrix, bunnyDown.Key, bunnyDown.Value))
  169.                         {
  170.                           bunnies.Add(bunnyDown);
  171.                         }
  172.  
  173.                         KeyValuePair<int, int> bunnyL = new KeyValuePair<int, int>(i, (k - 1));
  174.                         if (IsInsideMatrix(matrix, bunnyL.Key, bunnyL.Value))
  175.                         {
  176.                          bunnies.Add(bunnyL);
  177.                         }
  178.  
  179.                         KeyValuePair<int, int> bunnyR = new KeyValuePair<int, int>(i, (k + 1));
  180.                         if (IsInsideMatrix(matrix, bunnyR.Key, bunnyR.Value))
  181.                         {
  182.                           bunnies.Add(bunnyR);
  183.                         }
  184.                     }
  185.  
  186.  
  187.  
  188.                 }
  189.             }
  190.         }
  191.  
  192.         public static void AddNewBunnies(HashSet<KeyValuePair<int, int>> bunnies, char[,] matrix)
  193.         {
  194.             foreach (var bunny in bunnies)
  195.             {
  196.                 matrix[bunny.Key, bunny.Value] = 'B';
  197.             }
  198.         }
  199.         private static bool IsInsideMatrix(char[,] matrix, int r, int c)
  200.         {
  201.             return r >= 0 && r < matrix.GetLength(0) && c >= 0 && c < matrix.GetLength(1);
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement