Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Given a map of gold mine which can be represented as a NxN matrix

In this map, there are several gold mines that locates on random places (maximum 4
mines each map).
A camp will be setup in the map for mining the gold. Every day, workers will go
from the camp to gold mines location.
In order to reduce the cost, company want to setup the camp so that the distance
from it to mining location is optimized.

- Worker can not go through the rock


- It takes 1 hour to travel a cell in map
There are two mines, from start point (camp location), it would take 4h to go to
each gold mine. Let's suppose the final cost will be the highest cost among them,
then if we put the camp as above figure, the final cost will be 4.
Now, we consider other example :

In the 3rd example, the final cost will be minimum (1)


[Input]
The first line of input will be the number of test case T (T ≤ 50)
In each TC :
- The first line will give the size of map N (N ≤ 20) and the number of gold mines
G (2 ≤ G ≤ 4)
- The next G lines will give location R (row) & C (column) of gold mine (1-base
indexed)
- The next N lines will give the map's data : 1 represents the road & gold mines, 0
represents the rock (can not go through)
5
5 2
4 3
3 4
1 1 0 0 0
1 1 0 0 0
1 1 1 1 1
1 1 1 0 1
1 1 1 1 1
8 2
5 6
6 4
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 0 1 0 1 1 0
1 1 1 1 0 1 1 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
10 3
8 2
5 3
7 1
0 0 0 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 0
1 0 0 1 0 0 0 0 1 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 1 0 0 1 1
1 1 1 1 0 1 0 0 1 1
1 1 1 1 0 1 0 0 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 0 0 1 0 0 1 1
1 1 1 1 1 1 1 1 1 1
8 3
1 2
2 8
5 8
0 1 0 0 0 0 0 0
0 1 0 1 1 1 1 1
0 1 0 1 0 0 0 1
0 1 1 1 0 0 0 1
0 1 0 1 0 0 0 1
0 1 0 1 1 1 1 1
0 1 0 0 1 0 0 0
0 1 0 0 1 0 0 0
10 4
9 6
1 9
7 5
7 3
1 1 1 1 1 1 0 1 1 0
1 1 1 0 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 0 1 1 1
1 1 1 1 1 0 1 1 1 1
1 0 1 1 0 1 1 1 1 1
1 0 1 1 1 1 1 0 1 1
1 0 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 1 0 1
[Output]
Your program should output the Minimum final cost for traveling from camp to gold
mines.
Case #1
1
Case #2
2
Case #3
2
Case #4
7
Case #5
6

[Constraints]
- You can move on 4 directions (up/left/down/right)
- You can not setup the camp on gold mine location or rock
- You can go pass a gold mine then go to other gold mine
- You must place camp so that workers can go to all gold mines

You might also like