Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8790   Accepted: 5260

Description

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

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

XX   Grid 1       .XXX   Grid 2

XX .XXX

.XXX

...X

..X.

X...

An X in a grid square indicates that the entire grid square, including its boundaries, lies in some object. The X in the center of the grid below is adjacent to the X in any of the 8 positions around it. The grid squares for any two adjacent X's overlap on an edge 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 be linked to one another through a sequence of adjacent X's. In Grid 1, the whole grid is filled by one object. In Grid 2 there are two objects. One object contains only the lower left grid square. The remaining X's belong to the other object.

The technician will always click on an X, selecting the object containing that X. The coordinates of the click are recorded. Rows and columns are numbered starting from 1 in the upper left hand corner. The technician could select the object in Grid 1 by clicking on row 2 and column 2. The larger object in Grid 2 could be selected by clicking on row 2, column 3. The click could not be on row 4, column 3.



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

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

Impossible   Possible 

XXXX         XXXX   XXXX   XXXX

X..X XXXX X... X...

XX.X XXXX XX.X XX.X

XXXX XXXX XXXX XX.X ..... ..... ..... .....

..X.. ..X.. ..X.. ..X..

.X.X. .XXX. .X... .....

..X.. ..X.. ..X.. ..X..

..... ..... ..... .....

Input

The input will contain one or more grids. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the mouse click. All numbers are in the range 1-20. The rows of the grid follow, starting on the next 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 rows contain no blanks.

Output

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

Sample 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

Sample Output

8
18
40
48
8

Source

裸BFS或者DFS都可以……算目标点所在的X块儿的周长,其实就是统计周围的点的数量(由于实际上是算周长,点可以重复统计)。

 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int mx[]={,,,-,,,,-,-},
my[]={,,,,-,,-,,-};
char mp[][];
int n,m,sx,sy;
int DFS(int x,int y){
if(mp[x][y]!='X')return ;
mp[x][y]='';
int ans=,i;
for(i=;i<=;i++)
if(mp[x+mx[i]][y+my[i]]=='.')ans++;
for(i=;i<=;i++){
ans+=DFS(x+mx[i],y+my[i]);
}
return ans;
}
int main(){
while(scanf("%d%d%d%d",&n,&m,&sx,&sy) && n && m){
int i,j;
char c[];
memset(mp,'.',sizeof(mp));
for(i=;i<=n;i++){
scanf("%s",c);
for(j=;j<=m;j++){
mp[i][j]=c[j-];
}
}
printf("%d\n",DFS(sx,sy));
}
return ;
}

POJ1111 Image Perimeters的更多相关文章

  1. poj1111 Image Perimeters 广搜

    题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标 ...

  2. 【编程练习】poj1111

    Image Perimeters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8632   Accepted: 5168 ...

  3. poj1111(单身快乐)

                                                                                                         ...

  4. ZOJ 1047 Image Perimeters

    原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...

  5. poj1111 DFS

    J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit I ...

  6. B - Image Perimeters

    Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected f ...

  7. 深搜(DFS),Image Perimeters

    题目链接:http://poj.org/problem?id=1111 解题报告: 1.这里深搜有一点要注意,对角线上的点,如果为'.',则total不应该增加,因为这不是他的边长. #include ...

  8. POJ1111【BFS】

    在搜1011的时候误搜了1111,简单BFS吧,多一个X就是多四个面,每次看看他的四个面有多少个重复的,然后剪掉,最后答案加上就好了: code: //#include <bits/stdc++ ...

  9. [POJ]1111 Image Perimeters

    Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide ar ...

随机推荐

  1. 1037: [ZJOI2008]生日聚会Party

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3121  Solved: 1858[Submit][Status][Discuss] Descript ...

  2. linux下通过phpize为php在不重新编译php情况下安装模块memcache

    通过phpize为php在不重新编译php情况下安装模块memcache 1. 下载    wget http://pecl.php.net/get/memcache-2.2.4.tgz     解压 ...

  3. myeclipse10.5 crack(2012-12-27-bd 写的日志迁移

    首先去网上下一个破解文件如图: 解压过后打开的文件夹如图: 再打开crack文件夹如图: 运行run.bat如果点击它没反应就是你没有安装jdk,它如果运行就如图所示: 到这一步就在第一个方框user ...

  4. JZOJ 4743. 积木

    Description Input Output Sample Input 38 7 63 9 41 10 5 Sample Output 18

  5. python3 练习题100例 (十四)

    今天逛贴吧,看到有人求助,做了一下.请大家指正! #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' imp ...

  6. bootmem_free_node

    该函数设置: 1.pgdata节点的成员 2.pgdata->zone的成员 3.初始化zone->free_area 4.初始化zone所包含的所有页对应的页框描述符page结构体 /* ...

  7. LCS及方案数(DP)

    Description 对于一个序列

  8. DOS中断及程序调用

    http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html

  9. 其它- in-place

    in-place 刷编程题的时候,经常遇到题目要求do in-place.所以就上网搜了相关概念,简单总结一下.   in-place操作,意思是所有的操作都是”就地“操作,不允许进行移动,或者称作  ...

  10. 如何在C#中调试LINQ查询

    原文:How to Debug LINQ queries in C# 作者:Michael Shpilt 译文:如何在C#中调试LINQ查询 译者:Lamond Lu 在C#中我最喜欢的特性就是LIN ...