Loading...
Milko123 avatar Milko123 14 Точки

10. Radioactive Mutant Vampire Bunnies - Multidimensional Arrays - Exercise

Здравейте,

На задача https://judge.softuni.bg/Contests/Practice/Index/1455#9 с решение https://pastebin.com/bCygjASy Judge ми дава 80/100. Ще се радвам ако някой може да обърне внимание на моя код (разглеждал съм и други решения, но се интерсувам от моята грешка). Благодаря предварително.

Тагове:
0
C# Advanced
Axiomatik avatar Axiomatik 2422 Точки

Line 32, 52, 72, 92

  • After index-validation, you only check if( field == '.'), but not whether the player steps unto a bunny and gets eliminated. There is no check for :

    if(matrix[rowCheck + 1, colCheck] == 'B')

    {playerDead == true}

0
ivan_enchev avatar ivan_enchev 0 Точки

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 

0
Axiomatik avatar Axiomatik 2422 Точки

OK, I'll take a look at it today.

0
Axiomatik avatar Axiomatik 2422 Точки

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 });
                    }
                }
            }
        }
    }
}
 

1
ivan_enchev avatar ivan_enchev 0 Точки

Thank you so much!

0
Elena123456 avatar Elena123456 235 Точки

Hello to everybody and Happy New Year! smiley

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!

 

 

1
Axiomatik avatar Axiomatik 2422 Точки

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,

;-)

 

using System;
using System.Linq;
using System.Collections.Generic;

namespace RadioactiveMutantVampireBunnies
{
    static class Program
    {
        static void Main(string[] args)
        {
            int[] input = Console.ReadLine()
                .Split()
                .Select(int.Parse)
                .ToArray();

            int rows = input[0];
            int cols = input[1];
            var matrix = new char[rows, cols];
            int rowStart = 0;
            int colStart = 0;
            var queueWithBunnies = new Queue<List<int>>();
            var queueWithBunniesObjects = new Queue<Bunny>();

            for (int row = 0; row < rows; row++)
            {
                string currentLine = Console.ReadLine();
                for (int col = 0; col < cols; col++)
                {
                    matrix[row, col] = currentLine[col];
                    if (matrix[row, col] == 'P')
                    {
                        rowStart = row;
                        colStart = col;
                    }
                }
            }

            string commandLine = Console.ReadLine();

            foreach (var command in commandLine)
            {
                int currentRow = rowStart;
                int currentCol = colStart;
                switch (command)
                {
                    case 'U':
                        currentRow--;
                        break;
                    case 'D':
                        currentRow++;
                        break;
                    case 'L':
                        currentCol--;
                        break;
                    case 'R':
                        currentCol++;
                        break;
                    default:
                        break;
                }

                if (currentRow >= 0 && currentRow < rows && currentCol >= 0 && currentCol < cols) // the player is in the lair
                {
                    if (matrix[currentRow, currentCol] != 'B') // The player doesn't reach bunny
                    {
                        matrix[rowStart, colStart] = '.';
                        matrix[currentRow, currentCol] = 'P';
                        //var queueWithBunnies = new Queue<List<int>>();
                        //var queueWithBunniesObjects = new Queue<Bunny>();

                        FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);
                        BunniesMultiply(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);

                        if (matrix[currentRow, currentCol] != 'B') // after the multiply bunny doesn't reach the player
                        {
                            rowStart = currentRow;
                            colStart = currentCol;
                            continue;
                        }

                        else if (matrix[currentRow, currentCol] == 'B') // after the multiply bunny reach the player
                        {
                            PrintTheMatrix(rows, cols, matrix);
                            Console.WriteLine($"dead: {currentRow} {currentCol}");
                            return;
                        }
                    }

                    else if (matrix[currentRow, currentCol] == 'B') // The player reaches bunny
                    {
                        //var queueWithBunnies = new Queue<List<int>>();
                        FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);
                        BunniesMultiply(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);
                        PrintTheMatrix(rows, cols, matrix);
                        Console.WriteLine($"dead: {currentRow} {currentCol}");
                        return;
                    }
                }

                else // The player escapes from the lair     
                {
                    matrix[rowStart, colStart] = '.';
                    //var queueWithBunnies = new Queue<List<int>>();
                    FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);
                    BunniesMultiply(rows, cols, matrix, queueWithBunnies, queueWithBunniesObjects);
                    PrintTheMatrix(rows, cols, matrix);
                    Console.WriteLine($"won: {rowStart} {colStart}");
                    return;
                }
            }
        }


        private class Bunny
        {
            public Bunny(int row, int col)
            {
                this.Row = row;
                this.Col = col;
            }

            public int Row { get; set; }

            public int Col { get; set; }
        }


        private static void BunniesMultiply(int rows, int cols, char[,] matrix, Queue<List<int>> queueWithBunnies, Queue<Bunny> queueWithBunnies2)
        {
            while (queueWithBunnies.Count > 0)
            {
                var bunniesCoordinates = queueWithBunnies.Dequeue();
                var currentBunny = queueWithBunnies2.Dequeue();
                int rowBunnies = bunniesCoordinates[0];
                int rowBunnyObject = currentBunny.Row;
                int colBunnies = bunniesCoordinates[1];
                int colBunnyObject = currentBunny.Col;

                if (rowBunnies - 1 >= 0)
                {
                    matrix[rowBunnies - 1, colBunnies] = 'B';
                }

                if (rowBunnies + 1 < rows)
                {
                    matrix[rowBunnies + 1, colBunnies] = 'B';
                }

                if (colBunnies - 1 >= 0)
                {
                    matrix[rowBunnies, colBunnies - 1] = 'B';
                }

                if (colBunnies + 1 < cols)
                {
                    matrix[rowBunnies, colBunnies + 1] = 'B';
                }
            }
        }

        private static void FindWhereAreTheBunnies
            (int rows, int cols, char[,] matrix, Queue<List<int>> queueWithBunnies, Queue<Bunny> queueWithBunnies2)
        {
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    if (matrix[row, col] == 'B')
                    {
                        queueWithBunnies.Enqueue(new List<int> { row, col });
                        queueWithBunnies2.Enqueue(new Bunny(row, col));
                    }
                }
            }
        }

        private static void PrintTheMatrix(int rows, int cols, char[,] matrix)
        {
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Console.Write(matrix[row, col]);
                }

                Console.WriteLine();
            }
        }
    }
}

 

1
Elena123456 avatar Elena123456 235 Точки

@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!

1
02/01/2021 17:31:24
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.