Under construction

Still being built. Things may change

Hyundai Mobis Autonomous Driving System

Interview

Time limit1sMemory limit512 MB

Summary
Find the shortest path from the top-left to the bottom-right of a grid that visits at least one waypoint, using up to K uses of a 5x5 jump pattern.
Level

Medium5 of 10

Topics
BFS, Shortest path, Graph
Solved
No attempts yet

Problem

At the Seosan test track of Hyundai Mobis, a mobility fitted with parts from the company's new autonomous driving system is undergoing driving tests. The system uses satellite imagery to recognize the area around the mobility and computes the shortest distance from its current position to the destination. The old system moved the mobility only up, down, left, and right. The new system moves it in those four directions and also with additional movement patterns. The system cannot keep communicating with the satellite, so the additional patterns cannot be used without limit.

Seungjun is testing this system to find the shortest distance from a fixed start at the top-left corner to a fixed final destination at the bottom-right corner, passing through at least one of the waypoints. The destination may itself be a waypoint, in which case visiting the waypoint counts as arriving at the destination. Given the map, the additional movement patterns, and the number of times each pattern may be used, write a program that finds the shortest distance from the start, through at least one waypoint, to the final destination.

A pattern is given as a 5×55 \times 5 array. The mobility is at row 33, column 33 of the array. If the value at row AA, column BB is 11, the system can use a pattern that moves the mobility from row 33, column 33 to row AA, column BB.

Input

The first line contains the height NN, the width MM, and the number of pattern uses KK. (1≤N,M≤200,0≤K≤30)(1 \leq N,M \leq 200, 0 \leq K \leq 30)

The map has NN rows and MM columns. 0 is an empty cell, 1 is a wall, and 2 is a waypoint. The mobility cannot move onto a wall or outside the map. The start at the top-left cell is guaranteed to be 0.

The next 5 lines give the 5×55 \times 5 pattern array, with values 0 or 1.

Output

Print the shortest distance from the start, passing through at least one waypoint, to the destination. If the destination cannot be reached through at least one waypoint in any case, print -1.

Examples3

  1. Example 1

    Input
    3 3 0
    0 1 2
    0 1 0
    0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    
    Expected output
    8
    
  2. Example 2

    Input
    9 7 4
    0 2 0 0 0 1 2
    2 0 0 2 0 2 2
    2 1 1 1 2 0 0
    0 1 1 1 0 0 1
    2 0 0 2 1 0 1
    1 2 2 2 1 1 2
    1 2 1 1 2 1 0
    2 1 2 2 0 1 1
    2 1 2 0 1 2 2
    1 0 1 0 0
    0 0 0 1 0
    1 0 1 1 0
    0 0 1 1 1
    0 0 1 0 0
    
    Expected output
    7
    
  3. Example 3

    Input
    4 10 2
    0 0 0 1 1 2 1 2 1 2
    2 1 2 0 2 2 0 0 1 0
    1 0 1 2 2 2 2 1 2 2
    2 2 1 0 1 1 0 1 0 1
    1 0 0 0 1
    1 1 0 0 1
    1 0 0 0 1
    1 0 1 0 0
    0 0 1 1 1
    
    Expected output
    -1