其他分享
首页 > 其他分享> > Ancient Messages UVA - 1103

Ancient Messages UVA - 1103

作者:互联网

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.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
00000000000000000000000000000000000000
0 0

Sample Output

Case 1: AKW
Case 2: AAAAA

HINT

解题思路就是数每一个图形的洞口数量,使用三层dfs,第一层将每一个图形之外空白处标记,第二层开始查找像素为1的即黑色的,因为每一个图形多有的黑色像素都是相连的,因此一次dfs递归回来就是一个图形的遍历完成,再这个过程中要查出来有多少个洞洞,也是使用dfs,开一下代码就可以了。

注意:使用自己的编译器dfs递归的时候有可能会报错,可能是内存不够了吧,带如果数据量少一点就没问题,在vj上面也没有报错。

Accepted

#include<bits/stdc++.h>
using namespace std;

map<char, string>transfor{ {'0', "0000"}, { '1',"0001" }, { '2',"0010" }, { '3',"0011" }, { '4',"0100" }, { '5',"0101" },{'6',"0110"},{'7',"0111"},
                            {'8',"1000"},{'9',"1001"},{'a',"1010"},{'b',"1011"},{'c',"1100"},{'d',"1101"},{'e',"1110"},{'f',"1111"} };//16进制转化为2进制
char text[6] = { 'W','A','K','J','S','D' };        //映射字符
string photo[250],s1,s2;
int H, W, num = 0, sum = 0;                        //全局变量
void translate(string s, int i) {                //将16进制转化为2进制
    photo[i] += "0000";                         //并加上一圈空白
    s += '0';
    for (int j = 0;j < s.size();j++)
        photo[i] += transfor[s[j]];
}

void dfs(int m, int n, char flag, char ch) {    //m,n是坐标;flag是修改后的标签;ch是目前的标签
    if (m > H + 1 || m<0 || n>4*(W + 2)-1 || n < 0)return;  //越界返回
    if (ch == '1' && photo[m][n] == '0') {                  //如果本来是要查找‘1’而查找到了‘0’则洞数加一并且dfs这个空白洞洞
        sum++;
        dfs(m, n, '3', '0');
        return;
    }
    if (photo[m][n] != ch) return;
    photo[m][n] = flag;                             
    for (int dr = -1;dr <= 1;dr++)              //接着遍历
        for (int dc = -1;dc <= 1;dc++)
            if (dr != 0 || dc != 0)
                dfs(m + dr, n + dc, flag, ch);
}

int main() {
    while (cin >> H >> W && H && W) {
        getchar();
        for (int i = 1;i <= H;i++) {
            photo[i].clear();
            cin >> s1;
            translate(s1, i);
        }
        vector<char>result;
        photo[0].clear();photo[H + 1].clear();
        for (int i = 0;i < W + 2;i++)photo[0] += "0000";
        photo[H + 1] = photo[0];
        dfs(0, 0, '3', '0');                    //先将图形之外的空白标记
        for(int i=0;i< H + 2;i++)               //查找图形
            for (int j = 0;j < 4 * (W + 2);j++) {
                sum = 0;
                if (photo[i][j] == '1') {
                    dfs(i, j, '3', '1');        //dfs查找遍历       
                    result.push_back(text[sum]);//入栈
                }
            }
        sort(result.begin(), result.end());     //排序
        cout << "Case " << ++num << ": ";       //输出
        for (int i = 0;i < result.size();i++)
            cout << result[i];
        cout << endl;
    }

}

标签:Ancient,int,photo,image,Messages,dfs,black,each,UVA
来源: https://www.cnblogs.com/3236676588buladuo/p/14400455.html