zoj 1047 Image Perimeters

来源:互联网 发布:淘宝刷单降权多久恢复 编辑:程序博客网 时间:2024/05/17 09:21
ZOJ Problem Set - 1047
ImagePerimeters

Time Limit: 2 Seconds    Memory Limit: 65536 KB

Technicians in a pathology lab analyze digitized images ofslides. Objects on a slide are selected for analysis by a mouseclick on the object. The perimeter of the boundary of an object isone useful measure. Your task is to determine this perimeter forselected objects.

The digitized slides will be represented by a rectangular grid ofperiods, '.', indicating empty space, and the capital letter 'X',indicating part of an object. Simple examples are

zoj <wbr>1047 <wbr>Image <wbr>Perimeters

An X in a grid square indicates that the entire grid square,including its boundaries, lies in some object. The X in the centerof the grid below is adjacent to the X in any of the 8 positionsaround it. The grid squares for any two adjacent X's overlap on anedge or corner, so they are connected.

XXX
XXX Central X and adjacent X's
XXX

An object consists of the grid squares of all X's that can belinked to one another through a sequence of adjacent X's. In Grid1, the whole grid is filled by one object. In Grid 2 there are twoobjects. One object contains only the lower left grid square. Theremaining X's belong to the other object.

The technician will always click on an X, selecting the objectcontaining that X. The coordinates of the click are recorded. Rowsand columns are numbered starting from 1 in the upper left handcorner. The technician could select the object in Grid 1 byclicking on row 2 and column 2. The larger object in Grid 2 couldbe selected by clicking on row 2, column 3. The click could not beon row 4, column 3.

zoj <wbr>1047 <wbr>Image <wbr>Perimeters


One useful statistic is the perimeter of the object. Assume each Xcorresponds to a square one unit on each side. Hence the object inGrid 1 has perimeter 8 (2 on each of four sides). The perimeter forthe larger object in Grid 2 is illustrated in the figure at theleft. The length is 18.

Objects will not contain any totally enclosed holes, so theleftmost grid patterns shown below could NOT appear. The variationson the right could appear:

zoj <wbr>1047 <wbr>Image <wbr>Perimeters

The input will contain one or more grids. Each gridis preceded by a line containing the number of rows and columns inthe grid and the row and column of the mouse click. All numbers arein the range 1-20. The rows of the grid follow, starting on thenext line, consisting of '.' and 'X' characters.

The end of the input is indicated by a line containing four zeros.The numbers on any one line are separated by blanks. The grid rowscontain no blanks.

For each grid in the input, the output contains a single line withthe perimeter of the specified object.


Example input:

2 2 2 2
XX
XX
6 4 2 3
.XXX
.XXX
.XXX
...X
..X.
X...
5 6 1 3
.XXXX.
X....X
..XX.X
.X...X
..XXX.
7 7 2 6
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
7 7 4 4
XXXXXXX
XX...XX
X..X..X
X..X...
X..X..X
X.....X
XXXXXXX
0 0 0 0


Example output:

8
18
40
48
8


Source: Mid-Central USA 2001
源码:

zoj <wbr>1047 <wbr>Image <wbr>Perimeters
zoj <wbr>1047 <wbr>Image <wbr>Perimeters
解题报告:
   题目大意,对于给出的一个网格图,"X"代表有物体,"."代表没有物体,某一"X"他的八个
   方向上如果也有“X”,说明他们是属于一个物体,现在给出某一个点,求出包含这个点的物体
    的周长。
   算法思想:本题属于搜索题,深搜即可,从给出的点开始,对于8个方向要加上一个上下左右的判断
   即可,如果发现为“.”,全局变量ans++即可。

0 0