zoukankan      html  css  js  c++  java
  • HDU 3839 Ancient Messages(DFS)

    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,
    这里有个小操作是把存的图像外圈围一圈0,这样就可以直接从(0,0)点dfs对字外的0改标记为1,然后再遍历数组找1,即找字,找到字后将字内的0同样标上-1,并计算空洞数,
    同时将字上的1全部转换(相当于遍历过这里的标记),确定这个字之后将其压入vector数组,最后结果对vector数组排序并输出即可。
      1 #include <algorithm>
      2 #include <cstdio>
      3 #include <cmath>
      4 #include <cstring>
      5 #include <iostream>
      6 #include <cstdlib>
      7 #include <set>
      8 #include <vector>
      9 #include <cctype>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <climits>
     13 #include <queue>
     14 #include <stack>
     15 using namespace std;
     16 typedef long long ll;
     17 #define INF 0X3f3f3f3f
     18 const ll MAXN = 1e5 + 7;
     19 const ll MOD = 1e9 + 7;
     20 char trans[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
     21 int transto[][4] = {
     22     {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1},
     23     {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1},
     24     {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
     25     {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}
     26 };
     27 int maze[500][500];
     28 int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
     29 char ans[] = {'W', 'A', 'K', 'J', 'S', 'D'};
     30 int n, m, cnt=0;
     31 bool check(int x, int y)
     32 {
     33     if (x >= 0 && x <= n+1 && y >= 0 && y <= 1+4 * m)
     34         return true;
     35     return false;
     36 }
     37 void dfs_vis(int x, int y) //将字外0标为-1
     38 {
     39     if (!check(x, y)||maze[x][y])
     40         return;
     41     maze[x][y] = -1; //在字外面的0标为-1
     42     for (int i = 0; i < 4; i++)
     43         dfs_vis(x + dir[i][0], y + dir[i][1]);
     44 }
     45 void dfs_bak(int x, int y) //找字的空白块
     46 {
     47     if (!check(x, y) ||maze[x][y] == -1 ||  maze[x][y] == 2)
     48         return;
     49     if (maze[x][y] == 0)
     50     {
     51         cnt++;
     52         dfs_vis(x, y);
     53     }
     54     else
     55     {
     56         maze[x][y] = 2;
     57         for (int i = 0; i < 4; i++)
     58             dfs_bak(x + dir[i][0], y + dir[i][1]);
     59     }
     60     return;
     61 }
     62 void debug()
     63 {
     64     for (int i = 0; i <= n+1; i++)
     65     {
     66         for (int j = 0; j <=1+ 4 * m; j++)
     67             cout << maze[i][j];
     68         cout << endl;
     69     }
     70     return;
     71 }
     72 int main()
     73 {
     74     ios::sync_with_stdio(false);
     75     int t = 1;
     76     while (cin >> n >> m && n && m)
     77     {
     78         vector<char> vec;
     79         memset(maze, 0, sizeof(maze));
     80         for (int i = 0; i < n; i++)
     81         {
     82             char temp[60];
     83             cin >> temp;
     84             int cnt = 1;
     85             for (int i1 = 0; i1 < m; i1++)
     86             {
     87                 for (int j = 0; j < 16; j++)
     88                 {
     89                     if (trans[j] == temp[i1])
     90                     {
     91                         for (int k = 0; k < 4; k++)
     92                             maze[i+1][cnt++] = transto[j][k];
     93                         break;
     94                     }
     95                 }
     96             }
     97         }
     98         // debug();
     99         dfs_vis(0,0);
    100         for (int i = 1; i <= n; i++)
    101         {
    102             for (int j = 1; j <= 4*m+1; j++)
    103             {
    104                 if (maze[i][j] == 1)
    105                 {
    106                     dfs_bak(i, j);
    107                     vec.push_back(ans[cnt]);
    108                     cnt=0;
    109                     // debug();
    110                 }
    111             }
    112         }
    113         sort(vec.begin(), vec.end());
    114         cout << "Case " << t++ << ": ";
    115         vector<char>::iterator it;
    116         for (it = vec.begin(); it != vec.end(); it++)
    117             cout << *it;
    118         cout << endl;
    119     }
    120     return 0;
    121 }
    122 /* A-1 J-3 D-5 S-4 W-0 K-2*/
    
    

    再给出两组数据方便自行debug:

    100 25
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    00000f8000000000000000000
    00001fe000000000000000000
    00007ff000000000000000000
    00007ff800000000000000000
    0000f8f800000000000000000
    0001f07c00000000000000000
    0001e03c00000000001800000
    0001e01c00000000003c00000
    0001c01c00000000007c00000
    0003c01e0000000000f800000
    0003c01e0000000001f000000
    0001c01c0000000003f000000
    0001c01c0000000007e000000
    0001e01c000000000fc000000
    0001e03c000000001fc000000
    0000e03c000000001fc000000
    0000f038000000003ff000000
    0000f078000000003ff800000
    00007870000000007ff800000
    000038f0000000007cfc00000
    00003ce0000000007c7c00000
    00781fc0f0000000f87c00000
    007ffffff0000000f07c00000
    007ffffff0000000f07c00000
    007ffffff0000001f07c00000
    007ffffff0000000e03e00000
    007fcf81f0000000603e00000
    00000f8000000000003e00000
    00000f8000000000003e00000
    00000f8000000000003e00000
    00000f8000000000001e00000
    00000f8000000000001f00000
    00000fc000000000001f00000
    00000fc000000000001f00000
    00000fc000000000001f00000
    00000fc000000000000f00000
    00001fc000000000000f80000
    00001fc000000000000f80000
    00001fc000000000000f80000
    00001fc000000000000f80000
    00001fe000000000000f80000
    00001fe000000000000780000
    00001fe0000000000007c0000
    00001fe0000000000007c0000
    00003fe0000000000007c0000
    00003fe0000000000007c0000
    00003fe0000000000007c0000
    00003fe0000c00000003c0000
    00000000003ff0000003c0000
    00000000007ff8000003e0000
    0000000001fffc000003e0000
    0000000003e03f000003e0000
    0000000007c00f000003e0000
    000000000f0003800003f0000
    000000000e0001c00003fc000
    000000001c0001e00007fe000
    000000003c0000e0000fff000
    000000073c000070000fdf000
    0000001ff8000070001f0f800
    0000001ff8000070001e07800
    0000003cf0000078001e03800
    0000003870000033001e03800
    000000307800003fc01e03800
    000000703800007fe00e03800
    000000703800007ce00e03800
    000000703c000078700703800
    000000701e0000f0700701000
    000000701e0000e0700300000
    000000700f0001c0700000000
    0000006007800380600000000
    000000e003e00700600000000
    000000e001fe7e00600000000
    000000e000fffc00e00000000
    000000e0000ff000e00000000
    000000f800038000e00000000
    000000fff0000000e00000000
    000000fffff00000e00000000
    00000003ffffe000c00000000
    0000000007ffffc0c00000000
    000000000007ffffc00000000
    0000000000000fffc00000000
    000000000000001fc00000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    0000000000000000000000000
    150 38
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000f80000000001fff000000000000000000
    00001fe0000000007fff800000000000000000
    00007ff000000000ffffe00000000000000000
    00007ff800000003fffff0000000007c000000
    0000f8f800000007fffffc00000000fe000000
    0001f07c0000000ffffffe00000001ff000000
    0001e03c0000001fffffff00000003ff800000
    0001e01c0000003fffffff00000003ffc00000
    0001c01c0000003fffffff80000007efc00000
    0003c01e0000007fffffffc000000783c00000
    0003c01e000000ffffffffc000000f81e00000
    0001c01c000000fffc0fffe000000f01e00000
    0001c01c000001fff003ffe000000f01e00000
    0001e01c000001ffe001fff000000f00e00000
    0001e03c000003ffc0007ff000001e00f00000
    0000e03c000003ff80007ff800001e00f00000
    0000f038000007ff80003ff800001e00f00000
    0000f078000007ff00003ff800001e00f00000
    00007870000007ff00001ffc00000e00e00000
    000038f000000fff00001ffc00000e00e00000
    00003ce000000ffe00000ffc00000e00e00000
    00781fc0f0000ffe00000ffc00000f00e00000
    007ffffff0000ffc00000ffc00000f01e00000
    007ffffff0000ffc00000ffc00000f01e00000
    007ffffff0000ffc00000ffc00000701c00000
    007ffffff0000ffc00000ffc00000781c00000
    007fcf81f0000ffc000007fc00000783c00000
    00000f8000000ffc000007fc00000383800000
    00000f8000000ffc000007fc000003c7800000
    00000f8000000ffc000007fc000001c7800000
    00000f8000000ffc000007fc000001e7000000
    00000f8000000ffc000007fc000200ef008000
    00000fc000000ffc00000ffc0003f8fe3f8000
    00000fc000000ffc00000ffc0003ffffff8000
    00000fc000000ffc00000ffc0003ffffff8000
    00000fc000000ffc00000ffc0003ffffff8000
    00001fc000000ffc00000ffc0003ffffff8000
    00001fc0000007fe00000ff80003ffffff8000
    00001fc0000007fe00000ff80003fffdff8000
    00001fc0000007fe00000ff80003c03c000000
    00001fe0000007ff00001ff80000007c000000
    00001fe0000003ff00001ff00000007c000000
    00001fe0000003ff00001ff00000007c000000
    00001fe0000001ff80003ff00000007c000000
    00003fe0000001ff80003fe00000007c000000
    00003fe0000001ff80003fe00000007c000000
    00003fe0000000ffc0007fe00000007c000000
    00003fe0000000ffc0007fc00000007e000000
    000000000000007fe0007fc00000007e000000
    000000000000007fe000ff800000007e000000
    000000000000007ff001ff800000007e000000
    000000000000003ff001ff80000000fe000000
    000000000000001ff803ff00000000fe000000
    000000000000001ff803ff00000000fe000000
    000000000000000ffc07fe00000000fe000000
    000000000000000ffc0ffc00000000fe000000
    000000000000000ffe0ffc00000000fe000000
    0000000000000007ff0ff800000000fe000000
    0000000000000003ff1ff000000000ff000000
    0000000003c00001ffbff00000f000ff000000
    0000000003ffc001ffffe0007ff000ff000000
    0000000003fffff1ffffe3fffff000ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff001ff000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffffffffffffff00000000000
    0000000003fffffc1ffe0007fff00000000000
    0000000003ff80000ffe000000f00000000000
    00000000038000000ffe000000000000000000
    00000000000000001fff000000000000000000
    00000000000000001fff000000000000000000
    00000000000000001fff000000000000000000
    00000000000000001fff000000000000000000
    00000000000000001fff000000000000000000
    00000000000000001fff000000000000000000
    00000000000000003fff000000000000000000
    00000000000000003fff000000000000000000
    00000000000000003fff0000000fc000000000
    000000000fe000003fff8000003ff000000000
    000000003ffc00003fff8000007ffc00000000
    00000000fffe00003fff800000fcfc00000000
    00000001f01f00003fff800001f03e00000000
    00000003e00f80003fff800003e01f00000000
    00000003e00780003fff800003e00f00000000
    00000003e00780003fff800003c00f00000000
    00000003e00f80003fff800003c00f00000000
    00000001f00f00007fff800003c00f00000000
    00000000f81e00007fffc00003e00f00000000
    000000007c3c00007fffc00001e01e00000000
    000000003e7800007fffc00000f01e00000000
    000000fffffffe007fffc00000f03c00000000
    000000fffffffe007fffc00000787800000000
    000000fffffffe007fffc000003cf000000000
    0000000007c000007fffe000f81fe07c000000
    0000000007e000007fffe000fffffffc000000
    0000000007e000007fffe000fffffffc000000
    000000000fe000007fffe000fffffffc000000
    000000000ff00000ffffe000ffc7c0fc000000
    000000000ff00000ffffe0000007c000000000
    000000001ff00000ffffe000000fc000000000
    000000001ff00000ffffe000000fc000000000
    000000001ff80000ffffe000000fc000000000
    000000001ff80000ffffe000000fc000000000
    000000003ff80001ffffe000000fe000000000
    000000003ff80001ffffe000000fe000000000
    0000000000000001fffff000001fe000000000
    0000000000000001fffff000001fe000000000
    0000000000000001fffff000001fe000000000
    0000000000000001fffff000001ff000000000
    0000000000000001fffff000001ff000000000
    0000000000000001fffff000001ff000000000
    0000000000000001fffff000003ff000000000
    0000000000000001fffff000003ff000000000
    0000000000000001fffff000003ff000000000
    0000000000000001fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffff80000000000000000
    0000000000000003fffffc0000000000000000
    0000000000000003fffffc0000000000000000
    0000000000000007fffffc0000000000000000
    0000000000000007fffffc0000000000000000
    0000000000000007fffffc0000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000
    00000000000000000000000000000000000000

    Case 1: AKW
    Case 2: AAAAA


  • 相关阅读:
    用JavaScript 来创建 mac os x 程序这样是否好
    hadoop的namenode无法启动的解决的方法
    Cocos2d-x新建模板编译问题总汇
    JBoss AS 7之基本配置和部署(The Return Of The King)
    margin和padding的学习
    HDU 1232 畅通project
    C/C++程序内存分类
    iOS 推断设备为iPhone还是iPad
    or1200中载入存储类指令说明
    专业词汇-计算机:占位符
  • 原文地址:https://www.cnblogs.com/graytido/p/10414569.html
Copyright © 2011-2022 走看看