Vein Search of Rate-Earth-Elements C/C++ /JAVA 2S, 1MB
Samsung Electronics is planning to construct a research center in order to do research on the veins of rare-earth-elements which are used in electronic components.
Within the given discovery region, there had been rare-earth-elements found in two or more locations.
Thus, the plan is to construct a research center and to move by car from the center to the region to do the research.
The research center must be built in a location that fulfills the following requirements:
- The discovery region has the shape of a square and the size is given as N for both the height and width.
- Within the discovery region, there are areas where roads are constructed and areas which have no roads.
- The car moves from the research center to the location where a rare-earth-element is found by moving on roads that is the shortest path. The distance here is called the moving distance.
- The moving distance form the research center to each rare-earth-element containing location can all differ. The longest moving distance is called the longest moving distance.
- The research center has to be established in a place where the longest moving distance can become the shortest distance.
- The research center must be built in a place on which a road is constructed.
[Fig.1] below is an example of a discover region.
The discovery region is given as a square with a height and width length of 8.
If a cell value is “0” it refers to an area with no road and if the cell value is given as “1”, it refers to an area with a road.
The car can move only to areas that have a road constructed and it can only move to the left, right, up and down.
In the places marked with blue, red and green there had been rare-earth-elements found.
Now, the location for the research center construction has to be selected.
The locations of the rare-earth-elements will be given as their vertical, horizontal positions.
For instance, blue is given as (2,8), red as (1,2) and green as (5,8).
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
3 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 |
4 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 |
5 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 |
6 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
7 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
8 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
[Fig.1] discovery region which vertical, horizontal length is 8.
If the research center is built in the yellow marked location (2,5) as demonstrated in [Fig.2-1], the moving distance form the research center to be red location is 8, the moving distance to the blue location is 3 and the moving distance to the green location is 6. (Calculating the shortest distance. )
Therefore, if the research center is built in (2, 5), the longest moving distance is 8.
[Fig.2-1] Research center built in (2, 5) [Fig.2-2]Longest moving distance is 8
If the research center is built in the yellow marked location (4, 4) as demonstrated in [Fig3-1], the moving distance from the research center to the red location is 5, the moving distance to the blue location is 6 and the moving distance to the green location is 7
Thus, if the research center is built is (4,4), the longest moving distance is 7.
[Fig.3-1]Research center built in(4,4) [Fig.3-2] Longest moving distance is 7
Given the discovery region information and the locations where the rare-earth-elements have been found, find the location for the longest moving distance to become minimum and construct the research center. Write a program that prints this distance value.
For instance, if the information is given as in [Fig.1] and the research center is built in (4,4), the longest moving distance becomes the shortest and the correct answer is 7.
If the research center is built in (2, 4), the longest moving distance becomes 7 as well.
Thus, note that there can be several locations with the identical longest moving distances.
[Constraints]
- The height and width length of the discovery region N is given as an integer that is greater than or equal to 5 and less than or equal to 20. (5<=N<=20)
- The number of locations that contain rare-earth-elements C is greater than or equal to 2 and less than or equal to 4. (2<=C<=4)
- The location of the rare-earth-element is given in the order of vertical, horizontal position starting with 1.
The elements are only located where roads are constructed. Also, the locations are not going to overlap in the input.
- The research center can only be built where a road has been constructed.
- The research center cannot be built where a rare-earth-element has been discovered.
- The car can only move to areas where roads are constructed and it can move only towards the left, right, upper or lower direction.
- To move to a rare-earth-element location, it is possible to go through another rare-earth-element containing location.
- The locations of all rare-earth-elements are given in a manner so that they are all connected by a road.
[Input]
The first line contains a single integer T- the number of total test cases.
From the next line on, each test case is given.
The first line of each test case contains N, C- the height and width length of the discovery region, the number of regions that contain a rare-earth-element.
Throughout the following C lines, each rare-earth-element and its vertical, horizontal location is given.
Throughout the next N lines, the information on the discovery region is given.
Each line consists of N numbers. “1” refers to an area that has a road and “0” refers to an area that has no road which means it is impossible to move on it.
[Output]
For test case T print “#T” (without the quotes), leave a black space and find the location of the research center to be built of the corresponding test case so that the longest moving distance can be minimized.
Find the longest moving distance and print it. (T refers to the test case number and starts with 1.)
[Input Example]
5 // number of total test cases 5 2 // Test case 1 N=5, C=2 4 3 // location of the rare-earth-element (vertical:4, horizontal: 3) 3 4 // location of the rare-earth-element (vertical:3, horizontal: 4) 1 1 0 0 0 // discovery region information 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 |
[Correct Output to Input Example]
#1 1 #2 2 #3 2 #4 12 #5 15 |
*The location of one research center which enables the longest moving distance to become the shortest for the given input example.
#1 Construction of the research center in (3, 3)
#2 Construction of the research center in (5, 4)
#3 Construction of the research center in (6, 2)
#4 Construction of the research center in (7, 7)
#5 Construction of the research center in (13, 5)
Sample Input Vein Search of Rare-Earth-Elements
* Copy input data in the pop-up screen and paste it to the sample_input.txt file of the Visual Studio or Eclipse. (sample_input.txt is made automatically when you run Visual Studio or Eclipse And On eclipse has some problem when viewing long sample data, but when you click save button, full data will be saved)
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
15 4
11 15
15 9
1 2
14 3
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 0 0 0 0 1 1 0 1
1 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