图-用DFS求连通块 UVa 1103

这道题目甚长, 代码也是甚长, 但是思路却不是太难。然而有好多代码实现的细节, 确是十分的巧妙。 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, 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.

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 < H200) is the number of scan lines in the image. W (0 < W50) 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
时间: 2024-10-10 17:15:58

图-用DFS求连通块 UVa 1103的相关文章

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常量数组或者写8条DFS调用. 下述算法是:种子填充(floodfill) 两种连通区域 四连通区域:从区域内一点出发,可通过上.下.左.右四个方向的移动组合,在不越出区域的前提下,能到达区域内的任意像素 八连通区域:从区域内每一像素出发,可通过八个方向,即上.下.左.右.左上.右上.左下.右下移动的

DFS入门之二---DFS求连通块

用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所在位置相邻(上下左右对角共八个方位),则在一个连通块.典型例题:HDU 1241 Oil Deposits 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目描述:输入m行n列的字符矩阵, 统计字符“@”组成八连块的个数. 题意分析:读入数据

油田(Oil Deposits)-用DFS求连通块

[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横.纵或者对角线方向),就说它们属于一个八连块.例如,下图中有两个八连块. 一.分析 1.1 整体思路 图也有DFS和BFS遍历.由于DFS更容易编写,一般用DFS查找连通块:从每个&

UVA 572 dfs求连通块

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyze

用DFS求连通块(种子填充)

[问题] 输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分析] 和前面的二叉树遍历类似,图也有DFS和BFS遍历.由于DFS更容易编写,一般用DFS找连通块:从每个"@"格子出发,递归遍历它周围的"@"格子.每次访问一个格子时就给它写上一个"连通分量编号"(即下面代码

UVa 572 油田(DFS求连通块)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=513 终于开始接触图了,恩,开始接触DFS了,这道题就是求连通分量,比较简单. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int m, n; //记录连通块的数量 6 cha

UVa572 Oil Deposits (DFS求连通块)

链接:http://acm.hust.edu.cn/vjudge/problem/19435分析:DFS求图的连通块.每次从图中找出一个未访问过的‘@’开始递归遍历它周围的‘@’格子,将访问到的‘@’标上id存在idx数组中,dfs的次数就是连通块的个数.DFS遍历用递归实现,BFS用队列实现.DFS和BFS实现种子填充:https://zh.wikipedia.org/wiki/Flood_fill 1 #include <cstdio> 2 #include <cstring>

PAT-1013 Battle Over Cities (25 分) DFS求连通块

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of