Q&A Session: Всичко, което трябва да знаете преди да кандидатствате за работа - септември 2021
Loading...
+ Нов въпрос
Olegati avatar Olegati 2 Точки

Problem 2 - Python 85/100 точки (Java Advanced Exam - 26 June 2021)

Здравейте, 

На 2-ра задача от Java Advanced Exam - 26 June 2021 първият тест гърми. Предполагам, че проблема е при изхода "You win! Final python length is {length}", тъй като единствено този изход не се покрива от test case-овете. Но не намирам грешката.

Pastebin на задачата  -> https://pastebin.com/inkuP1wu

Линк към задача в Judge -> https://judge.softuni.org/Contests/Practice/Index/2997#1


Условието

One of the candidates-intern must solve the task - "Python". He must write a Java code that calculates wheater the python wins or loses the game.

Here are the rules of the game:

The game starts with a python length of 1.

We get as input the size of the screen in which our python moves. The screen is always a square. After that we receive the commands wich represent the directions in wich the python should move.The python starts from s-position The commands will be: left/right/up/down. If the python reaches the side edge of the screen (left or right), it goes to the opposite side of the same row. If the python reaches the top/bottom edge of the screen it goes on the opposite side of the same column . The possible characters that may appear on the screeen are:

  • *– that is a regular asterisk; it does nothing
  • e – represents an enemy.
  • f – this is the food
  • s - the place where the game starts

 

Each time you eat a piece of food your length increases by one. Keep track of the length, because in case you win you have to print it. If you step on an enemy the game is over (the python stops moving) and you have to print the output as shown in the output section. After executing all of the commands there are 3 possible outcomes:

  • you have eaten all the food and you win
  • you get killed by an enemy
  • there is still some food to be eaten

Print the corresponding output depending on the case.

Input

  • Length of the screen side – an integer number.
  • Commands to move the python – an array of strings separated by ",".

Output

  • There are three types of output:
    • If all of the food is eaten print the following output: "You win! Final python length is {length}"
    • If there are no left commands and there is still some food to be eaten: "You lose! There is still {left food} food to be eaten."
    • If you step on the enemy the game is over and you print "You lose! Killed by an enemy!"

Constraints

  • The input numbers will be a 32-bit integer in the range [0 … 2 147 483 647].
  • Allowed working time for your program: 0.1 seconds.
  • Allowed memory: 16 MB.
     

Examples

Input

Output

Comments

5

up, right, right, right, up

* * e * *

* * * f *

* f * * *

s * * * *

* * e * *

You win! Final python length is 3

After executing all of the commands the python has eaten all of the food and it is still alive.

4

right, right, right, right, right, down, right

* s * *

* * e *

* f * f

* * * f

You lose! Killed by an enemy!

The python moves 2 times to the right, then it goes off screen and appears on left side. Then it makes 2 more moves to the right, it goes down, steps on an enemy and the game is over.

6

down, left, left, down, right, right, right, right, right

* * * * s *

* e f * * *

f * * * * *

* * * f e *

* e * * * *

* * * * * *

You lose! There is still 1 food to be eaten.

The python survived but there is still 1 piece of food that the python could not eat.

Тагове:
0
Java Advanced
svephoto avatar svephoto 966 Точки

Здравей, Olegati,

 

Как тестваш решението в Judge с този синтаксис

 

case "up" -> moveUp(matrix, s);
case "down" -> moveDown(matrix, s);
case "left" -> moveLeft(matrix, s);
case "right" -> moveRight(matrix, s);

 

Като го пусна, получавам Compile time error. Иначе малка промяна в конструкцията и вече си на 100/100. Две от принтиранията трябва да стават в самия цикъл и при всяко от тях трябва да прекратим програмата: ако свърши храната или ако играчът стъпи върху враг. Съответно спираме и проверката на следващата команда. For-цикълът със switch-a и проверките с печатането:

 

for (String direction : directions) {
    switch (direction) {
        case "up":
            moveUp(matrix, s);
            break;
        case "down":
            moveDown(matrix, s);
            break;
        case "left":
            moveLeft(matrix, s);
            break;
        case "right":
            moveRight(matrix, s);
            break;
    }

    if (matrix[s[0]][s[1]].equals("f")) {
        countF--;
        length++;
    }

    if (matrix[s[0]][s[1]].equals("e")) {
        gameOver = true;
    }

    if (gameOver) {
        System.out.println("You lose! Killed by an enemy!");
        return;
    }

    if (countF == 0) {
        System.out.printf("You win! Final python length is %d%n", length);
        return;
    }
}

System.out.printf("You lose! There is still %d food to be eaten.%n", countF);
0
11/09/2021 00:51:50
Olegati avatar Olegati 2 Точки

Благодаря, сработи yes

0
svephoto avatar svephoto 966 Точки

Моля, така и трябваше. yes

0