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.

 
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.

 
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
 
Source

思路:依据圈的数量来识别。

#include <cstdio>
#include <algorithm>
using namespace std; char ts[201],mes[6]={'W','A','K','J','S','D'},ans[10];
bool vis[205][205];
int n,m,mp[205][205],nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}},num; void dfs(int x,int y)
{
int i; for(i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && !mp[x][y])
{
vis[x][y]=1;
dfs(x,y);
} x-=nxt[i][0];
y-=nxt[i][1];
}
} void dfs3(int x,int y)
{
int i; for(i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && !mp[x][y])
{
vis[x][y]=1;
dfs3(x,y);
} x-=nxt[i][0];
y-=nxt[i][1];
}
} void dfs2(int x,int y)
{
int i; for(i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1]; if(x>=0 && x<n && y>=0 && y<m && !vis[x][y])
{
if(mp[x][y])
{
vis[x][y]=1;
dfs2(x,y);
}
else
{
vis[x][y]=1;
num++;
dfs3(x,y);
} } x-=nxt[i][0];
y-=nxt[i][1];
}
} int main()
{
int i,j,t,casenum=1,cnt; while(~scanf("%d%d",&n,&m) && n)
{
n++;
m*=4;
m++; for(i=0;i<=n;i++) for(j=0;j<=m;j++) vis[i][j]=0; for(i=1;i<n;i++)
{
gets(ts); if(!ts[0])
{
i--;
continue;
} for(j=0;ts[j];j++)
{
if(ts[j]>='a' && ts[j]<='f')
{
t=ts[j]-'a'+10; mp[i][j*4+1]=t/8;
mp[i][j*4+2]=t%8/4;
mp[i][j*4+3]=t%4/2;
mp[i][j*4+4]=t%2/1;
}
else
{
t=ts[j]-'0'; mp[i][j*4+1]=t/8;
mp[i][j*4+2]=t%8/4;
mp[i][j*4+3]=t%4/2;
mp[i][j*4+4]=t%2/1;
}
}
} for(i=0;i<=m;i++) mp[n][i]=0;
for(i=0;i<=n;i++) mp[i][m]=0; n++;
m++; vis[0][0]=1;
dfs(0,0); cnt=0; for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mp[i][j] && !vis[i][j])
{
num=0; vis[i][j]=1; dfs2(i,j); ans[cnt++]=mes[num];
}
}
} sort(ans,ans+cnt); ans[cnt]=0; printf("Case %d: ",casenum++); puts(ans);
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU-3839-Ancient Messages(DFS)的更多相关文章

  1. HDU 3839 Ancient Messages(DFS)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  2. hdu 3839 Ancient Messages (dfs )

    题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...

  3. K - Ancient Messages(dfs求联通块)

    K - Ancient Messages Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  4. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  5. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  6. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  7. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  8. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  9. HDU 1401 Solitaire 双向DFS

    HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...

  10. ACM: HDU 2563 统计问题-DFS+打表

    HDU 2563 统计问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u HDU 2 ...

随机推荐

  1. Eclipse 快捷键大全 分类: C_OHTERS 2014-06-01 13:05 332人阅读 评论(0) 收藏

      精选常用: 1.  ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如a ...

  2. jquery画图插件jPainter

    jquery画图插件jPainter 一.总结 一句话总结:四年前的项目,四年无更新,不好用. 二.基于HTML5 Canvas和jQuery 的画图工具的实现 简介 HTML5 提供了强大的Canv ...

  3. notepad++ 正则替换

    比如原来的代码是 {$id['nihao']}{$id['lal']}{pigcms:$id.leere} 查找目标写 \{\$id\['(\w+)'\]\} 替换为写 \{pigcms:\$id\. ...

  4. 【37.21%】【codeforces 721B】Passwords

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. [Angular] Observable.catch error handling in Angular

    import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...

  6. Net Core 实现谷歌翻译ApI 免费版

    原文:Net Core 实现谷歌翻译ApI 免费版 由于谷歌翻译官方API是付费版本,本着免费和开源的精神.分享一下用 Net Core 实现谷歌翻译API的代码. 项目引用的Nuget 包: Cha ...

  7. 【U205】最大值

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 找到一个数组的最大值的一种方法是从数组开头从前到后对数组进行扫描,令max=a[0](数组下表从0.. ...

  8. 【hdu 3032】Nim or not Nim?

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

  9. KeePass v1.30

    Changes from 1.29 to 1.30: New Features:Refined application icons (thanks to Victor Andreyenkov).Add ...

  10. 快来看看Google出品的Protocol Buffer,别仅仅会用Json和XML了

    前言 习惯用 Json.XML 数据存储格式的你们,相信大多都没听过Protocol Buffer Protocol Buffer 事实上 是 Google出品的一种轻量 & 高效的结构化数据 ...