这道题目甚长, 代码也是甚长, 但是思路却不是太难。然而有好多代码实现的细节, 确是十分的巧妙。 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者耐心细读。(也许由于博主太弱, 才有此等感觉)。

 

题目: UVa 1103

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.

图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
Figure C.1: Six hieroglyphs

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图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。200) is the number of scan lines in the image. W (0 < W图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。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.

图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

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

代码请细读(加了自己所理解的注释):
  1 // we pad one empty line/column to the top/bottom/left/right border, so color 1 is always "background" white
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<vector>
  6 #include<set>
  7 using namespace std;
  8 
  9 char bin[256][5]; 
 10 
 11 const int maxh = 200 + 5;
 12 const int maxw = 50 * 4 + 5;
 13 
 14 int H, W, pic[maxh][maxw], color[maxh][maxw];
 15 char line[maxw];
 16 
 17 //把读入转化成 0, 1 的函数。 
 18 void decode(char ch, int row, int col) {
 19   for(int i = 0; i < 4; i++)
 20     pic[row][col+i] = bin[ch][i] - '0';
 21 }
 22 
 23 const int dr[] = {-1, 1, 0, 0};
 24 const int dc[] = {0, 0, -1, 1};
 25 
 26 // dfs from (row, col) and paint color c
 27 void dfs(int row, int col, int c) {
 28   color[row][col] = c;
 29   for(int i = 0; i < 4; i++) {
 30     int row2 = row + dr[i];
 31     int col2 = col + dc[i];
 32     if(row2 >= 0 && row2 < H && col2 >= 0 && col2 < W && pic[row2][col2] == pic[row][col] && color[row2][col2] == 0)
 33       dfs(row2, col2, c);
 34   }
 35 }
 36 
 37 vector<set<int> > neighbors;
 38 
 39 void check_neighbors(int row, int col) {
 40   for(int i = 0; i < 4; i++) {
 41     int row2 = row + dr[i];
 42     int col2 = col + dc[i];
 43     if(row2 >= 0 && row2 < H && col2 >= 0&& col2 < W && pic[row2][col2] == 0 && color[row2][col2] != 1)
 44       neighbors[color[row][col]].insert(color[row2][col2]);
 45   }
 46 }
 47 
 48 const char* code = "WAKJSD";
 49 //以容器长度来表示黑连通块旁的内白连通块数(即:象形文字内的白块数) 
 50 char recognize(int c) {
 51   int cnt = neighbors[c].size();
 52   return code[cnt];
 53 }
 54 
 55 // use this function to print the decoded picture
 56 void print() {
 57   for(int i = 0; i < H; i++) {
 58     for(int j = 0; j < W; j++) printf("%d", pic[i][j]);
 59     printf("\n");
 60   }
 61 }
 62 
 63 int main() {
 64   strcpy(bin['0'], "0000");//用来转化成二进制, 此法甚妙 
 65   strcpy(bin['1'], "0001");
 66   strcpy(bin['2'], "0010");
 67   strcpy(bin['3'], "0011");
 68   strcpy(bin['4'], "0100");
 69   strcpy(bin['5'], "0101");
 70   strcpy(bin['6'], "0110");
 71   strcpy(bin['7'], "0111");
 72   strcpy(bin['8'], "1000");
 73   strcpy(bin['9'], "1001");
 74   strcpy(bin['a'], "1010");
 75   strcpy(bin['b'], "1011");
 76   strcpy(bin['c'], "1100");
 77   strcpy(bin['d'], "1101");
 78   strcpy(bin['e'], "1110");
 79   strcpy(bin['f'], "1111");
 80 
 81   int kase = 0;
 82   while(scanf("%d%d", &H, &W) == 2 && H) {
 83     memset(pic, 0, sizeof(pic));
 84     for(int i = 0; i < H; i++) {
 85       scanf("%s", line);
 86       for(int j = 0; j < W; j++)
 87         decode(line[j], i+1, j*4+1);//把输入转发成二进制 存于数组pic[][]中 
 88     }
 89 
 90     H += 2;
 91     W = W * 4 + 2;
 92 
 93     int cnt = 0;
 94     vector<int> cc; // connected components of 1
 95     memset(color, 0, sizeof(color));//标记数组。 
 96     for(int i = 0; i < H; i++)
 97       for(int j = 0; j < W; j++)
 98         if(!color[i][j]) {
 99           dfs(i, j, ++cnt);
100           if(pic[i][j] == 1) cc.push_back(cnt);//扫描矩阵,且为所有连通块编号,并把黑连通块号存进cc容器中。 
101         }
102 
103     neighbors.clear();
104     neighbors.resize(cnt+1);//设置容器大小, 不清楚的自行百度。嘿嘿! 
105     for(int i = 0; i < H; i++)
106       for(int j = 0; j < W; j++)
107         if(pic[i][j] == 1)
108           check_neighbors(i, j);//扫描黑点,并把该点旁边有几个内连通白块存入neighbors容器。//其中下标为黑点对应的连通块编号。  
109 
110     vector<char> ans;
111     for(int i = 0; i < cc.size(); i++)
112       ans.push_back(recognize(cc[i]));//存目标值, 并排序。 
113     sort(ans.begin(), ans.end());
114 
115     printf("Case %d: ", ++kase);
116     for(int i = 0; i < ans.size(); i++) printf("%c", ans[i]);
117     printf("\n");
118   }
119   return 0;
120 }
View Code

相关文章: