This page is still under construction.

Parts of this page are still being built. What you see may change.

Crossing the Road (Small)

Time limit5sMemory limit512 MB

Summary
On a tiny grid whose intersections have periodic pedestrian lights, find the minimum time to walk from the southwest corner of the grid to the northeast corner.
Level

Medium5 of 10

Topics
Shortest path, Graph, Simulation
Solved
No attempts yet

Problem

Where roads intersect there are traffic lights that tell pedestrians when they may cross. A clever pedestrian plans her route through the city around the moments those lights turn green.

The city in this problem is a grid of NN roads running east to west and MM roads running north to south, so it has N×MN \times M intersections. The pedestrian wants to get from the northeast corner of the southwest block to the southwest corner of the northeast block. In other words, she starts at the southwest corner of the southwesternmost intersection and finishes at the northeast corner of the northeasternmost intersection. Find the smallest number of minutes she needs to get from the start corner to the goal corner.

Crossing one road takes 1 minute, and the light for that direction must be green for the entire crossing. Every intersection has four corners, and one crossing moves the pedestrian to an adjacent corner of the same intersection. Walking along one edge of a block, between two neighboring intersections, takes 2 minutes and needs no light. The pedestrian moves only along the edges of a block; she cannot cut diagonally from one corner of a block to the opposite corner.

Traffic lights follow this pattern. At intersection ii the north-south lights stay green for SiS_i minutes while the east-west lights stay red. Then the north-south lights turn red, the east-west lights turn green, and they stay that way for WiW_i minutes. Then the same cycle starts again. The pedestrian starts moving at t=0t=0 minutes, and the cycle at intersection ii starts by turning green in the north-south direction at t=Tit=T_i minutes. The same cycle also repeats before t=Tit=T_i.

For example, suppose intersection 0 has S0=3S_0 = 3, W0=2W_0 = 2, T0=0T_0 = 0. The north-south direction turns green at minute 0 and stays green for 3 minutes, and during those 3 minutes the pedestrian can cross in the north-south direction only. Then the lights switch, and for the next 2 minutes she can cross in the east-west direction only. Five minutes after it started, the cycle starts again. This configuration is exactly the same as S0=3S_0 = 3, W0=2W_0 = 2, T0=10T_0 = 10.

Input

The first line contains the number of test cases, CC. Then CC test cases follow, each in this format.

The first line of a test case contains NN and MM, the number of east-west roads and the number of north-south roads. Then NN lines follow. The iith of those lines describes the intersections on the iith row from the north, where the northmost row is row 0. Each of those lines contains 3M3M integers separated by spaces, in this order.

S[i][0] W[i][0] T[i][0] S[i][1] W[i][1] T[i][1] ... S[i][M-1] W[i][M-1] T[i][M-1]

Si,jS_{i,j}, Wi,jW_{i,j} and Ti,jT_{i,j} belong to the intersection in the iith row from the north and the jjth column from the west.

Limits

  • CC, NN, MM, Si,jS_{i,j}, Wi,jW_{i,j} and Ti,jT_{i,j} are all non-negative integers.
  • C≤100C \le 100
  • 1≤N,M≤31 \le N, M \le 3
  • 0<Si,j,Wi,j≤100 < S_{i,j}, W_{i,j} \le 10
  • 0≤Ti,j≤200 \le T_{i,j} \le 20

Output

For each test case, output one line containing Case #x: t, where xx is the number of the test case and tt is the minimum number of minutes the pedestrian needs to get from the start corner to the goal corner.

Note

The first case of the first example uses the light setting described above. The pedestrian crosses to the north (1 minute), waits 2 minutes, then crosses to the east (1 minute), for a total of 4 minutes.

The second case is drawn below. The pedestrian crosses to the east (1 minute), waits 2 minutes and crosses to the north (1 minute). Then she walks east one block (2 minutes) and crosses to the east again (1 minute), for a total of 7 minutes.

Examples1

  1. Example 1

    Input
    2
    1 1
    3 2 10
    1 2
    1 5 3 1 5 2
    
    Expected output
    Case #1: 4
    Case #2: 7