(HDU3839)Ancient Messages-dfs-连通块

Ancient Messages

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 999    Accepted Submission(s): 322


 

Problem 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.

(HDU3839)Ancient Messages-dfs-连通块

 

 

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 < 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:

  • 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.11.


     

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

1Two figures are topologically equivalent if each can be transformed into the other by stretching without tearing.

 

 

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.

(HDU3839)Ancient Messages-dfs-连通块

 

 

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

 

 

 

题目大意:给定一个压缩了的图片,问你图片上有哪些图案。

分析:通过观察图案的特点发现不同图案的内部白色连通块不同,所以可以通过求图案中白色联通块的个数来确定是哪个图案

,求连通块就dfs就可以了,但是怎么确定该白色的块是图片内的?首先先将图案外的白色做标记,为了一次就能将各个图案外的白色做标记,所以给图片加一个白边,这样可以使外面的白色块连通起来,然后去对图案做dfs,在向四周dfs时若遇到白色的块且没被标记,就说明该块是图案内的块,然后对该白色块dfs,cnt++,就可以的到图案内的白色连通块的个数,就能确定是哪个图案。

代码:

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string.h>
using namespace std;
char res[] = { 'A','D','J','K','S','W' };
char str[] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
int s[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 num[6];
int M[250][250], n, m, cnt;

bool in_M(int x, int y)
{
	return (x >= 0 && x <= n+1&&y >= 0 && y <= m + 1);
}
//求联通的空白块
void dfs(int x,int y)
{
	if (!in_M(x, y) || M[x][y] != 0)return;
	M[x][y] = -1;
	dfs(x + 1, y); dfs(x - 1, y); dfs(x, y + 1); dfs(x, y - 1);
}
//
void dfs2(int x, int y)
{
	if (M[x][y] == -1 || !in_M(x, y))return;
	//图案中的白色联通块
	if (M[x][y] == 0)
	{
		//白色联通块的个数
		cnt++;
		dfs(x, y);
		return;
	}
	M[x][y] = -1;
	//黑边
	dfs2(x + 1, y); dfs2(x - 1, y); dfs2(x, y + 1); dfs2(x, y - 1);
}
int main() {
	int t = 0;
	while(cin>>n>>m&&n&&m)
	{ 
		memset(M, 0, sizeof(M));
		for (int i = 1; i <= n; i++)
		{
			char tem[80];
			cin >> tem;
			int p = 1;
			//对于压缩的图片解码
			for (int j = 0; j < m; j++)
			{
				for (int k = 0; k < 16; k++)
				{
					if (tem[j] == str[k])
					{
						for (int l = 0; l < 4; l++)
						{
							M[i][p++] = s[k][l];
						}
						break;
					}
				}
			}
		}
	
		memset(num, 0, sizeof(num));
		m *= 4;
		dfs(0, 0);
		for (int i = 0; i <= n; i++)
		{
			for (int j = 0; j <= m; j++)
			{
				if (M[i][j] == 1)
				{
					cnt = 0;
					dfs2(i, j);
					if (cnt == 0)num[5]++;
					else if (cnt == 1)num[0]++;
					else if (cnt == 2)num[3]++;
					else if (cnt == 3)num[2]++;
					else if (cnt == 4)num[4]++;
					else if (cnt == 5)num[1]++;
				}
			}
		}
		cout << "Case " << ++t << ": ";
		for (int i = 0; i < 6; i++)
		{
			for (int j = 0; j < num[i]; j++)
			{
				cout << res[i];
			}
		}
		cout << endl;
	}
	return 0;
}