Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

vesoSoftUni avatar vesoSoftUni 0 Точки

Radioactive Mutant Vampire Bunnies - Test #3 (Incorrect answer)

https://pastebin.com/JN6zPgZv

Здравейте,

няколко часа се боря с тази задача и я докарах до 90/100 . Гърми само тест 3 и вече съм много уморен да намеря грешката , та ако може някой да погледне задачата и да даде съвет от къде идва проблема :) . 

0
C# Advanced
Axiomatik avatar Axiomatik 2422 Точки

110 %, ;-)

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

namespace R_Bunnies_2
{
    internal class Program
    {
        static void Main()
        {
            int[] size = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int row = size[0];
            int col = size[1];

            char[,] field = new char[row, col];

            int[] starPsn = FillElements(row, col, field);
            int playerRow = starPsn[0];
            int playerCol = starPsn[1];

            //this stack will hold the bunnies cordinates
            Stack<int> bunnies = new Stack<int>();

            //check if we go at the end of the field
            bool won = false;

            string comands = Console.ReadLine();
            const int MOVE = 1;


            for (int c = 0; c < comands.Length; c++)
            {
                char comand = comands[c];

                field[playerRow, playerCol] = '.';

                if (comand == 'U')
                {
                    if (playerRow > 0)
                        playerRow -= MOVE;
                    else
                    {
                        won = true;
                    }
                    // ERROR: Player has only won when leaving the field
                    // playerRow == 0 => Player is still on the field
                    //if (playerRow == 0)
                    //    won = true;
                }
                else if (comand == 'D')
                {
                    if (playerRow < row - 1)
                        playerRow += MOVE;
                    else
                    {
                        won = true;
                    }
                    // ERROR: Player has only won when leaving the field
                    // playerRow == row - 1 => Player is still on the field
                    //if (playerRow == row - 1)
                    //    won = true;
                }
                else if (comand == 'L')
                {
                    if (playerCol > 0)
                        playerCol -= MOVE;
                    else
                    {
                        won = true;
                    }
                    // ERROR: Player has  only won when leaving the field
                    // playerCol == 0 => Player is still on the field
                    //if (playerCol == 0)
                    //    won = true;
                }
                else if (comand == 'R')
                {
                    if (playerCol < col - 1)
                        playerCol += MOVE;
                    else
                    {
                        won = true;
                    }
                    // ERROR: Player has  only won when leaving the field
                    // playerCol == col - 1 => Player is still on the field
                    //if (playerCol == col - 1)
                    //    won = true;
                }

                // First check if player has won, second if player has stepped unto a bunny,
                // third bunny grows unto player's position

                //if won is true he made it out of the field
                if (won)
                {
                    //we clone the bunnies one more time
                    BunniesGrow(row, col, field, bunnies);

                    //print the matrix and player psn
                    PrintMatrix(field, row, col);
                    Console.WriteLine($"won: {playerRow} {playerCol}");
                    return;
                }

                if (field[playerRow, playerCol] == 'B')
                {
                    BunniesGrow(row, col, field, bunnies);
                    PrintMatrix(field, row, col);
                    Console.WriteLine($"dead: {playerRow} {playerCol}");
                    return;
                }

                //Grow the number of bunnies
                BunniesGrow(row, col, field, bunnies);

                //PrintMatrix(field, row, col);

                //if player is on B he is dead
                if (field[playerRow, playerCol] == 'B')
                {
                    PrintMatrix(field, row, col);
                    Console.WriteLine($"dead: {playerRow} {playerCol}");
                    return;
                }
            }
        }//main

        private static void BunniesGrow(int row, int col, char[,] field, Stack<int> bunnies)
        {
            //fill up the stack with all the bunnies cords 
            FaindBunny(field, bunnies);

            while (bunnies.Count != 0)
            {
                int bunnyCol = bunnies.Pop();
                int bunnyRow = bunnies.Pop();

                //bunny check if is possible to grow up
                //if (bunnyRow > 0 && field[bunnyRow - 1, bunnyCol] != 'B')
                if (bunnyRow > 0)
                {
                    field[bunnyRow - 1, bunnyCol] = 'B';
                }
                //bunny check if is possible to grow down 
                //if (bunnyRow < row - 1 && field[bunnyRow + 1, bunnyCol] != 'B')
                if (bunnyRow < row - 1)
                {
                    field[bunnyRow + 1, bunnyCol] = 'B';
                }
                //bunny check if is possible to grow left
                //if (bunnyCol > 0 && field[bunnyRow, bunnyCol - 1] != 'B')
                if (bunnyCol > 0)
                {
                    field[bunnyRow, bunnyCol - 1] = 'B';
                }
                //bunny check if is possible to grow right
                //if (bunnyCol < col - 1 && field[bunnyRow, bunnyCol + 1] != 'B')
                if (bunnyCol < col - 1)
                {
                    field[bunnyRow, bunnyCol + 1] = 'B';
                }
            }
        }

        private static void FaindBunny(char[,] field, Stack<int> bunnies)
        {
            //read the field and save cords of all bunnies
            for (int i = 0; i < field.GetLength(0); i++)
            {
                for (int j = 0; j < field.GetLength(1); j++)
                {
                    if (field[i, j] == 'B')
                    {
                        bunnies.Push(i);
                        bunnies.Push(j);
                    }
                }
            }
        }

        private static int[] FillElements(int row, int col, char[,] field)
        {
            int[] result = new int[2];


            for (int i = 0; i < row; i++)
            {
                string temp = Console.ReadLine();

                for (int j = 0; j < col; j++)
                {
                    //fill up the char to the psn on the field
                    field[i, j] = temp[j];

                    //when we have the char "P" we save the cordinates
                    if (field[i, j] == 'P')
                    {
                        result[0] = i;
                        result[1] = j;
                    }
                }
            }
            //return index 1 = start row ; 2 = start col ;
            return result;
        }

        private static void PrintMatrix(char[,] matrix, int row, int col)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    Console.Write($"{matrix[i, j]}");
                }
                Console.WriteLine();
            }
        }
    }
}

 

0
vesoSoftUni avatar vesoSoftUni 0 Точки

Мерси :) стана 100/100.

 

// First check if player has won, second if player has stepped unto a bunny, // third bunny grows unto player's position

Отностно това то няма значение дали ще го настъпиш или то ще порасне върху тебе просто си правя 1 проверка накрая и за 2те. Спестява редове ,а и в 2та случая умираш :) няма разлика

 

https://pastebin.com/ZZ8MyDfX работещия код ако на някой му трябва.

0
27/12/2021 08:26:41
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.