Ancient Messages HDU - 3839

题目链接:Ancient Messages HDU - 3839

===================================================

Ancient Messages

Time Limit: 1000 ms
Memory Limit: 32768 kB

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

===================================================

题意:就是给你一副图,图里面有很多埃及象形字,然后图像由像素0,1构成,1表示颜色黑,0则表示白。

算法:BFS

难点:

  • 第一点在于怎么巧妙区别埃及象形字? 每个埃及象形字都是有闭合空间并且数量对于每个埃及象形字不同,所以可以从空白入手。
  • 第二点在于如何区分埃及象形字内的空白与外空白? 这里题目给出了关键点,就是每两个埃及象形字必不连接且不嵌套。并且黑色部分永远相连不断。

思路:

  • 预处理:这里使用十六进制代表二进制像素,所以需要还原。
  • 第一步:从边界开始读空白,进行白色BFS,把所有埃及象形字的外空白标记。
  • 第二步:从上到下从左到右,找埃及象形字,遇到则开始黑色BFS,每遇到白色没标记则开始白色BFS标记;重点记录,遇到几次空白,即这个埃及象形字有多少闭合空白空间。从而区分这个是什么埃及象形字;
  • 第三步:通过BFS得出的埃及象形字数量,然后字典输出即可AC。

===================================================

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>

using namespace std;

int n,m,ditu[202][202],ans[8],cas=1;

int gg[] = {1,5,3,2,4,0};
char gk[] = {'A','D','J','K','S','W'};

int xx[] = {1,-1,0,0};
int yy[] = {0,0,1,-1};

bool vis[202][202];

struct node{
    int x,y;
    node(int a,int b):x(a),y(b) {};
};

void bfs_white(int a,int b){
    queue<node> q;
    q.push(node(a,b)),vis[a][b] = true;
    while(!q.empty()){
        node p = q.front();q.pop();
        for(int i=0;i<4;i++){
            int x = p.x + xx[i],y = p.y + yy[i];
            if(x<0||x>=n||y<0||y>=m||ditu[x][y]||vis[x][y]) continue;
            q.push(node(x,y)),vis[x][y] = true;
        }
    }
    return ;
}

void bfs_black(int a,int b){
    queue<node> q;
    int acer = 0;
    q.push(node(a,b)),vis[a][b] = true;
    while(!q.empty()){
        node p = q.front();q.pop();
        for(int i=0;i<4;i++){
            int x = p.x + xx[i],y = p.y + yy[i];
            if(x<0||x>=n||y<0||y>=m||vis[x][y]) continue;
            if(ditu[x][y]) q.push(node(x,y)),vis[x][y] = true;
            else bfs_white(x,y),acer++;
        }
    }
    ans[acer]++;
    return ;
}

void show(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++) cout<<ditu[i][j]<<" ";cout<<endl;
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m)){
        if(!n&&!m) break;
        for(int i=0;i<n;i++) for(int j=0,z=0;j<m;j++){
            char ch = getchar();
            while(ch==' '||ch=='\n') ch = getchar();
            int num;
            if(ch>='a'&&ch<='z') num = ch-'a'+10;
            else num = ch-'0';
            if(num>=8) ditu[i][z++] = 1,num-=8;
            else ditu[i][z++] = 0;
            if(num>=4) ditu[i][z++] = 1,num-=4;
            else ditu[i][z++] = 0;
            if(num>=2) ditu[i][z++] = 1,num-=2;
            else ditu[i][z++] = 0;
            if(num>=1) ditu[i][z++] = 1;
            else ditu[i][z++] = 0;
        }m*=4;
        //show();

        memset(ans,0,sizeof ans);
        memset(vis,false,sizeof vis);

        //边界找0
        for(int j=0;j<m;j++){
            if(!vis[0][j]&&ditu[0][j]==0) bfs_white(0,j);
            if(!vis[n-1][j]&&ditu[n-1][j]==0) bfs_white(n-1,j);
        }
        for(int i=0;i<n;i++){
            if(!vis[i][0]&&ditu[i][0]==0) bfs_white(i,0);
            if(!vis[i][m-1]&&ditu[i][m-1]==0) bfs_white(i,m-1);
        }

        //找图像
        for(int i=0;i<n;i++)for(int j=0;j<m;j++) if(!vis[i][j]&&ditu[i][j]) bfs_black(i,j);

        printf("Case %d: ",cas++);
        for(int i=0;i<6;i++) for(int j=0;j<ans[gg[i]];j++) printf("%c",gk[i]);
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值