UVa 1103 Ancient Messages(二重深搜)

时间:2023-03-08 16:17:39

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.
UVa 1103 Ancient Messages(二重深搜)
Input
The input consists of several test cases, each of which describes an image containing one or morehieroglyphs chosen from among those shown in Figure C.1. The image is given in the form of a seriesof horizontal scan lines consisting of black pixels (represented by 1) and white pixels (represented by0). In the input data, each scan line is encoded in hexadecimal notation. For example, the sequence ofeight pixels 10011100 (one black pixel, followed by two white pixels, and so on) would be represented inhexadecimal notation as 9c. Only digits and lowercase letters a through f are used in the hexadecimalencoding. The first line of each test case contains two integers, H and W. H (0 < H ≤ 200) is thenumber 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:
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.
UVa 1103 Ancient Messages(二重深搜)
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
000000000000000000000000000000000000

00000000000000000000000000000000000000
0 0
Sample Output
Case 1: AKW
Case 2: AAAAA

题意

给你n行m列十六进制字符(0-f)每个字符等于4个二进制数(1代表黑点,0代表白点),再给你6个图,请按字典序输出所有符号

题解

首先找6个图怎么变都不会变的条件,黑点中间的白洞数量分别为(1,3,5,4,0,2)

问题就在于怎么找符号内的白洞

dfs()用于清除白点0变成2,dfs1()用于找黑点

1.首先根据输入,所有符号不会接触,也就是说,符号外的白点属于多余的白点得去掉

  这时候就需要在图的最外一圈填上0,连通所有多余的白点,dfs(0,0)

2.然后根据输入,每个符号都是1个四连块,也就是说,符号是连通的,这样只需要dfs1(黑点)

  如果是1,继续搜

  如果是0,白洞+1,dfs(这个点)

代码

 #include<bits/stdc++.h>
using namespace std;
char s[+][*+];
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,cnt;
bool check(int x,int y)//边界
{
if(x>=&&x<=n+&&y>=&&y<=m*+)return true;
return false;
}
int dfs(int x,int y)//清除白点
{ if(s[x][y]=='')return ;
s[x][y]='';
for(int i=;i<;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(check(xx,yy)&&s[xx][yy]=='')
dfs(xx,yy);
}
return ;
}
int dfs1(int x,int y)//搜索黑点
{
if(s[x][y]=='')return ;
s[x][y]='';
for(int i=;i<;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(check(xx,yy))
{
if(s[xx][yy]=='')
dfs1(xx,yy);
if(s[xx][yy]=='')
cnt++,dfs(xx,yy);
}
}
return ;
}
int main()
{
int o=;
char ch;
map<char,string> ma;
ma['']="";ma['']="";ma['']="";ma['']="";ma['']="";
ma['']="";ma['']="";ma['']="";ma['']="";ma['']="";
ma['a']="";ma['b']="";ma['c']="";ma['d']="";ma['e']="";
ma['f']="";
map<int,char> mb;
mb[]='W';mb[]='A';mb[]='K';mb[]='J';mb[]='S';mb[]='D';
while(scanf("%d %d",&n,&m)!=EOF,n||m)
{
getchar();
memset(s,'',sizeof(s));//填0
for(int i=;i<=n;i++)
{
for(int j=;j<m;j++)
{
scanf("%c",&ch);
s[i][j*+]=ma[ch][];
s[i][j*+]=ma[ch][];
s[i][j*+]=ma[ch][];
s[i][j*+]=ma[ch][];
}
getchar();
}
dfs(,);
vector<char> vec;
for(int i=;i<=n+;i++)
{
for(int j=;j<=m*+;j++)
{
if(s[i][j]=='')
{
cnt=;
dfs1(i,j);
vec.push_back(mb[cnt]);
}
}
}
sort(vec.begin(),vec.end());
printf("Case %d: ",o++);
for(int i=;i<vec.size();i++)
printf("%c",vec[i]);
printf("\n");
}
return ;
}