标签:
原题:https://uva.onlinejudge.org/external/11/1103.pdf
给一幅图(16进制), 判断图中有哪些象形文字。
只识别
这6个就可以
示例:
将16进制数据
转换为
二进制数据
然后输出象形文字的名字
原理: 其实很简单,因为这六个象形文字比较特殊,每个文字包含的空心部分个数不一样。
比如Ankh有一个空心部分,Wedjat有3个空心部分。所以只要先用dfs找到象形文字,然后数一数
每个有几个空心部分就可以了。
1 #include <cstdio> 2 #include <cstring> 3 #include <utility> 4 #include <set> 5 #include <algorithm> 6 #include <vector> 7 using namespace std; 8 9 const int MAXN = 200 + 10; 10 bool pic[MAXN][MAXN], record[MAXN][MAXN]; 11 const char *name = "WAKJSD"; 12 int f[MAXN][MAXN], dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0}; 13 int cnt, H, W, T; 14 vector<pair<int, int> > startp; 15 16 void init_read() { 17 char s[55]; cnt = 0; H += 2; int NW = 4 * W + 2; 18 memset(pic, 0, sizeof(pic)); 19 memset(f, 0, sizeof(f)); 20 memset(record, 0, sizeof(record)); 21 startp.clear(); 22 for (int i = 0; i < NW; i++) pic[0][i] = pic[H - 1][i] = 0; 23 for (int i = 0; i < H; i++) pic[i][0] = pic[i][NW - 1] = 0; 24 for (int i = 1; i < H - 1; i++) { 25 scanf("%s", s); 26 for (int j = 0; j < W; j++) { 27 int num = s[j] > ‘9‘ ? s[j] - ‘a‘ + 10 : s[j] - ‘0‘; 28 for (int k = 0; k < 4; k++) 29 if (num >= (1 << (3 - k))) { pic[i][j * 4 + k + 1] = 1; num -= (1 << (3 - k));} 30 } 31 } 32 W = NW; 33 } 34 35 void colorization(int x, int y, bool c) { 36 f[x][y] = cnt; 37 for (int i = 0; i < 4; i++) { 38 int nx = x + dx[i], ny = y + dy[i]; 39 if (nx >= 0 && ny >= 0 && nx < H && ny < W && pic[nx][ny] == c && !f[nx][ny]) 40 colorization(nx, ny, c); 41 } 42 } 43 void floodfill() { 44 for (int i = 0; i < H; i++) 45 for (int j = 0; j < W; j++) 46 if (!f[i][j]) { 47 cnt++; colorization(i, j, pic[i][j]); 48 if (pic[i][j]) startp.push_back(make_pair(i, j)); 49 } 50 } 51 52 void get_signiture(set<int> &signature, int x, int y) { 53 record[x][y] = 1; 54 for (int i = 0; i < 4; i++) { 55 int nx = x + dx[i], ny = y + dy[i]; 56 if (nx >= 0 && ny >= 0 && nx < H && ny < W) { 57 if (f[nx][ny] != f[x][y]) signature.insert(f[nx][ny]); 58 else if(f[nx][ny] == f[x][y] && !record[nx][ny]) 59 get_signiture(signature, nx, ny); 60 } 61 } 62 } 63 void recognize() { 64 vector<char> ans; 65 int n = startp.size(); 66 for (int i = 0; i < n; i++) { 67 int x = startp[i].first, y = startp[i].second; 68 set<int> signature; 69 get_signiture(signature, x, y); 70 ans.push_back(name[signature.size() - 1]); 71 } 72 sort(ans.begin(), ans.end()); 73 printf("Case %d: ", ++T); 74 for (int i = 0; i < n; i++) 75 printf("%c", ans[i]); 76 printf("\n"); 77 } 78 79 int main() { 80 while (scanf("%d%d", &H, &W) == 2 && H) { 81 init_read(); 82 floodfill(); 83 recognize(); 84 } 85 return 0; 86 }
Ancient Message (古埃及象形文字识别 Uva 1103)
标签:
原文地址:http://www.cnblogs.com/Bowen-/p/4937983.html