Професионална програма
Loading...
+ Нов въпрос
ItsMomchilS avatar ItsMomchilS 2 Точки

War Planes - Python Advanced Exam

Здравейте, бих искал да видите къде може да греша в моя код. Защото на тестовете ми излиза всичко вярно, но в Judge ми излиза 50/100.

Тук е задачата: https://judge.softuni.bg/Contests/Practice/Index/2025#1

Ето и моя код:

n = int(input())

targets_counter = 0
destroyed_targets = 0
field = []
plane_pos = ()
directions = {'up': (-1, 0), 'right': (0, 1), 'down': (1, 0), 'left': (0, -1)}

for row in range(n):
    line = input().split(" ")
    if "p" in line:
        plane_pos = (row, line.index("p"))
    field.append(line)

for r in field:
    if 't' in r:
        targets_counter += 1

commands_counter = int(input())

for _ in range(commands_counter):
    command = input().split(" ")
    activity = command[0]
    direction = command[1]
    moves = int(command[2])

    row = plane_pos[0]
    col = plane_pos[1]

    row_changes = directions[direction][0]
    col_changes = directions[direction][1]

    sum_row = row + row_changes * moves
    sum_col = col + col_changes * moves

    if sum_col >= n or sum_row >= n:
        continue

    current_cell = field[sum_row][sum_col]

    if activity == "shoot":
        if current_cell != "p" and current_cell != "t":
            current_cell = 'x'
        elif current_cell == "t":
            current_cell = 'x'
            destroyed_targets += 1
        field[sum_row][sum_col] = current_cell

    if destroyed_targets == targets_counter:
        print(f"Mission accomplished! All {destroyed_targets} targets destroyed.")
        break

    elif activity == "move":
        field_place = field[sum_row][sum_col]
        if field_place == '.':
            field[row][col] = '.'
            plane_pos = (sum_row, sum_col)
            field[sum_row][sum_col] = 'p'


if targets_counter > destroyed_targets:
    print(f"Mission failed! {targets_counter - destroyed_targets} targets left.")

for x in field:
    print(' '.join(x))

 

Благодаря предварително!

0
Python 23/06/2020 15:12:02
MartinBG avatar MartinBG 2791 Точки

Има две грешки:

1. Грешно изчисляване на броя таргети, ако има повече от един таргет на ред. Фикс:

for r in field:
    for c in r:
        if c == 't':
            targets_counter += 1

 

2. Липсваше проверка за индекс, по-малък от 0. Фикс:

    if sum_col >= n or sum_row >= n or sum_row < 0 or sum_col < 0:
        continue

 

0
23/06/2020 16:00:59