Python Battle Ship
Здравейте,
Нужна ми е малко помощ при решаването на тази задача. Получавам търсения резултат в терминала, но в judge - не.
Здравейте,
Нужна ми е малко помощ при решаването на тази задача. Получавам търсения резултат в терминала, но в judge - не.
col = int(input())
board = []
att = []
count = 0
for i in range(col):
rows = input().split(" ")
board.append([rows])
attacks = input().split(" ")
while True:
for i, j in enumerate(attacks):
att.append([])
for o in j:
if o.isdigit():
att[i].append(int(o))
while len(att) > 0:
for i, j in enumerate(board):
if len(att) == 0:
break
if att[0][0] <= col:
if i == att[0][0]:
if j[0][att[0][-1]] == "0":
att.pop(0)
else:
if j[0][att[0][-1]] == "1":
count += 1
j[0][att[0][-1]] = "0"
att.pop(0)
else:
j[0][att[0][-1]] = str(int(j[0][att[0][-1]]) - 1)
att.pop(0)
break
print(count)
Едно изчистено решение:
n_rows = int(input())
field = []
for _ in range(n_rows):
field.append([int(x) for x in input().split()])
targets = [[int(x) for x in x.split('-')] for x in input().split()]
destroyed_ships = 0
for t in targets:
r, c = t[0], t[1]
if field[r][c] > 0:
field[r][c] -= 1
if field[r][c] == 0:
destroyed_ships += 1
print(destroyed_ships)
Здравейте, без enumerate,
rows = int(input()) battle_ships = [] count = 0 for row in range(rows): battle_ship = [int(num) for num in input().split()] battle_ships.append(battle_ship) attack = input().split() while attack: attack_sq = attack[0].split("-") row = int(attack_sq[0]) col = int(attack_sq[1]) for element in range(len(battle_ships)): if element == row: columns = battle_ships[element] if columns[col] != 0: columns[col] -= 1 if columns[col] == 0: count += 1 attack.pop(0) print(count)