Loading...
yanchev.ilian avatar yanchev.ilian 24 Точки

02.Survivor / C# Advanced Exam - 26 June 2021

Здравейте, колеги и колежки.

Моля за малко помощ относно решението на конкретната задача.

На моя код judge ми дава скромните 42/100, не успях да си намеря грешките. => https://pastebin.com/9yjBQnRn

02.Survivor

Welcome to the new elimination Wednesday in Survivor. Your task today is to find all the tokens from the surrounding beach. But better do it before the other contestants can, you don’t want to risk getting eliminated.

Write a program, that collects tokens from the beach. First you will be given the number of rows of the beach – an integer n. On the next n lines, you will receive the available tokens to collect for each row, separated by a single space in the format:
"{token1} {token2} … {tokenn}"
The positions (cells) without tokens in them are considered empty and they will be marked with a dash ('-').

After that you will start receiving commands. There are three possibilities:

  • "Find {row} {col}":
    • You have to go to the given place if it is valid and collect the token, if there is one.
    • When you collect it, you have to mark the place as an empty, using dash symbol.
  • "Opponent {row} {col} {direction}":
    • One of your opponents is searching for a token at the given coordinates if they exist.
    • After going at the given coordinates (if they exist) and collecting the token (if there is such), the opponent is beginning a movement in the given direction by 3 steps. He collects all the tokens that are placed on his way.
    • If opponent's movement is going to step outside of the field, he is stepping only on the possible indexes.
    • When he finds tokens, he marks the cells as empty.
    • There are four possible directions, in which he can go: "up", "down", "left", "right".
  • "Gong" - the gong rings and the challenge is over.

In the end, print on the console the last condition of the beach. The cells, containing a token or not, should be separated by single space. After that print the count of the tokens you've collected:
"Collected tokens: {countOfCollected}"

Last step is to print the number of found by your opponent tokens in the format:
"Opponent's tokens: {countOfOpponentsTokens}"

1.Input

  • On the first line, you will receive the number of beach's rows - integer n
  • On the next n lines, for each row, the situation of the seashells at the beach in the described format above
  • Next, until you receive "Gong", you will get the commands in the specified format.

2.Output

  • Print the resulting beach - each cell separated by single space
  • On the next output line - print information for tokens you've collected in the described format
  • On the last line - print the number of tokens found by the opponent

3.Constraints

  • The number of rows will be positive integer between [1, 10]
  • Not all rows will have the same length
  • The tokens be marked with 'T'
  • Move commands will be: "up", "down", "left", "right"

4.Examples

 

Input

Output

Comment

6

T T - T T - T

- T - -

T - T - T T - -

- T - T - T

T T

T T T - T

Find 2 2

Find 4 1

Opponent 3 1 up

Find 4 3

Find 5 0

Find 4 0

Opponent 2 0 down

Gong

T - - T T - T

- - - -

- - - - T T - -

- - - T - T

- -

- T T - T

Collected tokens: 4

Opponent's tokens: 4

First we receive two "Find" commands, we go to the given coordinates, collect the 'T' and leave its cells empty ('-'). After that there is "Opponent" command – your opponent goes at coordinates 3 1, first collects 'T', then takes 3 steps up - the first cell is empty, so he continues up, on the second step he steals 'T' and  on the third - 'T' and sets their cells as empty. The "Find" command is next, but we don't do anything, because the coordinates are invalid. We execute the last commands in the same way. In the end we print the beach. We've collected 4 tokens. Your opponent managed to find 4 tokens.

4

- T T

T

T - - -

T

Find 9 0

Find 1 4

Opponent 0 2 right

Opponent 5 5 up

Gong

- T -

T

T - - -

T

Collected tokens: 0

Opponent's tokens: 1

 

 

 

0
Module: C# Advanced
Axiomatik avatar Axiomatik 2422 Точки
Best Answer

Main-problem => when the opponent begins his movement, he can move as long as he does not go outside of the boundary.

"is beginning a movement in the given direction by 3 steps. He collects all the tokens that are placed on his way."

 

if (isValidIndexes(row -/+ 3, col, matrix)) // if (isValidIndexes(row, col -/+ 3, matrix))

=> will not allow the opponent to make any move at all when validation is not valid, even though some initial steps can be taken.

;-)

Code:

using System;
using System.Linq;

namespace _02.Survivor
{
    public class Program
    {
        static void Main(string[] args)
        {
            int rows = int.Parse(Console.ReadLine());
            char[][] matrix = new char[rows][];

            FillTheJaggedMatrix(matrix);
            var collectedTokens = CollectedTokens(matrix, out var opponentTokens);

            //Console.WriteLine(string.Join(Environment.NewLine, matrix.Select(r => string.Join(" ",r))));
            PrintMatrix(matrix);

            Console.WriteLine($"Collected tokens: {collectedTokens}");
            Console.WriteLine($"Opponent's tokens: {opponentTokens}");
        }

        public static int CollectedTokens(char[][] matrix, out int opponentTokens)
        {
            string command;
            int collectedTokens = 0;
            opponentTokens = 0;

            while ((command = Console.ReadLine().ToLower()) != "gong")
            {
                string[] cmdArr = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                if (cmdArr.Length == 3)
                {
                    int row = int.Parse(cmdArr[1]);
                    int col = int.Parse(cmdArr[2]);
                    if (isValidIndexes(row, col, matrix))
                    {
                        if (matrix[row][col] == 'T')
                        {
                            collectedTokens += 1;
                            matrix[row][col] = '-';
                        }
                    }
                }
                else
                {
                    int row = int.Parse(cmdArr[1]);
                    int col = int.Parse(cmdArr[2]);
                    string direction = cmdArr[3];

                    if (isValidIndexes(row, col, matrix))
                    {
                        if (matrix[row][col] == 'T')
                        {
                            opponentTokens += 1;
                            matrix[row][col] = '-';
                        }

                        switch (direction)
                        {
                            case "up":

                                for (int i = 1; i <= 3; i++)
                                {
                                    int movement = row - i;

                                    if (isValidIndexes(movement, col, matrix))
                                    {
                                        if (matrix[movement][col] == 'T')
                                        {
                                            opponentTokens += 1;
                                            matrix[movement][col] = '-';
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                //if (isValidIndexes(row - 3, col, matrix))
                                //{
                                //    for (int i = row; i >= 0; i--)
                                //    {
                                //        if (matrix[i][col] == 'T')
                                //        {
                                //            opponentTokens += 1;
                                //            matrix[i][col] = '-';
                                //        }
                                //    }
                                //}

                                break;
                            case "down":
                                for (int i = 1; i <= 3; i++)
                                {
                                    int movement = row + i;

                                    if (isValidIndexes(movement, col, matrix))
                                    {
                                        if (matrix[movement][col] == 'T')
                                        {
                                            opponentTokens += 1;
                                            matrix[movement][col] = '-';
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //if (isValidIndexes(row + 3, col, matrix))
                                //{
                                //    for (int i = row; i <= row + 3; i++)
                                //    {
                                //        if (matrix[i][col] == 'T')
                                //        {
                                //            opponentTokens += 1;
                                //            matrix[i][col] = '-';
                                //        }
                                //    }
                                //}

                                break;
                            case "left":
                                for (int i = 1; i <= 3; i++)
                                {
                                    int movement = col - i;

                                    if (isValidIndexes(row, movement, matrix))
                                    {
                                        if (matrix[row][movement] == 'T')
                                        {
                                            opponentTokens += 1;
                                            matrix[row][movement] = '-';
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                //if (isValidIndexes(row, col - 3, matrix))
                                //{
                                //    for (int i = col - 3; i >= 0; i--)
                                //    {
                                //        if (matrix[row][i] == 'T')
                                //        {
                                //            opponentTokens += 1;
                                //            matrix[row][i] = '-';
                                //        }
                                //    }
                                //}

                                break;
                            case "right":
                                for (int i = 1; i <= 3; i++)
                                {
                                    int movement = col + i;

                                    if (isValidIndexes(row, movement, matrix))
                                    {
                                        if (matrix[row][movement] == 'T')
                                        {
                                            opponentTokens += 1;
                                            matrix[row][movement] = '-';
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //if (isValidIndexes(row, col + 3, matrix))
                                //{
                                //    for (int i = col; i <= col + 3; i++)
                                //    {
                                //        if (matrix[row][col] == 'T')
                                //        {
                                //            opponentTokens += 1;
                                //            matrix[row][i] = '-';
                                //        }
                                //    }
                                //}

                                break;
                        }
                    }
                }
            }

            return collectedTokens;
        }

        private static void PrintMatrix(char[][] matrix)
        {
            foreach (var line in matrix)
            {
                var currentLine = string.Join(' ', line);
                Console.WriteLine(currentLine);
            }

            //for (int row = 0; row < matrix.GetLength(0); row++)
            //{
            //    //for (int col = 0; col < matrix[row].Length; col++)
            //    //{
            //    //    Console.Write(matrix[row][col] + " ");
            //    //}

            //    var currentLine = string.Join(' ', matrix[row]);

            //    Console.WriteLine(currentLine);

            //    Console.WriteLine();
            //}
        }

        public static bool isValidIndexes(int row, int col, char[][] matrix)
        {
            return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix[row].Length;
        }

        public static void FillTheJaggedMatrix(char[][] matrix)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                char[] tokensChars = Console.ReadLine().Replace(" ", "").ToCharArray();
                //matrix[row] = new char[tokensChars.Length];
                //for (int cols = 0; cols < matrix[row].Length; cols++)
                //{
                //    if (tokensChars[cols] == ' ')
                //    {
                //        matrix[row][cols] = '-';
                //    }
                //    else
                //    {
                //        matrix[row][cols] = tokensChars[cols];
                //    }
                //}

                matrix[row] = tokensChars;
            }
        }
    }
}

 

 

0
yanchev.ilian avatar yanchev.ilian 24 Точки

Thank you, dude.

Wish you all the best!

 

Regards,

Iliyan Yanchev

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