The Erythea Campaign
Time limit1sMemory limit128 MB
On an m by n grid with blocked stronghold cells, each grid intersection has risk m+n minus its shortest distance to a stronghold boundary; find the minimum-risk grid-line path from S to D.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, BFS, Matrix
- Solved
- No attempts yet
Problem
O' mighty warrior,
Thy mission is to slay the foul king of Erythea.
Thou shall find him in his realm in the south.God bless you,
King of Isladia.
To carry out this order you must travel far to the south, enter the realm of Erythea, and slay its king at his hiding place. The realm is a rectangular region dotted with dangerous strongholds. Impenetrable walls surround the region, so the only way in is to land your Pegasus (flying horse) at some point inside it. The king's hiding place is known, so you only need to reach that location. Because the area is large and crossed by grid-like roads, you must travel along those roads. Guards watch from the towers of the strongholds: the closer you pass to a stronghold, the more likely you are to be seen. You must find the safest route from your landing point to the king.
Formally, you are given an grid of unit squares that models the realm and its roads (here is the number of rows and the number of columns). You travel along the grid lines (roads) and may turn onto another road at any intersection (road crossing). Each stronghold is a set of adjacent squares. You may not enter a stronghold, so your path never crosses the interior of a stronghold, although you may travel on a road that lies on a stronghold's boundary. You land your Pegasus exactly on a road crossing (the source ), and the king hides on another road crossing (the destination ). Neither point lies inside a stronghold, but either may sit on a stronghold boundary.
Every road crossing has a risk level. Let be the shortest road distance from the crossing to a point that lies on the boundary of a stronghold; then the crossing's risk level is . At least one stronghold is present, so this definition is always well defined.
Given the map together with the source and destination, find a path from to that runs along grid lines and minimizes the sum of the risk levels of the crossings on the path (including and ). The path may not cross the interior of any stronghold.

Input
The first line contains a single integer , the number of test cases (). The test cases follow.
Each test case begins with a line containing the number of rows and the number of columns of the grid, both between and . The second line contains four integers: the and coordinates of the source (where the Pegasus lands) followed by the and coordinates of the destination (where the king hides). Horizontal grid lines are indexed from top to bottom starting at (giving the coordinate), and vertical grid lines from left to right starting at (giving the coordinate).
After these two lines come the rows of the map. Each row is a string of 0s and 1s describing the squares of that row: a 1 means the corresponding square belongs to a stronghold, a 0 means it does not. The width of the region equals the length of each string and its height equals the number of strings.
Output
For each test case print, on its own line, the total risk of the minimum-risk path from the landing point to the destination. The total risk of a path is the sum of the risk levels of the crossings on it (including the source and the destination). If no path exists between the source and the destination, print no solution instead.