10. Radioactive Mutant Vampire Bunnies - Multidimensional Arrays - Exercise
Здравейте,
На задача https://judge.softuni.bg/Contests/Practice/Index/1455#9 с решение https://pastebin.com/bCygjASy Judge ми дава 80/100. Ще се радвам ако някой може да обърне внимание на моя код (разглеждал съм и други решения, но се интерсувам от моята грешка). Благодаря предварително.
Hi Axiomatik,
Could you please try to find also my fault?
https://pastebin.com/p7Nm0dfk
80/100
Test #4 (Incorrect answer)
Test #8 (Incorrect answer)
Thank you very much!
BR,
Ivan
OK, I'll take a look at it today.
Two problems in your code:
1. MultiplyBunny has to be moved from line 45 to 162. In the assignment, it says that first the player moves, then the bunnies multiply.
2. After line 169 (when the bunnies have multiplied, there is no check whether they have eaten up the player).
I've included the changes and now it gives 100%
Best,
using System;
using System.Collections.Generic;
using System.Linq;
namespace bunniesMay
{
class Program
{
static void Main(string[] args)
{
int[] size = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
int rows = size[0];
int cols = size[1];
int playerRow = -1;
int playerCol = -1;
char[,] playGraund = new char[rows, cols];
for (int row = 0; row < rows; row++)
{
char[] input = Console.ReadLine().ToCharArray();
for (int col = 0; col < cols; col++)
{
playGraund[row, col] = input[col];
if (playGraund[row, col] == 'P')
{
playerRow = row;
playerCol = col;
}
}
}
char[] cmd = Console.ReadLine().ToArray();
Queue<int[]> bunniesIndexes = new Queue<int[]>();
for (int c = 0; c < cmd.Length; c++)
{
BunniesFinder(rows, cols, playGraund, bunniesIndexes);
bool won = false;
char currMove = cmd[c];
//MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
if (currMove == 'U')
{
if (playerRow - 1 >= 0)
{
if (playGraund[playerRow - 1, playerCol] == '.')
{
playGraund[playerRow, playerCol] = '.';
playGraund[playerRow - 1, playerCol] = 'P';
playerRow -= 1;
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"dead: {playerRow - 1} {playerCol}");
break;
}
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
won = true;
}
}
else if (currMove == 'D')
{
if (playerRow + 1 < rows)
{
if (playGraund[playerRow + 1, playerCol] == '.')
{
playGraund[playerRow, playerCol] = '.';
playGraund[playerRow + 1, playerCol] = 'P';
playerRow += 1;
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"dead: {playerRow + 1} {playerCol}");
break;
}
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
won = true;
}
}
else if (currMove == 'L')
{
if (playerCol - 1 >= 0)
{
if (playGraund[playerRow, playerCol - 1] == '.')
{
playGraund[playerRow, playerCol] = '.';
playGraund[playerRow, playerCol - 1] = 'P';
playerCol -= 1;
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"dead: {playerRow} {playerCol - 1}");
break;
}
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
won = true;
}
}
else if (currMove == 'R')
{
if (playerCol + 1 < cols)
{
if (playGraund[playerRow, playerCol + 1] == '.')
{
playGraund[playerRow, playerCol] = '.';
playGraund[playerRow, playerCol + 1] = 'P';
playerCol += 1;
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"dead: {playerRow} {playerCol + 1}");
break;
}
}
else
{
if (playGraund[playerRow, playerCol] != 'B')
{
playGraund[playerRow, playerCol] = '.';
}
won = true;
}
}
MultiplyBunny(rows, cols, playGraund, bunniesIndexes);
if (won)
{
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"won: {playerRow} {playerCol}");
break;
}
bool alivePlayer = CheckLive(playGraund);
if (alivePlayer == false)
{
PrintPlayground(rows, cols, playGraund);
Console.WriteLine($"dead: {playerRow} {playerCol}");
break;
}
}
}
static bool CheckLive(char[,] playGraund)
{
foreach (var items in playGraund)
{
if (items == 'P')
{
return true;
}
}
return false;
}
static void MultiplyBunny(int rows, int cols, char[,] playGraund, Queue<int[]> bunniesIndexes)
{
while (bunniesIndexes.Count != 0)
{
int[] currBunny = bunniesIndexes.Dequeue();
int row = currBunny[0];
int col = currBunny[1];
if (row - 1 >= 0)
{
playGraund[row - 1, col] = 'B';
}
if (row + 1 < rows)
{
playGraund[row + 1, col] = 'B';
}
if (col - 1 >= 0)
{
playGraund[row, col - 1] = 'B';
}
if (col + 1 < cols)
{
playGraund[row, col + 1] = 'B';
}
}
}
static void PrintPlayground(int rows, int cols, char[,] playGraund)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
Console.Write(playGraund[row, col]);
}
Console.WriteLine();
}
}
static void BunniesFinder(int rows, int cols, char[,] playGraund, Queue<int[]> bunniesIndexes)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (playGraund[row, col] == 'B')
{
bunniesIndexes.Enqueue(new int[] { row, col });
}
}
}
}
}
}
Hello, everyone.
This is not the right place for my question but because it is about Multidimensional Arrays, I would be happy if someone could solve my problem https://softuni.bg/forum/30851/problem-02-present-delivery-csharp-advanced-retake-exam-17-december-2019.
Thank you so much!
Hello to everybody and Happy New Year!
I love to share the inputs about this exercise: https://judge.softuni.bg/Contests/118/Advanced-CSharp-Exam-11-October-2015
And if somebody has time to spare and want to give me some feedback they are more than welcome:
my code with one Queue<List<int>>() and some explanations- https://pastebin.com/51CHGh6W (100/100)
Best regards!
Hi Elena,
Code is more than OK, thanks to MartinBG. The only thing that I can think of is to perhaps include a Bunny-class and use that instead of a List of bunny-coordinates, maybe it's also a good idea to include a default case in your switch since otherwise your program will bug down if you receive an unknown command ("Capuccino, ....").
All the best for 2021,
;-)
@Axiomatik
Thanks a lot!
I think with default in the switch and with class the code become better.
Happy New Year, I wish you all the best!
Thank you for all the help and feedback you gave me in tha last year. It really meant a lot to me!