CSES - Datatähti 2022 loppu - Results
Submission details
Task:Peli
Sender:okkokko
Submission time:2022-01-22 16:47:58 +0200
Language:Python3 (PyPy3)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.06 s1, 2, 3details
#20.06 s2, 3details
#30.06 s3details
#40.06 s1, 2, 3details
#50.06 s1, 2, 3details

Code

from itertools import product


def I():
    return map(int, input().split())


def main():
    n, m = I()  # y,x
    sokStr = [input() for _ in range(n)]

    def findPerson(person):
        for i in range(n):
            a = sokStr[i].find(person)
            if a != -1:
                return a, i
    Ax, Ay = findPerson("A")
    Bx, By = findPerson("B")
    Apath = []
    Bpath = []
    sokkelo = [[1 if i == "#" else 0 for i in s] for s in sokStr]
    # wall is 1, A is 2, B is 3

    def mark(x, y, side):
        if not sokkelo[y][x]:
            sokkelo[y][x] = side
            (Apath if side == 2 else Bpath).append((y, x))
            for a in (1, -1):
                if 1 <= x + a < m - 1:  # jokainen reunaruutu on seinää
                    mark(x + a, y, side)
                if 1 <= y + a < n - 1:
                    mark(x, y + a, side)

    def markA(x, y):
        if not sokkelo[y][x]:
            sokkelo[y][x] = 2
            Apath.append((y, x))
            for a in (1, -1):
                if 1 <= x + a < m - 1:  # jokainen reunaruutu on seinää
                    markA(x + a, y)
                if 1 <= y + a < n - 1:
                    markA(x, y + a)

    def markB(x, y):
        if not sokkelo[y][x]:
            sokkelo[y][x] = 3
            Bpath.append((y, x))
            for a in (1, -1):
                if 1 <= x + a < m - 1:  # jokainen reunaruutu on seinää
                    markB(x + a, y)
                if 1 <= y + a < n - 1:
                    markB(x, y + a)
    try:
        markA(Ax, Ay)  # Virhe on täällä, ei ole IndexError
    except:
        # print(0)
        # return
        pass
    if sokkelo[By][Bx] == 2:
        print(1)
        return
    markB(Bx, By)

    def manhattan(a, b):
        return abs(a[0] - b[0]) + abs(a[1] - b[1])
    print(min(manhattan(a, b) for a, b in product(Apath, Bpath)))


if __name__ == "__main__":
    main()

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100000 3
BBAACBCBACBACABBCBAABCBCCBCCAA...

correct output
18201

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 70, in <module>
    main()...

Test 2

Group: 2, 3

Verdict:

input
100000 10
BAACABCCBCBAACBBCCCCABBBBACCBA...

correct output
29684

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 70, in <module>
    main()...

Test 3

Group: 3

Verdict:

input
100000 50
ACAABCBBAAAACCBBABACACACBCAACA...

correct output
32740

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 70, in <module>
    main()...

Test 4

Group: 1, 2, 3

Verdict:

input
3 1
ABC

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 70, in <module>
    main()...

Test 5

Group: 1, 2, 3

Verdict:

input
3 2
ABC

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 70, in <module>
    main()...