Pictures taken from an airplane or satellite of an ar ea 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 diff erent letters) cannot be overlaid in the same cell.

          --A-C     C----     C----     ----D     -D--C
----D D---F ----- -E--B ----G
----B B---- B-A-C ----- ----B
Map # 1 2 3 4 5

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 (rc) that specifies how two maps, a and b, are overlaid. The value of rgives 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:

  1. Merge the pair of maps in the sequence that yield the highest positive score (resolving ties by choos ing pair that has the map with the lowest sequence number).
  2. Remove the maps that were merged from the sequence.
  3. 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  (   1NRNC10 ) 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.

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----|
+-----+
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct MAPS
{
int n,m;
char feature[110][110];
};
struct Merge
{
int score,r,c;
};
char s[2000];
int cases,n,i,j,k,sum,max,x,y;
MAPS map[100];
Merge merge[100][100];
bool sign[100];
int calc(char a,char b)
{
if(a=='-') return 0;
if(b=='-') return 0;
if(a==b) return 1;
else return -10000;
} int Max(int a,int b)
{
if(a>b) return a;
else return b;
} int Min(int a,int b)
{
if(a>b) return b;
else return a;
} Merge Try(int x,int y)
{
int r,c,i,j,k,score,x1,y1,x2,y2;
Merge tmp;
tmp.score=0;
for(r=-map[y].n+1;r<map[x].n;r++)
for(c=-map[y].m+1;c<map[x].m;c++)
{
x1=Max(0,r);y1=Max(0,c);
x2=Min(map[x].n,map[y].n+r);y2=Min(map[x].m,map[y].m+c);
score=0;
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
score+=calc(map[x].feature[i][j],map[y].feature[i-r][j-c]);
if(score>tmp.score)
{
tmp.score=score;
tmp.r=r;
tmp.c=c;
}
}
return tmp;
} void add_map(int x,int y,int z)
{
int i,j,k,Dx,Dy;
Dx=Max(-merge[x][y].r,0);Dy=Max(-merge[x][y].c,0);
map[z].n=Max(map[x].n,map[y].n+merge[x][y].r)+Dx;
map[z].m=Max(map[x].m,map[y].m+merge[x][y].c)+Dy;
for(i=0;i<map[z].n;i++)
for(j=0;j<map[z].m;j++)
map[z].feature[i][j]='-';
for(i=0;i<map[x].n;i++)
for(j=0;j<map[x].m;j++)
map[z].feature[i+Dx][j+Dy]=map[x].feature[i][j];
Dx+=merge[x][y].r;Dy+=merge[x][y].c;
for(i=0;i<map[y].n;i++)
for(j=0;j<map[y].m;j++)
if(map[y].feature[i][j]!='-')
map[z].feature[i+Dx][j+Dy]=map[y].feature[i][j];
} int main()
{
for(cases=1;;cases++)
{
scanf("%d",&n);
if(n==0) break;
for(i=1;i<=n;i++)
{
scanf("%d %d",&map[i].n,&map[i].m);
for(j=0;j<map[i].n;j++)
{
scanf("%s",s);
for(k=0;k<map[i].m;k++)
map[i].feature[j][k]=s[k];
}
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
merge[i][j]=Try(i,j);
memset(sign,1,sizeof(sign));
while(1)
{
max=0;
for(i=1;i<=n;i++) if(sign[i])
for(j=i+1;j<=n;j++) if(sign[j])
if(merge[i][j].score>max)
{
max=merge[i][j].score;
x=i;y=j;
}
if(max==0)
break; sign[x]=sign[y]=0;
n++;
add_map(x,y,n);
for(i=1;i<n;i++)
if(sign[i])
merge[i][n]=Try(i,n);
}
if(cases>1)
printf("\n");
printf("Case %d\n",cases);
x=0;
for(i=1;i<=n;i++)
if(sign[i])
{
if(x)
printf("\n");
x++;
printf(" MAP %d:\n",i);
printf(" +");
for(j=0;j<map[i].m;j++)
printf("-");
printf("+\n");
for(j=0;j<map[i].n;j++)
{
printf(" |");
for(k=0;k<map[i].m;k++)
printf("%c",map[i].feature[j][k]);
printf("|\n");
}
printf(" +");
for(j=1;j<=map[i].m;j++)
printf("-");
printf("+\n");
}
}
return 0;
}

1033 - Merging Maps的更多相关文章

  1. 检索Google Maps地图位置(小训练)

    名称:检索地图位置 内容:地图初期显示和检索显示 功能:根据条件检索地图的经度与纬度 1.在这之前我们需要创建一个表(Accoun__c),添加一个重要的字段地理位置情報,它会默认的给你两个字段经度和 ...

  2. [Erlang 0121] 当我们谈论Erlang Maps时,我们谈论什么 Part 3

    Erlang/OTP 17.0 has been released  http://www.erlang.org/download/otp_src_17.0.readme     Erlang/OTP ...

  3. [Erlang 0117] 当我们谈论Erlang Maps时,我们谈论什么 Part 2

    声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.后续Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...

  4. [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1

         Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...

  5. The Practical Guide to Empathy Maps: 10-Minute User Personas

    That’s where the empathy map comes in. When created correctly, empathy maps serve as the perfect lea ...

  6. Windows 10 新特性 -- Bing Maps 3D地图开发入门(一)

    本文主要内容是讲述如何创建基于 Windows Universal App 的Windows 10 3D地图应用,涉及的Windows 10新特性包括 Bing Maps 控件.Compiled da ...

  7. Google Maps API V3 之绘图库 信息窗口

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  8. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  9. Google Maps API V3 之 路线服务

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

随机推荐

  1. [JS] save txt file

    (function () { var blob = new Blob(['content'], {type: 'text/plain; charset=utf-8'}), blobUrl = URL. ...

  2. statistic学习笔记

    1. 假设检验:就是对于符合一定前提条件的数据,先作一个假设H0,还有一个备择假设H1(一般是H0的反面,或者是H0不包含的情况),通过一定的计算公式,算出一个值(比如开方检验就是开方值),这个值的理 ...

  3. linux网络编程常用头文件

    sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arpa/inet.h:提供IP地址转换函 ...

  4. 疯狂学习java web5(SSI框架)

    其实前面的所有只是铺垫,目的只是为了了解现有工程是怎样的.之前直接上来就看,看了很久依然是云里雾里,所以不得已学习点基础知识,前面的基础只是蜻蜓点水一带而过,希望能起部分作用了. 发现struct2有 ...

  5. Java学习----运算符与表达式

    一.运算符 1.算术运算符 +   -   *   /  %  ++  -- public class Test7 { public static void main(String[] args) { ...

  6. 34 Search for a Range(目标数的范围Medium)

    题目意思:递增数组,找到目标数的范围,找不到则返回[-1,-1] 思路:折半查找 class Solution { public: vector<int> searchRange(vect ...

  7. 一元云购完整源码 云购CMS系统 带安卓和ios手机客户端

    看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己反编译.    这里不做功能和其它更多的介绍,可以自己下载后慢慢测试了解.    下面演示图为亲测截图< ...

  8. ACCESS中类型操作(限制、转换)

    ACCESS如何保留两位小数 1.可以通过修改表结构中字段的“小数位数”即可. 2.可以通过“更新查询”,将所有该字段的值更新为round(字段名,2) ACCESS如何转换类型 每个函数都可以强制将 ...

  9. 把 图片 资源文件 编译到dll

    今天盘古 lucene的改了下.然后 里面有很多文件 . 还有一些 生成多音字的 汉语词典等. 索性一下子编译到dll里面 . 就不在项目里面设置 这些文件的目录了 然后找了下.愣是没找到. 后来发现 ...

  10. Visual studio code (vscode)

    调东西 : 左上角 File -> Preferences -> Workspace Settings ( User Settings 也可以, 它是 for 所有的 project, W ...