| Task: | Ruudukko |
| Sender: | Leipaviipale |
| Submission time: | 2022-11-04 17:08:24 +0200 |
| Language: | Python3 (CPython3) |
| Status: | READY |
| Result: | 28 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 28 |
| #2 | TIME LIMIT EXCEEDED | 0 |
| #3 | TIME LIMIT EXCEEDED | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.02 s | 1, 2, 3 | details |
| #2 | ACCEPTED | 0.02 s | 1, 2, 3 | details |
| #3 | ACCEPTED | 0.02 s | 1, 2, 3 | details |
| #4 | ACCEPTED | 0.30 s | 2, 3 | details |
| #5 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
| #6 | WRONG ANSWER | 0.95 s | 2, 3 | details |
| #7 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #8 | TIME LIMIT EXCEEDED | -- | 3 | details |
| #9 | TIME LIMIT EXCEEDED | -- | 3 | details |
Code
# returns array of num arrays
def get_input():
height = int(input())
lines = [] # of int[]
line = [] # of str
for _ in range(height):
line = input().split()
lines.append([eval(num) for num in line])
return (height, lines)
# constants
(HEIGHT, LINES) = get_input()
MAX_ARRAY_INDEX = HEIGHT - 1
# global memo item
memo = dict()
# str in format "row,col" and int
# uses recursion
def count_possible_moves(square_coordinate : str, square_num : int) -> int:
move_count = memo.get(square_coordinate, -1)
if (move_count >= 0):
return move_count
# row col combo not in memo
counter = 0
separator_index = str(square_coordinate).find(",")
row = int(square_coordinate[:separator_index])
col = int(square_coordinate[separator_index + 1:])
# loop right
right_col = col + 1
while (right_col <= MAX_ARRAY_INDEX):
if (square_num > LINES[row][right_col]):
counter += 1
counter += count_possible_moves(f"{row},{right_col}", LINES[row][right_col])
right_col += 1
# loop left
left_col = col - 1
while (left_col >= 0):
if (square_num > LINES[row][left_col]):
counter += 1
counter += count_possible_moves(f"{row},{left_col}", LINES[row][left_col])
left_col -= 1
# loop down
down_row = row + 1
while (down_row <= MAX_ARRAY_INDEX):
if (square_num > LINES[down_row][col]):
counter += 1
counter += count_possible_moves(f"{down_row},{col}", LINES[down_row][col])
down_row += 1
# loop up
up_row = row - 1
while (up_row >= 0):
if (square_num > LINES[up_row][col]):
counter += 1
counter += count_possible_moves(f"{up_row},{col}", LINES[up_row][col])
up_row -= 1
memo[square_coordinate] = counter
return counter
def main():
counter = 0
# loop throught every square
for i in range(len(LINES)):
for j in range(len(LINES[i])):
count = count_possible_moves(f"{i},{j}", LINES[i][j])
# add current square in count
counter += count + 1
# print result
print(counter % ((10**9)+9))
main()Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 3 1 1 1 1 1 1 1 1 1 |
| correct output |
|---|
| 9 |
| user output |
|---|
| 9 |
Test 2
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 3 1 2 3 6 5 4 7 8 9 |
| correct output |
|---|
| 135 |
| user output |
|---|
| 135 |
Test 3
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 3 7 8 1 4 5 4 3 9 6 |
| correct output |
|---|
| 57 |
| user output |
|---|
| 57 |
Test 4
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 10000 |
| user output |
|---|
| 10000 |
Test 5
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| correct output |
|---|
| 187458477 |
| user output |
|---|
| (empty) |
Test 6
Group: 2, 3
Verdict: WRONG ANSWER
| input |
|---|
| 100 2995 8734 1018 2513 7971 5063 ... |
| correct output |
|---|
| 964692694 |
| user output |
|---|
| 278714701 |
Test 7
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 1000000 |
| user output |
|---|
| (empty) |
Test 8
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
| correct output |
|---|
| 229147081 |
| user output |
|---|
| (empty) |
Test 9
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000 520283 805991 492643 75254 527... |
| correct output |
|---|
| 951147313 |
| user output |
|---|
| (empty) |
