gold mining

Revision en1, by linheryt, 2021-11-29 05:57:19

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. Let consider below 5x5 map :

  • 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)

[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


  Rev. Lang. By When Δ Comment
en2 English linheryt 2021-11-29 05:58:02 920
en1 English linheryt 2021-11-29 05:57:19 1694 Initial revision (published)