CSES - Datatähti 2023 alku - Results
Submission details
Task:Ruudukko
Sender:Leipaviipale
Submission time:2022-11-04 17:08:24 +0200
Language:CPython3
Status:READY
Result:28
Feedback
groupverdictscore
#1ACCEPTED28
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.02 s1, 2, 3details
#2ACCEPTED0.02 s1, 2, 3details
#3ACCEPTED0.02 s1, 2, 3details
#4ACCEPTED0.30 s2, 3details
#5--2, 3details
#60.95 s2, 3details
#7--3details
#8--3details
#9--3details

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:

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:

input
100
2995 8734 1018 2513 7971 5063 ...

correct output
964692694

user output
278714701

Test 7

Group: 3

Verdict:

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:

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:

input
1000
520283 805991 492643 75254 527...

correct output
951147313

user output
(empty)