UVA

来源:互联网 发布:民间小调软件下载 编辑:程序博客网 时间:2024/05/29 17:36

In order to understand early civilizations, archaeologists often study texts written in ancient languages. One such language, used in Egypt more than 3000 years ago, is based on characters called hieroglyphs. Figure C.1 shows six hieroglyphs and their names. In this problem, you will write a program to recognize these six characters.


Figure C.1: Six hieroglyphs

Input

The input consists of several test cases, each of which describes an image containing one or more hieroglyphs chosen from among those shown in Figure C.1. The image is given in the form of a series of horizontal scan lines consisting of black pixels (represented by 1) and white pixels (represented by 0). In the input data, each scan line is encoded in hexadecimal notation. For example, the sequence of eight pixels 10011100 (one black pixel, followed by two white pixels, and so on) would be represented in hexadecimal notation as 9c. Only digits and lowercase letters a through f are used in the hexadecimal encoding. The rst line of each test case contains two integers, H and W. H (0 < H 200) is the number of scan lines in the image. W (0 < W 50) is the number of hexadecimal characters in each line. The next H lines contain the hexadecimal characters of the image, working from top to bottom. Input images conform to the following rules:

 

 

l The image contains only hieroglyphs shown in Figure C.1.

l Each image contains at least one valid hieroglyph.

l Each black pixel in the image is part of a valid hieroglyph.

l Each hieroglyph consists of a connected set of black pixels and each black pixel has at least one other black pixel on its top, bottom, left, or right side.

l The hieroglyphs do not touch and no hieroglyph is inside another hieroglyph.

l Two black pixels that touch diagonally will always have a common touching black pixel.

l The hieroglyphs may be distorted but each has a shape that is topologically equivalent to one of the symbols in Figure C.1. (Two gures are topologically equivalent if each can be transformed into the other by stretching without tearing.)

 

The last test case is followed by a line containing two zeros.

Output

For each test case, display its case number followed by a string containing one character for each hieroglyph recognized in the image, using the following code:

 

Ankh: A

Wedjat: J

Djed: D

Scarab: S

Was: W

Akhet: K

 

In each output string, print the codes in alphabetic order. Follow the format of the sample output. The sample input contains descriptions of test cases shown in Figures C.2 and C.3. Due to space constraints not all of the sample input can be shown on this page.


Figure C.2: AKW Figure C.3: AAAAA

Sample Input

100 25

0000000000000000000000000

0000000000000000000000000

...(50 lines omitted)...

00001fe0000000000007c0000

00003fe0000000000007c0000

...(44 lines omitted)...

0000000000000000000000000

0000000000000000000000000

150 38

00000000000000000000000000000000000000

00000000000000000000000000000000000000

...(75 lines omitted)...

0000000003fffffffffffffffff00000000000

0000000003fffffffffffffffff00000000000

...(69 lines omitted)...

00000000000000000000000000000000000000

00000000000000000000000000000000000000

0 0

Sample Output

Case 1: AKW

Case 2: AAAAA

Test Data

6 2007c447c30006 250000000000000000000000000000000000000000000000000000001fe0000000000007c000000003fe0000000000007c00000000000000000000000000000000000000000000000000000010 30007785487485787000007f01e000016 2007e427e427e427e427e427e00004a0016 10123456789abcdef9 2007e427e427e427e0043 2007e007e427e427e427e007e427e007e427e427e007e427e427e427e427e007e427e427e427e427e427e00200 500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000100 2500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f800000000000000000000001fe00000000000000000000007ff00000000000000000000007ff8000000000000000000000f8f8000000000000000000001f07c000000000000000000001e03c000000000018000000001e01c00000000003c000000001c01c00000000007c000000003c01e0000000000f8000000003c01e0000000001f0000000001c01c0000000003f0000000001c01c0000000007e0000000001e01c000000000fc0000000001e03c000000001fc0000000000e03c000000001fc0000000000f038000000003ff0000000000f078000000003ff80000000007870000000007ff800000000038f0000000007cfc0000000003ce0000000007c7c0000000781fc0f0000000f87c00000007ffffff0000000f07c00000007ffffff0000000f07c00000007ffffff0000001f07c00000007ffffff0000000e03e00000007fcf81f0000000603e0000000000f8000000000003e0000000000f8000000000003e0000000000f8000000000003e0000000000f8000000000001e0000000000f8000000000001f0000000000fc000000000001f0000000000fc000000000001f0000000000fc000000000001f0000000000fc000000000000f0000000001fc000000000000f8000000001fc000000000000f8000000001fc000000000000f8000000001fc000000000000f8000000001fe000000000000f8000000001fe00000000000078000000001fe0000000000007c000000001fe0000000000007c000000003fe0000000000007c000000003fe0000000000007c000000003fe0000000000007c000000003fe0000c00000003c000000000000003ff0000003c000000000000007ff8000003e00000000000001fffc000003e00000000000003e03f000003e00000000000007c00f000003e0000000000000f0003800003f0000000000000e0001c00003fc000000000001c0001e00007fe000000000003c0000e0000fff000000000073c000070000fdf0000000001ff8000070001f0f8000000001ff8000070001e078000000003cf0000078001e038000000003870000033001e03800000000307800003fc01e03800000000703800007fe00e03800000000703800007ce00e03800000000703c000078700703800000000701e0000f0700701000000000701e0000e0700300000000000700f0001c07000000000000006007800380600000000000000e003e00700600000000000000e001fe7e00600000000000000e000fffc00e00000000000000e0000ff000e00000000000000f800038000e00000000000000fff0000000e00000000000000fffff00000e0000000000000003ffffe000c000000000000000007ffffc0c00000000000000000007ffffc000000000000000000000fffc00000000000000000000001fc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000150 3800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000f80000000001fff00000000780000000000001fe0000000007fff80000000ff0000000000007ff000000000ffffe0000001ff8000000000007ff800000003fffff0000001fffc0000000000f8f800000007fffffc000001fffe0000000001f07c0000000ffffffe000000ffff0000000001e03c0000001fffffff000000ffff8000000001e01c0000003fffffff0000007fffc000000001c01c0000003fffffff8000007fefc000000003c01e0000007fffffffc000003f83c000000003c01e000000ffffffffc000001f81e000000001c01c000000fffc0fffe000001f01e000000001c01c000001fff003ffe000000f01e000000001e01c000001ffe001fff000000f00e000000001e03c000003ffc0007ff000001e00f000000000e03c000003ff80007ff800001e00f000000000f038000007ff80003ff800001e00f000000000f078000007ff00003ff800001e00f0000000007870000007ff00001ffc00000e00e00000000038f000000fff00001ffc00000e00e0000000003ce000000ffe00000ffc00000e00e0000000781fc0f0000ffe00000ffc00000f00e00000007ffffff0000ffc00000ffc00000f01e00000007ffffff0000ffc00000ffc00000f01e00000007ffffff0000ffc00000ffc00000701c00000007ffffff0000ffc00000ffc00000781c00000007fcf81f0000ffc000007fc00000783c0000000000f8000000ffc000007fc0000038380000000000f8000000ffc000007fc000003c780000000000f8000000ffc000007fc000001c780000000000f8000000ffc000007fc000001e700000000000f8000000ffc000007fc000200ef00800000000fc000000ffc00000ffc0003f8fe3f800000000fc000000ffc00000ffc0003ffffff800000000fc000000ffc00000ffc0003ffffff800000000fc000000ffc00000ffc0003ffffff800000001fc000000ffc00000ffc0003ffffff800000001fc0000007fe00000ff80003ffffff800000001fc0000007fe00000ff80003fffdff800000001fc0000007fe00000ff80003c03c00000000001fe0000007ff00001ff80000007c00000000001fe0000003ff00001ff00000007c00000000001fe0000003ff00001ff00000007c00000000001fe0000001ff80003ff00000007c00000000003fe0000001ff80003fe00000007c00000000003fe0000001ff80003fe00000007c00000000003fe0000000ffc0007fe00000007c00000000003fe0000000ffc0007fc00000007c000000000000000000007fe0007fc00000007c000000000000000000007fe000ff800000007c000000000000000000007ff001ff800000007c000000000000000000003ff001ff800000007c000000000000000000001ff803ff000000007c002000000000000000001ff803ff000000007c006000000000000000000ffc07fe000000007c006000000000000000000ffc0ffc000000007c00c000000000000000000ffe0ffc000000003e01c0000000000000000007ff0ff8000000003f03c0000000000000000003ff1ff0000000003f8f80000000000003c00001ffbff00000f0001fff80000000000003ffc001ffffe0007ff0000fff80000000000003fffff1ffffe3fffff00007ff80000000000003fffffffffffffffff00001ff00000000000003fffffffffffffffff00000ff00000000000003fffffffffffffffff000007f00000000000003fffffffffffffffff000001e00000000000003fffffffffffffffff000000e00000000000003fffffffffffffffff000000200000000000003fffffffffffffffff000000000000000000003fffffffffffffffff000000000000000000003fffffffffffffffff000000000000000000003fffffffffffffffff000000000000000000003fffffffffffffffff000000000000000000003fffffffffffffffff000000000000000000003fffffc1ffe0007fff000000000000000000003ff80000ffe000000f0000000000000000000038000000ffe00000000000000000000000000000000001fff00000000000000000000000000000000001fff00000000000000000000000000000000001fff00000000000000000000000000000000001fff00000000000000000000000000000000001fff00000000000000000000000000000000001fff00000000000000000000000000000000003fff00000000000000000000000000000000003fff00000000000000000000000000000000003fff0000000fc000000000000000000fe000003fff8000003ff000000000000000003ffc00003fff8000007ffc0000000000000000fffe00003fff800000fcfc0000000000000001f01f00003fff800001f03e0000000000000003e00f80003fff800003e01f0000000000000003e00780003fff800003e00f0000000000000003e00780003fff800003c00f0000000000000003e00f80003fff800003c00f0000000000000001f00f00007fff800003c00f0000000000000000f81e00007fffc00003e00f00000000000000007c3c00007fffc00001e01e00000000000000003e7800007fffc00000f01e00000000000000fffffffe007fffc00000f03c00000000000000fffffffe007fffc00000787800000000000000fffffffe007fffc000003cf0000000000000000007c000007fffe000f81fe07c0000000000000007e000007fffe000fffffffc0000000000000007e000007fffe000fffffffc000000000000000fe000007fffe000fffffffc000000000000000ff00000ffffe000ffc7c0fc000000000000000ff00000ffffe0000007c000000000000000001ff00000ffffe000000fc000000000000000001ff00000ffffe000000fc000000000000000001ff80000ffffe000000fc000000000000000001ff80000ffffe000000fc000000000000000003ff80001ffffe000000fe000000000000000003ff80001ffffe000000fe0000000000000000000000001fffff000001fe0000000000000000000000001fffff000001fe0000000000000000000000001fffff000001fe0000000000000000000000001fffff000001ff0000000000000000000000001fffff000001ff0000000000000000000000001fffff000001ff0000000000000000000000001fffff000003ff0000000000000000000000001fffff000003ff0000000000000000000000001fffff000003ff0000000000000000000000001fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffff800000000000000000000000000000003fffffc00000000000000000000000000000003fffffc00000000000000000000000000000007fffffc00000000000000000000000000000007fffffc00000000000000000000000000000007fffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0

Sample Code

#include<cstdio>#include<cstring>using namespace std;const int maxn=200+10;char arr[maxn][maxn];int h,w;char hex[17]="0123456789abcdef";char bin[16][5]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};char ch[7]="ADJKSW";int hole[6]={1,5,3,2,4,0};int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};int ans[maxn],num;int find_ch(char *str,char ch){for(int i=0;i<strlen(str);i++)if(str[i]==ch)return i;} void dfs(int x,int y,bool enable){arr[x][y]='@';for(int i=0;i<4;i++){int nx=x+dx[i],ny=y+dy[i];if(nx>=0 && nx<=h+1 && ny>=0 && ny<=w+1){if(enable==false && arr[nx][ny]=='0')dfs(nx,ny,enable);if(enable==true){if(arr[nx][ny]=='1')dfs(nx,ny,true);if(arr[nx][ny]=='0'){dfs(nx,ny,false);ans[num]+=1;}}}}}int main(){int kase=0;while(scanf("%d%d",&h,&w)==2 && h){memset(arr,0,sizeof(arr));memset(ans,0,sizeof(ans));int i,j;w=4*w;//宽度变为原来的4倍//为区域设置一个边界,则区域范围为[0,w+1][0,h+1] for(i=0;i<=h+1;i++)arr[i][0]='0';for(j=0;j<=w+1;j++){arr[0][j]='0';arr[h+1][j]='0'; }char str[maxn];for(i=1;i<=h;i++){scanf("%s",str);for(j=0;j<strlen(str);j++){int pos=find_ch(hex,str[j]);strcat(arr[i],bin[pos]);}arr[i][w+1]='0';} dfs(0,0,false);//设置符号外的背景 num=0;for(i=1;i<=h;i++){for(j=1;j<=w;j++){if(arr[i][j]=='1'){dfs(i,j,true);num++;}} }printf("Case %d: ",++kase);for(i=0;i<6;i++)for(j=0;j<num;j++)if(hole[i]==ans[j])printf("%c",ch[i]);printf("\n");}return 0;}