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

    Input
    The input consists of several test cases, each of which describes an image containing one or morehieroglyphs chosen from among those shown in Figure C.1. The image is given in the form of a seriesof horizontal scan lines consisting of black pixels (represented by 1) and white pixels (represented by0). In the input data, each scan line is encoded in hexadecimal notation. For example, the sequence ofeight pixels 10011100 (one black pixel, followed by two white pixels, and so on) would be represented inhexadecimal notation as 9c. Only digits and lowercase letters a through f are used in the hexadecimalencoding. The first line of each test case contains two integers, H and W. H (0 < H ≤ 200) is thenumber 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.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.

    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
    000000000000000000000000000000000000

    00000000000000000000000000000000000000
    0 0
    Sample Output
    Case 1: AKW
    Case 2: AAAAA

    题意

    给你n行m列十六进制字符(0-f)每个字符等于4个二进制数(1代表黑点,0代表白点),再给你6个图,请按字典序输出所有符号

    题解

    首先找6个图怎么变都不会变的条件,黑点中间的白洞数量分别为(1,3,5,4,0,2)

    问题就在于怎么找符号内的白洞

    dfs()用于清除白点0变成2,dfs1()用于找黑点

    1.首先根据输入,所有符号不会接触,也就是说,符号外的白点属于多余的白点得去掉

      这时候就需要在图的最外一圈填上0,连通所有多余的白点,dfs(0,0)

    2.然后根据输入,每个符号都是1个四连块,也就是说,符号是连通的,这样只需要dfs1(黑点)

      如果是1,继续搜

      如果是0,白洞+1,dfs(这个点)

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 char s[200+5][50*4+5];
     4 int dx[]={0,0,1,-1};
     5 int dy[]={1,-1,0,0};
     6 int n,m,cnt;
     7 bool check(int x,int y)//边界
     8 {
     9     if(x>=0&&x<=n+1&&y>=0&&y<=m*4+1)return true;
    10     return false;
    11 }
    12 int dfs(int x,int y)//清除白点
    13 {
    14 
    15     if(s[x][y]=='2')return 0;
    16     s[x][y]='2';
    17     for(int i=0;i<4;i++)
    18     {
    19         int xx=x+dx[i];
    20         int yy=y+dy[i];
    21         if(check(xx,yy)&&s[xx][yy]=='0')
    22             dfs(xx,yy);
    23     }
    24     return 1;
    25 }
    26 int dfs1(int x,int y)//搜索黑点
    27 {
    28     if(s[x][y]=='2')return 0;
    29     s[x][y]='2';
    30     for(int i=0;i<4;i++)
    31     {
    32         int xx=x+dx[i];
    33         int yy=y+dy[i];
    34         if(check(xx,yy))
    35         {
    36             if(s[xx][yy]=='1')
    37                 dfs1(xx,yy);
    38             if(s[xx][yy]=='0')
    39                 cnt++,dfs(xx,yy);
    40         }
    41     }
    42     return 1;
    43 }
    44 int main()
    45 {
    46     int o=1;
    47     char ch;
    48     map<char,string> ma;
    49     ma['0']="0000";ma['1']="0001";ma['2']="0010";ma['3']="0011";ma['4']="0100";
    50     ma['5']="0101";ma['6']="0110";ma['7']="0111";ma['8']="1000";ma['9']="1001";
    51     ma['a']="1010";ma['b']="1011";ma['c']="1100";ma['d']="1101";ma['e']="1110";
    52     ma['f']="1111";
    53     map<int,char> mb;
    54     mb[0]='W';mb[1]='A';mb[2]='K';mb[3]='J';mb[4]='S';mb[5]='D';
    55     while(scanf("%d %d",&n,&m)!=EOF,n||m)
    56     {
    57         getchar();
    58         memset(s,'0',sizeof(s));//填0
    59         for(int i=1;i<=n;i++)
    60         {
    61             for(int j=0;j<m;j++)
    62             {
    63                 scanf("%c",&ch);
    64                 s[i][j*4+1]=ma[ch][0];
    65                 s[i][j*4+2]=ma[ch][1];
    66                 s[i][j*4+3]=ma[ch][2];
    67                 s[i][j*4+4]=ma[ch][3];
    68             }
    69             getchar();
    70         }
    71         dfs(0,0);
    72         vector<char> vec;
    73         for(int i=0;i<=n+1;i++)
    74         {
    75             for(int j=0;j<=m*4+1;j++)
    76             {
    77                 if(s[i][j]=='1')
    78                 {
    79                     cnt=0;
    80                     dfs1(i,j);
    81                     vec.push_back(mb[cnt]);
    82                 }
    83             }
    84         }
    85         sort(vec.begin(),vec.end());
    86         printf("Case %d: ",o++);
    87         for(int i=0;i<vec.size();i++)
    88             printf("%c",vec[i]);
    89         printf("
    ");
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    【java编程】使用System.getProperty方法,如何配置JVM系统属性
    【java多线程】CountDownLatch
    【java多线程】ConcurrentLinkedHashMap
    【java编程】ServiceLoader使用看这一篇就够了
    【JVM】java对象
    【git】Git常用命令
    centos7安装配置mysql5.7
    第十三章 redis-cluster原理
    《mysql技术内幕 InnoDB存储引擎(第二版)》阅读笔记
    第二章 BIO与NIO
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8469231.html
Copyright © 2020-2023  润新知