CSES - Datatähti 2021 alku - Results
Submission details
Task:Ratsun reitit
Sender:Sartec
Submission time:2020-10-01 22:00:52 +0300
Language:Python3 (CPython3)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.02 s1, 2, 3details
#20.02 s1, 2, 3details
#30.02 s1, 2, 3details
#40.02 s1, 2, 3details
#50.02 s1, 2, 3details
#60.02 s1, 2, 3details
#70.03 s1, 2, 3details
#80.03 s2, 3details
#90.03 s2, 3details
#100.03 s2, 3details
#110.04 s3details
#120.06 s3details
#130.06 s3details

Code

n2 = int(input(""))

dp = [[0 for i in range(n2)] for j in range(n2)];

startCoodinateX=0
startCoodinateY=0

def getsteps(x, y, tx, ty):

    # if knight is on the target
    # position return 0.
    if (x == tx and y == ty):
        return dp[0][0];

    # if already calculated then return
    # that value. Taking absolute difference.
    elif(dp[abs(x - tx)][abs(y - ty)] != 0):
        return dp[abs(x - tx)][abs(y - ty)];
    else:

        # there will be two distinct positions
        # from the knight towards a target.
        # if the target is in same row or column
        # as of knight than there can be four
        # positions towards the target but in that
        # two would be the same and the other two
        # would be the same.
        x1, y1, x2, y2 = 0, 0, 0, 0;

        # (x1, y1) and (x2, y2) are two positions.
        # these can be different according to situation.
        # From position of knight, the chess board can be
        # divided into four blocks i.e.. N-E, E-S, S-W, W-N .
        if (x <= tx):
            if (y <= ty):
                x1 = x + 2;
                y1 = y + 1;
                x2 = x + 1;
                y2 = y + 2;
            else:
                x1 = x + 2;
                y1 = y - 1;
                x2 = x + 1;
                y2 = y - 2;

        elif (y <= ty):
            x1 = x - 2;
            y1 = y + 1;
            x2 = x - 1;
            y2 = y + 2;
        else:
            x1 = x - 2;
            y1 = y - 1;
            x2 = x - 1;
            y2 = y - 2;

        # ans will be, 1 + minimum of steps
        # required from (x1, y1) and (x2, y2).
        dp[abs(x - tx)][abs(y - ty)] = \
        min(getsteps(x1, y1, tx, ty),
        getsteps(x2, y2, tx, ty)) + 1;

        # exchanging the coordinates x with y of both
        # knight and target will result in same ans.
        dp[abs(y - ty)][abs(x - tx)] = \
        dp[abs(x - tx)][abs(y - ty)];
        return dp[abs(x - tx)][abs(y - ty)];

# Driver Code
def stepsToPoint(targetX, targetY):

    # size of chess board n*n
    n = n2*n2;

    # (x, y) coordinate of the knight.
    # (tx, ty) coordinate of the target position.
    x = 1;
    y = n2;
    tx = targetX;
    ty = targetY;

    # (Exception) these are the four corner points
    # for which the minimum steps is 4.
    if ((x == 1 and y == 1 and tx == 2 and ty == 2) or
            (x == 2 and y == 2 and tx == 1 and ty == 1)):
        ans = 4;
    elif ((x == 1 and y == n and tx == 2 and ty == n - 1) or
        (x == 2 and y == n - 1 and tx == 1 and ty == n)):
        ans = 4;
    elif ((x == n and y == 1 and tx == n - 1 and ty == 2) or
        (x == n - 1 and y == 2 and tx == n and ty == 1)):
        ans = 4;
    elif ((x == n and y == n and tx == n - 1 and ty == n - 1)
        or (x == n - 1 and y == n - 1 and tx == n and ty == n)):
        ans = 4;
    else:

        # dp[a][b], here a, b is the difference of
        # x & tx and y & ty respectively.
        dp[1][0] = 3;
        dp[0][1] = 3;
        dp[1][1] = 2;
        dp[2][0] = 2;
        dp[0][2] = 2;
        dp[2][1] = 1;
        dp[1][2] = 1;

        ans = getsteps(x, y, tx, ty);

    return ans


for (y, row) in enumerate(dp):
    for (x, value) in enumerate(row):
        dp[y][x] = (stepsToPoint(x+1,n2-y))


for (i,row) in enumerate(dp):
    for (x, number) in enumerate(row):
        if x==n2-1:
            print(number, end="\n")
        else:
            print(number, end=" ")

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
4

correct output
0 3 2 5 
3 4 1 2 
2 1 4 3 
5 2 3 2 

user output
0 3 2 3
3 2 1 2
2 1 4 3
3 2 3 2

Test 2

Group: 1, 2, 3

Verdict:

input
5

correct output
0 3 2 3 2 
3 4 1 2 3 
2 1 4 3 2 
3 2 3 2 3 
2 3 2 3 4 

user output
0 3 2 3 2
3 2 1 2 3
2 1 4 3 2
3 2 3 2 3
2 3 2 3 4

Test 3

Group: 1, 2, 3

Verdict:

input
6

correct output
0 3 2 3 2 3 
3 4 1 2 3 4 
2 1 4 3 2 3 
3 2 3 2 3 4 
2 3 2 3 4 3 
...

user output
0 3 2 3 2 3
3 2 1 2 3 4
2 1 4 3 2 3
3 2 3 2 3 4
2 3 2 3 4 3
...

Test 4

Group: 1, 2, 3

Verdict:

input
7

correct output
0 3 2 3 2 3 4 
3 4 1 2 3 4 3 
2 1 4 3 2 3 4 
3 2 3 2 3 4 3 
2 3 2 3 4 3 4 
...

user output
0 3 2 3 2 3 4
3 2 1 2 3 4 3
2 1 4 3 2 3 4
3 2 3 2 3 4 3
2 3 2 3 4 3 4
...
Truncated

Test 5

Group: 1, 2, 3

Verdict:

input
8

correct output
0 3 2 3 2 3 4 5 
3 4 1 2 3 4 3 4 
2 1 4 3 2 3 4 5 
3 2 3 2 3 4 3 4 
2 3 2 3 4 3 4 5 
...

user output
0 3 2 3 2 3 4 5
3 2 1 2 3 4 3 4
2 1 4 3 2 3 4 5
3 2 3 2 3 4 3 4
2 3 2 3 4 3 4 5
...
Truncated

Test 6

Group: 1, 2, 3

Verdict:

input
9

correct output
0 3 2 3 2 3 4 5 4 
3 4 1 2 3 4 3 4 5 
2 1 4 3 2 3 4 5 4 
3 2 3 2 3 4 3 4 5 
2 3 2 3 4 3 4 5 4 
...

user output
0 3 2 3 2 3 4 5 4
3 2 1 2 3 4 3 4 5
2 1 4 3 2 3 4 5 4
3 2 3 2 3 4 3 4 5
2 3 2 3 4 3 4 5 4
...
Truncated

Test 7

Group: 1, 2, 3

Verdict:

input
10

correct output
0 3 2 3 2 3 4 5 4 5 
3 4 1 2 3 4 3 4 5 6 
2 1 4 3 2 3 4 5 4 5 
3 2 3 2 3 4 3 4 5 6 
2 3 2 3 4 3 4 5 4 5 
...

user output
0 3 2 3 2 3 4 5 4 5
3 2 1 2 3 4 3 4 5 6
2 1 4 3 2 3 4 5 4 5
3 2 3 2 3 4 3 4 5 6
2 3 2 3 4 
...
Truncated

Test 8

Group: 2, 3

Verdict:

input
25

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated

Test 9

Group: 2, 3

Verdict:

input
49

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated

Test 10

Group: 2, 3

Verdict:

input
50

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated

Test 11

Group: 3

Verdict:

input
75

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated

Test 12

Group: 3

Verdict:

input
99

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated

Test 13

Group: 3

Verdict:

input
100

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...
Truncated