Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Matrixes8
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack<KeyValuePair<int,int>> player=new Stack<KeyValuePair<int, int>>();
- HashSet<KeyValuePair<int,int>>bunnies=new HashSet<KeyValuePair<int, int>>();
- KeyValuePair<int, int> position= new KeyValuePair<int, int>();
- KeyValuePair<int, int> newPosition=new KeyValuePair<int, int>();
- int[] nums = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int row = nums[0];
- int col = nums[1];
- char[,] matrix = new char[row, col];
- for (int rows = 0;rows < row; rows++)
- {
- char[] dungeonlairs = Console.ReadLine().ToCharArray();
- for (int cols= 0;cols <col; cols++)
- {
- matrix[rows, cols] = dungeonlairs[cols];
- if (dungeonlairs[cols] == 'B')
- {
- bunnies.Add(new KeyValuePair<int, int>(rows, cols));
- }
- else if (dungeonlairs[cols] == 'P')
- {
- player.Push(new KeyValuePair<int, int>(rows,cols));
- }
- }
- }
- char[] input = Console.ReadLine().ToCharArray();
- Queue<char>commands=new Queue<char>(input);
- // do tuk raboti perfektno pulni matricata i mestata masivite idealno ostava da napravim igrata :D
- bool dead = false;
- while (commands.Count>0)
- {
- char cmd = commands.Dequeue();
- switch (cmd)
- {
- case 'U':
- position = player.Peek();
- newPosition = new KeyValuePair<int, int>(position.Key - 1, position.Value);
- break;
- case 'D':
- position = player.Peek();
- newPosition = new KeyValuePair<int, int>(position.Key + 1, position.Value);
- break;
- case 'L':
- position = player.Peek();
- newPosition = new KeyValuePair<int, int>(position.Key, position.Value - 1);
- break;
- case 'R':
- position = player.Peek();
- newPosition = new KeyValuePair<int, int>(position.Key, position.Value + 1);
- break;
- default:
- Console.WriteLine("Not correct input");
- break;
- }
- if (cmd=='U' && newPosition.Key>row)
- {
- player.Push(position);
- }
- else if(cmd == 'U' && newPosition.Key <0)
- {
- player.Push(position);
- }
- else if (cmd == 'D' && newPosition.Key > row-1)
- {
- player.Push(position);
- }
- else if (cmd == 'L' && newPosition.Value< 0)
- {
- player.Push(position);
- }
- else if(cmd == 'R' && newPosition.Value >col-1)
- {
- player.Push(position);
- }
- else
- {
- player.Push(newPosition);
- matrix[position.Key,position.Value] = '.';
- }
- BunniesSpawn(matrix, bunnies, row, col);
- KeyValuePair<int, int> lastPosition = player.Peek();
- AddNewBunnies(bunnies, matrix);
- if (bunnies.Contains(lastPosition))
- {
- dead = true;
- break;
- }
- }
- PrintResult(player,matrix,dead);
- }
- public static void PrintResult(Stack<KeyValuePair<int, int>> player, char[,] matrix,bool dead)
- {
- KeyValuePair<int, int> last = player.Peek();
- if (dead)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- StringBuilder write = new StringBuilder();
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- write.Append(matrix[i, j]);
- }
- Console.WriteLine(write);
- }
- Console.WriteLine($"dead: {last.Key} {last.Value}");
- }
- else
- {
- for(int i=0;i<matrix.GetLength(0);i++)
- {
- StringBuilder write = new StringBuilder();
- for (int j = 0; j <matrix.GetLength(1); j++)
- {
- write.Append(matrix[i, j]);
- }
- Console.WriteLine(write);
- }
- Console.WriteLine($"won: {last.Key} {last.Value}");
- }
- }
- public static void BunniesSpawn(char[,] matrix, HashSet<KeyValuePair<int, int>> bunnies, int row, int col)
- {
- for (int i = 0; i < row; i++)
- {
- for (int k = 0; k < col; k++)
- {
- if (matrix[i, k] == 'B')
- {
- KeyValuePair<int, int> bunnyUp = new KeyValuePair<int, int>((i - 1), k);
- if (IsInsideMatrix(matrix,bunnyUp.Key,bunnyUp.Value))
- {
- bunnies.Add(bunnyUp);
- }
- KeyValuePair<int, int> bunnyDown = new KeyValuePair<int, int>((i + 1), k);
- if (IsInsideMatrix(matrix, bunnyDown.Key, bunnyDown.Value))
- {
- bunnies.Add(bunnyDown);
- }
- KeyValuePair<int, int> bunnyL = new KeyValuePair<int, int>(i, (k - 1));
- if (IsInsideMatrix(matrix, bunnyL.Key, bunnyL.Value))
- {
- bunnies.Add(bunnyL);
- }
- KeyValuePair<int, int> bunnyR = new KeyValuePair<int, int>(i, (k + 1));
- if (IsInsideMatrix(matrix, bunnyR.Key, bunnyR.Value))
- {
- bunnies.Add(bunnyR);
- }
- }
- }
- }
- }
- public static void AddNewBunnies(HashSet<KeyValuePair<int, int>> bunnies, char[,] matrix)
- {
- foreach (var bunny in bunnies)
- {
- matrix[bunny.Key, bunny.Value] = 'B';
- }
- }
- private static bool IsInsideMatrix(char[,] matrix, int r, int c)
- {
- return r >= 0 && r < matrix.GetLength(0) && c >= 0 && c < matrix.GetLength(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement