Description
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.
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 first line of each test case contains two integers, H and W. H(0 < H200) is the number of scan lines in the image. W(0 < W50) 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:
- The image contains only hieroglyphs shown in Figure C.1.
- Each image contains at least one valid hieroglyph.
- Each black pixel in the image is part of a valid hieroglyph.
- 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.
- The hieroglyphs do not touch and no hieroglyph is inside another hieroglyph.
- Two black pixels that touch diagonally will always have a common touching black pixel.
- The hieroglyphs may be distorted but each has a shape that is topologically equivalent to one of the symbols in Figure C.1. (Two figures 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.
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
前段时间家里开CSDN一直有问题 做的题就没写题解 今天突然好了 = = 呃 这题就是先把边界处理 不过为了防止边界被隔断的情况的发生。。可以将图形从(1,1)开始放 找洞也是 DFS 然后字符转二进制的话直接打表就行了
#include <bits/stdc++.h> using namespace::std; char ch[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; int t[16][4] = {{0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},{0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1},{1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1},{1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}}; int ma[300][300],m,n,cnt; void dfs (int x,int y) { if(x<0||y<0||x>m+1||y>n+1||ma[x][y]!=0)//图形边界有可能被图形隔开 所以查找至边界值+1 return; ma[x][y]=-1; dfs(x+1,y); dfs(x-1,y); dfs(x,y-1); dfs(x,y+1); } void DFS(int x,int y) { if(x<0||y<0||x>m+1||y>n+1||ma[x][y]==-1) //超出图形边界 return; if(ma[x][y]==0) { cnt++; dfs(x,y); } ma[x][y]=-1; DFS(x+1,y); DFS(x-1,y); DFS(x,y-1); DFS(x,y+1); } int main() { //freopen("in.txt","r",stdin); int cases=0; char s[55]; while(~scanf("%d%d",&m,&n)&&(m||n)) { memset(ma,0,sizeof(ma)); string ans; cnt=0; for(int i=1;i<=m;i++) { scanf("%s",s); int len=strlen(s),pos=1; for(int j=0;j<len;j++) { //将字符转换成二进制; for(int k=0;k<16;k++) { if(s[j]==ch[k]) { for(int o=0;o<4;o++) ma[i][pos++]=t[k][o]; break; } } } } n*=4; printf("Case %d: ",++cases); for(int i=0;i<=m;i++) //找到第一个边界像素; for(int j=0;j<=n;j++) if(ma[i][j]==0) { dfs(i,j);//将所有边界标记为-1; goto loop; } loop: for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(ma[i][j]==1) { //找到图形 cnt=0,DFS(i,j); if(cnt==0) ans.push_back('W'); else if(cnt==1) ans.push_back('A'); else if(cnt==2) ans.push_back('K'); else if(cnt==3) ans.push_back('J'); else if(cnt==4) ans.push_back('S'); else if(cnt==5) ans.push_back('D'); } } } sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++) printf("%c",ans[i]); printf("\n"); } return 0; }
至于测试数据么 我在百度找到了一组
5 3 fff f0f fff f0f fff 答案是K 不过这组数据过了 程序还是有极大的可能不对啊 然后我又想了几组
5 5 0fff0 0f0f0 fffff 00f00 00f00 这个答案是A
5 5 0f0f0 0f0f0 fffff 00f00 00f00 这个是当时不小心画错的 虽然形状不对啊不过没什么影响 因为没有封闭的地方所以答案是W