1103 - Ancient Messages

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.

\epsfbox{p5130a.eps}

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 pixels10011100 (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 WH (0 < H$ \le$200) is the number of scan lines in the image. W (0 < W$ \le$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.


$\textstyle \parbox{.5\textwidth}{\begin{center}\mbox{}\epsfbox{p5130b.eps}\parFigure C.2: AKW\end{center}}$$\textstyle \parbox{.49\textwidth}{\begin{center}\mbox{}\epsfbox{p5130c.eps}\parFigure C.3: AAAAA\end{center}}$

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




#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
#define PB push_back
int N,M,A[209][209];
bool vis[209][209];
int belong[209][209];
int b[209*209];

void Init()
{
	for(int i=0;i<=N+1;i++)
		for(int j=0;j<=M*4+1;j++)
			A[i][j]=0;
	for(int i=1;i<=N;i++)
		for(int j=1,jj=1;j<=M;j++,jj+=4)
		{
			char r;
			cin>>r;
			int x;
			if(r>='a'&&r<='z')
				x=10+r-'a';
			else
				x=r-'0';
			A[i][jj]=(x/8)%2;
			A[i][jj+1]=(x/4)%2;
			A[i][jj+2]=(x/2)%2;
			A[i][jj+3]=(x/1)%2;
		}
	M*=4;
}

void Dfs0(int x,int y,int c)
{
	if(x<0||y<0||x>N+1||y>M+1)
		return ;
	if(vis[x][y])
		return ;
	if(A[x][y]!=0)
		return;
	vis[x][y]=true;
	belong[x][y]=c;
	Dfs0(x+1,y,c);
	Dfs0(x-1,y,c);
	Dfs0(x,y+1,c);
	Dfs0(x,y-1,c);
}

int Dfs1(int x,int y,int vt)
{
	int ret=0;
	if(x<0||y<0||x>N+1||y>M+1)
		return 0;
	if(vis[x][y])
		return 0;
	if(A[x][y]!=1)
	{
		if(b[belong[x][y]]!=vt)
			ret++;
		b[belong[x][y]]=vt;
		return ret;
	}
	vis[x][y]=true;
	ret+=Dfs1(x+1,y,vt);
	ret+=Dfs1(x-1,y,vt);
	ret+=Dfs1(x,y+1,vt);
	ret+=Dfs1(x,y-1,vt);
	return ret;
}

void Solve()
{
	for(int i=0;i<=N+1;i++)
		for(int j=0;j<=M+1;j++)
			vis[i][j]=false;
	int SpaceNum=0;
	for(int i=0;i<=N+1;i++)
		for(int j=0;j<=M+1;j++)
			if(A[i][j]==0)
				Dfs0(i,j,++SpaceNum);
	for(int i=0;i<=N+1;i++)
		for(int j=0;j<=M+1;j++)
			vis[i][j]=false;
	vector<char> Answer;
	Answer.clear();
	int vt=0;
	for(int i=1;i<=SpaceNum;i++)
		b[i]=-1;
	for(int i=0;i<=N+1;i++)
		for(int j=0;j<=M+1;j++)
			if(A[i][j]==1)
			{
				int t=Dfs1(i,j,vt++);
				if(t==1)
					Answer.PB('W');
				if(t==2)
					Answer.PB('A');
				if(t==3)
					Answer.PB('K');
				if(t==4)
					Answer.PB('J');
				if(t==5)
					Answer.PB('S');
				if(t==6)
					Answer.PB('D');
			}
	sort(Answer.begin(),Answer.end());
	for(int i=0;i<Answer.size();i++)
		cout<<Answer[i];
	cout<<"\n";
}


int main()
{
	int Case=0;
	while(cin>>N>>M&&(N||M))
	{
		Init();
		cout<<"Case "<<++Case<<": ";
		Solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值