14745 - Potato's Password III   

Description

LeeFuuChang the Potato once saw a way to generate password by combining numbers from a grid in a certain pattern.

So you, as a kind and generous student, wanted to help Potato finish the program that does the transformation from grid to password.

 

Feel free to use the following code as a starter.

def solve(n, d, t, grid):
    """
    This is the explanation of the template code

    We've read the input for you.

    this function will take in 4 parameters:
        - n:
            a integer representing the size of the grid
        - d:
            a integer representing the starting direction
        - t:
            a integer representing the rotating direction
        - grid:
            a 'nxn' grid filled with integers
            a.k.a, for any grid[i][j] is int

    you need to return a string that satisfies the problem's requirement.

    Feel free to delete this explanation after reading it.
    """

    ans = ""

    # your
    # code
    # here

    return ans

_n, _d, _t = map(int, input().split())

_grid = []
for _ in range(_n):
    _row = []
    for num in input().split():
        _row.append(int(num))
    _grid.append(_row)

print(solve(_n, _d, _t, _grid))

 

Potato judges your code by the following limitations:

  • Testcase 1:
    n = 3d = 0t = 1
  • Testcase 2 ~ 3:
    n <= 29d ∈ {0, 1, 2, 3}t = 1
  • Testcase 4 ~ 5:
    n <= 29d = 0t ∈ {0, 1}
  • Testcase 6:
    n <= 59d ∈ {0, 1, 2, 3}t ∈ {0, 1}

Potato's Whisper: If you take a closer look at the 25 grid above:

  • the 2-nd cycle always take 1 steps before turn
  • the 3-rd cycle always take 2 steps before turn
  • the 4-th cycle always take 3 steps before turn
  • and so on...

Input

First line contains 3 integers n,d,t representing:

  • The size of the grid is nxn
  • Start walking the spiral facing d direction ( Up d=0 | Left d=1 | Down d=2 | Right d=3 )
  • The spiral goes clockwise t=1 or counter-clockwise t=0

Each of the following n lines contains n integers, forming a nxn grid.

It's guaranteed that:

  • n%2 == 1
  • 1 <= n <= 59
  • d ∈ {0, 1, 2, 3}
  • t ∈ {0, 1}
  • 0 <= grid[i][j] <= n*n

Output

Output the password formed by the order of traversing the grid, with no space between numbers, and a newline at the end.

Sample Input  Download

Sample Output  Download

Tags




Discuss