HDU-3839-Ancient Messages(DFS)

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

2011WorldFinal

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

#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)

时间: 2024-10-31 00:29:25

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

K - Ancient Messages(dfs求联通块)

K - Ancient Messages Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1103 Appoint description: Description In order to understand early civilizations, archaeologists often study texts written in ancie

UVA 1103 Ancient Messages (DFS)

起初学习dfs的时候 看不懂这个题目  回过头来今天看的时候思路相对比较清晰了.但还是想不到怎么处理 查询有多少个空白洞.并且一个图中还有好多个象形字符.网上思路是 在周围扩展一圈0,使得文字之外的所有0 都连通,一次dfs标记所有文字之外的0为-1.然后遍历图,发现1就沿着有1 的路径dfs2,在这个过程中,如果发现0,那么必然是文字内部的空洞,此时把空洞dfs 置为-1,标记以后不再走.那么发现几次0就有几个空洞在文字中.那么每一次的dfs2,就处理了一个象形文字.cnt的值就是空洞的个数即

HDU 5546 Ancient Go DFS

Ancient Go Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1732    Accepted Submission(s): 552 Problem Description Yu Zhou likes to play Go with Su Lu. From the historical research, we found tha

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

uva 1103 - Ancient Messages(象!形!文!字! dfs遍历计数)

我今天做的这叫什么题-- 今天这个题直接跪了,一看十六进制直接懵了.. 然后在csdn上竟然发现了身边直系学长写的解题报告,然后问了一下解题的思路.然后写出来的代码,想要测试数据吗吧哈哈 给一组最基本的~ 5 3 fff f0f fff f0f fff 输出应该是K AC代码如下: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace

hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci 问题描述 斐波那契数列的递归定义如下: Fi=???01Fi?1+Fi?2i = 0i = 1i > 1 现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积. 输入描述 有多组数据,第一行为数据组数T(T≤100,000). 对于每组数据有一个整数n,表示要判断的数字. 0≤n≤1,000,000,000 输出描述 对于每组数据,如果可以输出"Yes",否则输出"No". 输入样例 3 4 17 233 输出样例

HDU 3158 PropBot(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3158 Problem Description You have been selected to write the navigation module for PropBot. Unfortunately, the mechanical engineers have not provided a lot of flexibility in movement; indeed, the PropBot

hdu 4499 Cannon 暴力dfs搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 338 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move