Crossing the Road (Large)
Time limit5sMemory limit512 MB
Find the minimum time for a pedestrian to travel across a grid of intersections where crossing depends on periodically timed traffic lights.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Simulation
- Solved
- No attempts yet
Problem
The city in this problem is a grid made of roads running east to west and roads running north to south. There is an intersection wherever an east-west road meets a north-south road, and every intersection has pedestrian lights. A pedestrian can cross a road only in the direction that has a green light.
Intersections are numbered from row to row going from north to south, and from column to column going from west to east.
The pedestrian wants to get from the northeast corner of the southwest block to the southwest corner of the northeast block. She starts at the southwest corner of intersection and finishes at the northeast corner of intersection .
Two moves are available.
- Cross a road at one intersection, moving to a corner that is not diagonally opposite. This takes 1 minute, and the light for the direction she is crossing must stay green for that entire minute. Crossing in the north-south direction needs the north-south light to be green, and crossing in the east-west direction needs the east-west light to be green.
- Walk along one edge of a block to a corner of a neighboring intersection. This takes 2 minutes.
The pedestrian moves only along the edges of the blocks. She cannot go straight from one corner of a block to the opposite corner. She can wait at a corner as long as she likes.

Traffic lights repeat the following cycle. At intersection the north-south lights stay green for 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 minutes. Then the same cycle starts again. The pedestrian starts moving at minutes, and the lights at intersection start a cycle by turning green in the north-south direction at minutes. The same cycle repeats before as well.
For example, suppose one intersection has , , . The north-south direction turns green at 0 minutes and stays green for 3 minutes, so during that time the pedestrian can cross in the north-south direction and not in the east-west direction. 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 is exactly the same as , , .
Input
The first line contains the number of test cases, . Then test cases follow in the format below.
The first line of a test case contains and , the number of east-west roads (rows) and the number of north-south roads (columns). Then lines follow. The th of those lines describes the intersections in row counted from the north, where row 0 is the northmost, and contains integers separated by spaces in this order:
, and refer to the intersection in row from the north and column from the west.
Limits
- , , , , and are all non-negative integers.
Output
For each test case, output one line containing Case #x: t, where is the test case number and is the minimum number of minutes the pedestrian needs to get from her starting corner to her destination.
Hint
In the first test case of the first example, the lights are the ones described in the statement. The pedestrian crosses to the north (1 minute), waits 2 minutes and then crosses to the east (1 minute), for a total of 4 minutes.
The second test case is shown in the diagram 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 (1 minute), for a total of 7 minutes.
