Ancient Message (古埃及象形文字识别 Uva 1103)

时间:2023-03-09 02:14:24
Ancient Message (古埃及象形文字识别 Uva 1103)

原题:https://uva.onlinejudge.org/external/11/1103.pdf

给一幅图(16进制), 判断图中有哪些象形文字。

只识别Ancient Message (古埃及象形文字识别 Uva 1103)

这6个就可以


示例:

将16进制数据

Ancient Message (古埃及象形文字识别 Uva 1103)

转换为

Ancient Message (古埃及象形文字识别 Uva 1103)

二进制数据

然后输出象形文字的名字

Ancient Message (古埃及象形文字识别 Uva 1103)


原理: 其实很简单,因为这六个象形文字比较特殊,每个文字包含的空心部分个数不一样。

比如Ankh有一个空心部分,Wedjat有3个空心部分。所以只要先用dfs找到象形文字,然后数一数

每个有几个空心部分就可以了。


 #include <cstdio>
#include <cstring>
#include <utility>
#include <set>
#include <algorithm>
#include <vector>
using namespace std; const int MAXN = + ;
bool pic[MAXN][MAXN], record[MAXN][MAXN];
const char *name = "WAKJSD";
int f[MAXN][MAXN], dx[] = {, -, , }, dy[] = {, , -, };
int cnt, H, W, T;
vector<pair<int, int> > startp; void init_read() {
char s[]; cnt = ; H += ; int NW = * W + ;
memset(pic, , sizeof(pic));
memset(f, , sizeof(f));
memset(record, , sizeof(record));
startp.clear();
for (int i = ; i < NW; i++) pic[][i] = pic[H - ][i] = ;
for (int i = ; i < H; i++) pic[i][] = pic[i][NW - ] = ;
for (int i = ; i < H - ; i++) {
scanf("%s", s);
for (int j = ; j < W; j++) {
int num = s[j] > '' ? s[j] - 'a' + : s[j] - '';
for (int k = ; k < ; k++)
if (num >= ( << ( - k))) { pic[i][j * + k + ] = ; num -= ( << ( - k));}
}
}
W = NW;
} void colorization(int x, int y, bool c) {
f[x][y] = cnt;
for (int i = ; i < ; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= && ny >= && nx < H && ny < W && pic[nx][ny] == c && !f[nx][ny])
colorization(nx, ny, c);
}
}
void floodfill() {
for (int i = ; i < H; i++)
for (int j = ; j < W; j++)
if (!f[i][j]) {
cnt++; colorization(i, j, pic[i][j]);
if (pic[i][j]) startp.push_back(make_pair(i, j));
}
} void get_signiture(set<int> &signature, int x, int y) {
record[x][y] = ;
for (int i = ; i < ; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= && ny >= && nx < H && ny < W) {
if (f[nx][ny] != f[x][y]) signature.insert(f[nx][ny]);
else if(f[nx][ny] == f[x][y] && !record[nx][ny])
get_signiture(signature, nx, ny);
}
}
}
void recognize() {
vector<char> ans;
int n = startp.size();
for (int i = ; i < n; i++) {
int x = startp[i].first, y = startp[i].second;
set<int> signature;
get_signiture(signature, x, y);
ans.push_back(name[signature.size() - ]);
}
sort(ans.begin(), ans.end());
printf("Case %d: ", ++T);
for (int i = ; i < n; i++)
printf("%c", ans[i]);
printf("\n");
} int main() {
while (scanf("%d%d", &H, &W) == && H) {
init_read();
floodfill();
recognize();
}
return ;
}