编程介的小学生 2017-02-04 16:14 采纳率: 20.5%
浏览 767
已采纳

Merging Maps

Pictures taken from an airplane or satellite of an area to be mapped are often of sufficiently high resolution to uniquely identify major features. Since a single picture can cover only a small portion of the earth, mapping larger areas requires taking pictures of smaller overlapping areas, and then merging these to produce a map of a larger area.

For this problem you are given several maps of rectangular areas, each represented as an array of single-character cells. A cell contains an uppercase alphabetic character ('A' to 'Z') if its corresponding area contains an identifiable major feature. Different letters correspond to different features, but the same major feature (such as a road) may be identified in multiple cells. A cell contains a hyphen ('-') if no identifiable feature is located in the cell area. Merging two maps means overlaying them so that one or more common major features are aligned. A cell containing a major feature in one map can be overlaid with a cell not containing a major feature in the other. However, different major features (with different letters) cannot be overlaid in the same cell.

Consider the five 3-row, 5-column maps shown above. The rightmost column of map 1 perfectly matches the leftmost column of map 2, so those maps could be overlaid to yield a 3-row, 9-column map. But map 1 could also overlay map 3 as well, since the C and B features in the rightmost column of map 1 match those in the leftmost column of map 3; the D does not perfectly match the '-' in the center of the column, but there is no conflict. In a similar manner, the top row of map 1 could also overlay the bottom row of map 3.

The "score" of a pair of maps indicates the extent to which the two maps match. The score of an overlay of a pair of maps is the number of cells containing major features that coincide in the overlay that gives the best match. The score for the map pair is the maximum score for the possible overlays of the maps. Thus, the score for a pair of maps each having 3 rows and 5 columns must be in the range 0 to 15.

An "offset" is a pair of integers (r,c) that specifies how two maps, a and b, are overlaid. The value of r gives the offset of rows in b relative to rows in a; similarly, c gives the offset of columns in b relative to columns in a. For example, the overlay of map 1 and map 2 shown above has the offset (0,4) and a score of 3. The two overlays of map 1 and map 3 yielding scores of 2 have offsets of (0,4) and (-2,0).

The following steps describe how to merge a sequence of maps:

Merge the pair of maps in the sequence that yield the highest positive score (resolving ties by choosing pair that has the map with the lowest sequence number).
Remove the maps that were merged from the sequence.
Add the resulting merged map to the sequence, giving it the next larger sequence number.
In the example above, maps 1 and 2 would be merged to produce map 6, and maps 1 and 2 would be removed from the sequence. Steps 1, 2 and 3 are repeated until only a single map remains in the sequence, or until none of the maps in the sequence can be merged (that is, until the overlay score for each possible map pair is zero).

If two maps can be merged in several ways to yield the same score, then merge them using the smallest row offset. If the result is still ambiguous, use the smallest row offset and the smallest column offset.

Input

The input will contain one or more sets of data, each containing between 2 and 10 maps. Each set of data begins with an integer specifying the number of maps in the sequence. The maps follow, each beginning with a line containing two integers NR and NC (1 <= NR, NC <= 10) that specify the number of rows and columns in the map that immediately follows on the next NR lines. The first NC characters on each of these NR lines are the map data, and any trailing characters on such lines are to be ignored.

Input for the last test case is followed by a line consisting of the number 0.

Output

For each set of data, display the input case number (1, 2, ��) and the merged maps, each identified with its sequence number and enclosed by a border. The output should be formatted as shown in the samples below. No merged map will have more than 70 columns. Print a blank line between cases.

Sample Input

5
3 5
--A-C
----D
----B
3 5
C----
D---F
B----
3 5

C----

B-A-C
3 5
----D

-E--B

3 5
-D--C
----G
----B
2
3 5
----A
----B
----C
3 5
A----
B----
D----
0
Sample Output

Case 1
MAP 9:
+-------------+
|-D--C--------|
|----G--------|
|----B-A-C----|
|--------D---F|
|-----E--B----|
|-------------|
+-------------+

Case 2
MAP 1:
+-----+
|----A|
|----B|
|----C|
+-----+

MAP 2:
+-----+
|A----|
|B----|
|D----|
+-----+
  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-11 15:21
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!