hdu 3839 Ancient Messages(搜索)

Ancient Messages

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 367    Accepted Submission(s): 124


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
 

题意就是 给出一个图 判断图里面有哪些图案

这个题比上一题稍微复杂一点 在于 这个要判断 黑色块包围的白色块的数量

一开始要在外围添加白色的块  确保外面全是白色的

然后通过连通块 给每个白色的块编号

然后求出一个黑色连通块旁边白色连通块的数量

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <set>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

struct node
{
    int x,y;
};

int n,m;
int mp[900][250];
int vis[900][250];
int col[900][250];
int cnt;
char pic[6]={'W','A','K','J','S','D'};
set<int> S;
//int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
void color(int x,int y)
{
    vis[x][y]=1;
    node no;
    no.x=x; no.y=y;
    col[x][y]=cnt;
    queue<node> que;
    que.push(no);
    while(!que.empty())
    {
        node q=que.front();     que.pop();
        for(int i=0;i<4;i++)
        {
            node next;
            next.x=q.x+dir[i][0]; next.y=q.y+dir[i][1];
            if(next.x>=0&&next.x<=(n+1)&&next.y>=0&&next.y<(4*m+8))
            {
                if(vis[next.x][next.y]) continue;
                if(mp[next.x][next.y]) continue;
                if(col[next.x][next.y]) continue;
                col[next.x][next.y]=cnt;
                vis[next.x][next.y]=1;
                que.push(next);
            }
        }
    }
}

void bfs(int x,int y)
{
    node no;
    no.x=x; no.y=y;
    vis[x][y]=1;
    queue<node> que;
    que.push(no);
    while(!que.empty())
    {
        node q=que.front(); que.pop();
        for(int i=0;i<4;i++)
        {
            node next;
            next.x=q.x+dir[i][0]; next.y=q.y+dir[i][1];
//            if(next.x>=0&&next.x<=(n+1)&&next.y>=0&&next.y<(4*m+8))
//            {
//                if(!mp[next.x][next.y])
//                {
//                    if(col[next.x][next.y]!=col[0][0])
//                        S.insert(col[next.x][next.y]);
//                }
//                else if(!vis[next.x][next.y])
//                {
//                    que.push(next);
//                    vis[next.x][next.y]=1;
//                }
                if(vis[next.x][next.y]) continue;
//                vis[next.x][next.y]=1;
                if(mp[next.x][next.y])
                {
                    que.push(next);
                    vis[next.x][next.y]=1;
                }
                else
                {
                    if(col[next.x][next.y]!=col[0][0])
                        S.insert(col[next.x][next.y]);
                }
            }
        }
    }


int main()
{
//    fread;
    int cs=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)  break;
        MEM(vis,0);
        for(int i=0;i<4*m+8;i++)
            mp[0][i]=mp[n+1][i]=0;
        for(int i=1;i<=n;i++)
        {
            char ch[250];
            scanf("%s",ch);
            for(int k=0;k<4;k++)
            {
                mp[i][k]=mp[i][4+4*m+k]=0;
            }
            for(int j=0;j<m;j++)
            {
                int x;
                if(ch[j]>='0'&&ch[j]<='9')
                    x=ch[j]-'0';
                else x=ch[j]-'a'+10;
                for(int k=0;k<4;k++)
                {
                    if(x&(1<<(3-k)))//这一步。。。。
                        mp[i][4*(j+1)+k]=1;
                    else mp[i][4*(j+1)+k]=0;
                }
            }
        }
        MEM(col,0);
        cnt=0;
        for(int i=0;i<=(n+1);i++)
        {
            for(int j=0;j<4*(m+2);j++)
            {
                if(!mp[i][j]&&!vis[i][j])
                {
                    cnt++;
                    color(i,j);
                }
            }
        }
        MEM(vis,0);
        vector<char> vec;
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<(4*m+8);j++)
            {
                if(mp[i][j]&&!vis[i][j])
                {
                    S.clear();
                    bfs(i,j);
                    int num=S.size();
//                    cout<<"num "<<num<<endl;
                    vec.push_back(pic[num]);
                }
            }
        }
        sort(vec.begin(),vec.end());
        printf("Case %d: ",cs++);
        int len=vec.size();
//        cout<<"len "<<len<<endl;
        for(int i=0;i<len;i++)
            printf("%c",vec[i]);
        printf("\n");
        vec.clear();
    }
    return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <set>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int n,m;
int mp[250][250];
int vis[250][250];
int col[250][250];
int cnt;
char pic[6]={'W','A','K','J','S','D'};
set<int> S;
//int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

void color(int x,int y)
{
    col[x][y]=cnt;
    for(int i=0;i<4;i++)
    {
        int a=x+dir[i][0]; int b=y+dir[i][1];
        if(a>=0&&a<=(n+1)&&b>=0&&b<4*(m+2))
        {
            if(!mp[a][b]&&!col[a][b])
                color(a,b);
        }
    }
}

void dfs(int x,int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        int a=x+dir[i][0],b=y+dir[i][1];
        if(a>=0&&a<=n+1&&b>=0&&b<4*(m+2))
        {
            if(!mp[a][b])
            {
                if(col[a][b]!=col[0][0])
                    S.insert(col[a][b]);
            }
            else if(!vis[a][b])
                dfs(a,b);
        }
    }
}

int main()
{
//    fread;
    int cs=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=0;i<4*(m+2);i++)
            mp[0][i]=mp[n+1][i]=0;
        for(int i=1;i<=n;i++)
        {
            char ch[250];
            scanf("%s",ch);
            for(int k=0;k<4;k++)
            {
                mp[i][k]=mp[i][4+m*4+k]=0;
            }
            for(int j=0;j<m;j++)
            {
                int x;
                if(ch[j]>='0'&&ch[j]<='9')
                    x=ch[j]-'0';
                else x=ch[j]-'a'+10;
                for(int k=0;k<4;k++)
                {
                    if(x&(1<<(3-k)))//。。。。
                        mp[i][4+4*j+k]=1;
                    else mp[i][4+4*j+k]=0;
                }
            }
        }
//        for(int i=0;i<=n+1;i++)
//        {
//            for(int j=0;j<4*(m+2);j++)
//                printf("%d",mp[i][j]);
//            printf("\n");
//        }
        MEM(vis,0);
        MEM(col,0);
        cnt=0;
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<4*(m+2);j++)
            {
                if(!mp[i][j]&&!col[i][j])
                {
                    cnt++;
                    color(i,j);
                }
            }
        }
        vector<char> vec;
        for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<4*m+8;j++)
            {
                if(mp[i][j]&&!vis[i][j])
                {
                    S.clear();
                    dfs(i,j);
                    int num=S.size();
                    vec.push_back(pic[num]);
                }
            }
        }
        sort(vec.begin(),vec.end());
        printf("Case %d: ",cs++);
        int len=vec.size();
        for(int i=0;i<len;i++)
            printf("%c",vec[i]);
        puts("");
        vec.clear();
    }
    return 0;
}

两个都过了 再给出几个测试数据

5 3
fff
f0f
fff
f0f
fff
3 3
fff
f0f
fff
11 11
fff0fff0fff
f0f0f0f0f0f
fff0fff0fff
f0f0fff0f0f
fff0fff0fff
00000000000
fff0fff0fff
f0f0f0f0f0f
fff0fff0fff
f0f0fff0f0f
fff0fff0fff
0 0





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值