Generating a Map Application
C/C++/JAVA 3S
The plan is to create a map-application which helps find the way from a starting point to a destination point through passing several stops.
The map is constructed of N lines in width and N lines in height (N*N), and given as a square shaped grid.
[Fig. 1] below is an example of a map with N=5.
[Fig. 1]
On the map, the points to stop at are marked with M numbers that are given from 1 to M.
⦁ 1: Starting point
⦁ 2~3: Stop point
⦁ 4: Destination point
In case of the map [Fig.1] above, the destination point number M is 4, the starting point 1 and the points to stop at sequentially are 2, 3 and the destination point is eventually point 4.
Also, there are areas on the map that are marked with .
This means that there has been an accident and thus cannot be passed.
Given are N, M- the size of the map, the destination point number- and the information on the map.
Write a program that finds the number of shortest paths form the starting point by passing each stop in number order to reach the destination point.
The paths must be the shortest distance. Print “0”, if no path exists.
[Example]
If a map as demonstrated above in [Fig.1] is given the number of shortest paths is as follows.
⦁ First, by starting with 1->2, there are 3 ways as demonstrated below in [Fig. 2]
⦁ Next, by moving form 2->3 there are 3 ways as demonstrated below in [Fig. 3].
⦁ Lastly, by moving form 3->4, there are 2 ways as demonstrated below in [Fig. 4].
Therefore, the number of total shortest paths is – multiplying each of 1), 2), 3) by their ways- which is 332=18.
{Constraints}
⦁ The size of the map N is given as an integer which is greater than or equal to 3 and less than or equal to 7(3<=N<=7)
⦁ The destination point number M is given as an integer which is greater than or equal to 2 and less than or equal to 5(2<=M<=5)
⦁ A case where the number of ways to be printed exceeds the int range will not be given in any case.
⦁ When searching a path between two points, it is possible to pass through other given points (the starting point, stops, destination point), as long as it is not passing an accident point.
([Fig. 5] below demonstrates an example of moving this way.)
[Fig. 5]
[Input]
The first line contains a single integer T- the number of total test case.
From the next line on each test case is given. The first line of each test case contains N, M in order – the size of the map, the destination point number.
On the following line, the N*N map size information is given. The numbers 1 to M on the map denote the starting point (1), the stops and the destination point (M). “9” refers to an accident point.
[In/Output Example]
Input Example
5
5 4
1 0 0 0 0
0 0 0 0 0
0 2 0 4 0
9 0 0 9 0
0 0 0 3 0
3 3
1 0 0
0 2 9
0 9 3
6 5
0 0 0 0 0 9
0 0 0 0 9 0
0 0 0 5 0 0
2 9 9 0 0 9
3 0 0 0 0 0
1 0 0 0 04
7 2
0 0 0 0 0 0 0
0 9 9 9 9 9 0
0 0 0 0 0 9 0
0 9 9 0 0 9 0
0 9 0 0 2 9 0
0 9 9 9 9 9 0
0 0 0 0 0 0 1
7 5
1 3 0 0 0 0 0
9 0 0 0 0 0 0
0 0 5 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 4
0 0 0 0 0