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. 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
题目类型:简单DFS搜索
算法分析:先构造出图片的二进制01矩阵,然后'1'的四连通块的个数就是输出字母的个数,可以给每个'1'连通块打上标记,然后给每个'0'连通块也打上标记,由于发现字母内部所含有的白色“洞”的个数不同,可以依此来分辨字母。只需要依次判断每个'1'相邻的四个位置是否是'0',并记录相邻的不同标号'0'的数量即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
/************************************************** filename :f.cpp author :maksyuki created time :2018/5/31 18:17:47 last modified :2018/5/31 19:34:42 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 266; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const char dict[] = "WAKJSD"; int row, col, cntb, cntw; char s[maxn][maxn]; int g[maxn][maxn]; int vis[maxn][maxn], wcnt; bool wflag[maxn*maxn]; bool In(int x, int y) { if(x >= 0 && x < row + 2 && y >= 0 && y < col * 4 + 2) return true; return false; } void dfs(int x, int y, int f) { if(!f) vis[x][y] = cntb; else vis[x][y] = cntw; for(int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(In(xx, yy)) { if(!f && g[xx][yy] == 1 && !vis[xx][yy]) dfs(xx, yy, f); if(f && g[xx][yy] == 0 && !vis[xx][yy]) dfs(xx, yy, f); } } } void check(int x, int y) { for(int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(In(xx, yy) && g[xx][yy] == 0 && vis[xx][yy] != 1 && !wflag[vis[xx][yy]]) { wflag[vis[xx][yy]] = true; wcnt++; } } } void ParseChar(int i, int j) { int tmp; if(isdigit(s[i][j])) { tmp = s[i][j] - '0'; } else tmp = s[i][j] - 'a' + 10; for(int k = j * 4 + 4; k >= j * 4 + 1; k--) { g[i+1][k] = tmp % 2; tmp /= 2; } } int main() { #ifdef LOCAL CFF; CFO; #endif int id = 1; while(cin >> row >> col) { if(!row && !col) break; memset(g, 0, sizeof(g)); memset(vis, 0, sizeof(vis)); for(int i = 0; i < row; i++) { cin >> s[i]; for(int j = 0; j < col; j++) ParseChar(i, j); } // for(int i = 0; i < row + 2; i++) { // for(int j = 0; j < col * 4 + 2; j++) // cout << g[i][j]; // cout << endl; // } cntb = -1, cntw = 1; for(int i = 0; i < row + 2; i++) for(int j = 0; j < col * 4 + 2; j++) { if(g[i][j] == 1 && vis[i][j] == 0) { dfs(i, j, 0); cntb--; } if(g[i][j] == 0 && vis[i][j] == 0) { dfs(i, j, 1); cntw++; } } // for(int i = 0; i < row + 2; i++) { // for(int j = 0; j < col * 4 + 2; j++) // cout << vis[i][j]; // cout << endl; // } vector<char> ans; for(int i = -1; i > cntb; i--) { memset(wflag, false, sizeof(wflag)); wcnt = 0; for(int j = 0; j < row + 2; j++) for(int k = 0; k < col * 4 + 2; k++) if(vis[j][k] == i) { check(j, k); } ans.emplace_back(dict[wcnt]); } sort(ans.begin(), ans.end()); cout << "Case " << id++ << ": "; int anslen = ans.size(); for(int i = 0; i < anslen; i++) cout << ans[i]; cout << endl; } return 0; } |