HDU-3839-Ancient messages (DFS)

Source: Internet
Author: User
Problem descriptionin order to understand early civilizations, archaeologists often study texts written in each ent ages. 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.
 
Inputthe 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 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) wocould 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.
Outputfor 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:
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 2500000000000000000000000000000000000000000000000000 ...(50 lines omitted)...00001fe0000000000007c000000003fe0000000000007c0000 ...(44 lines omitted)...00000000000000000000000000000000000000000000000000150 380000000000000000000000000000000000000000000000000000000000000000000000000000    ...(75 lines omitted)...0000000003fffffffffffffffff000000000000000000003fffffffffffffffff00000000000    ...(69 lines omitted)...00000000000000000000000000000000000000000000000000000000000000000000000000000 0
 
Sample output
Case 1: AKWCase 2: AAAAA
 
Source2011worldfinal


Idea: Identify based on the number of circles.


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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.