20160720 adv Vein Search

本文描述了三星电子计划建立一个研究中心,以研究稀有地球元素,这些元素用于电子组件。中心需要建在满足条件的地方,即从中心到所有含有稀有元素地点的最短移动距离最小。文章提供了一个算法来确定最优的研究中心位置,给定发现区域的结构和稀有元素位置。
摘要由CSDN通过智能技术生成

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:

  1. The discovery region has the shape of a square and the size is given as N for both the height and width.
  2. Within the discovery region, there are areas where roads are constructed and areas which have no roads.
  3. 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.
  4. 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.
  5. The research center has to be established in a place where the longest moving distance can become the shortest distance.
  6. 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]

  1. 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)
  2. 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)
  3. 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.

  1. The research center can only be built where a road has been constructed.
  2. The research center cannot be built where a rare-earth-element has been discovered.
  3. The car can only move to areas where roads are constructed and it can move only towards the left, right, upper or lower direction.
  4. To move to a rare-earth-element location, it is possible to go through another rare-earth-element containing location.
  5. 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 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1 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0

  • 42
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值