您的位置:首页 > 其它

ZZULI 1597 Maze(果断DFS)

2012-12-05 17:14 253 查看
Maze
Time Limit:1000MS  Memory
Limit:65536KTotal Submit:6 Accepted:3
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 areXX Grid 1 .XXX Grid 2XX .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.XXXXXX Central X and adjacent X'sXXXAn 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 PossibleXXXX XXXX XXXX XXXXX..X XXXX X... X...XX.X XXXX XX.X XX.XXXXX 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


Sample
Output


Source
 现在看来,其实如果数据比较强的话,这道题还是最简单的,可是我比赛的时候从头到尾竟然没有看这道题,看着那么多的人过1598和1599,就跟着他们做了,没想到啊……
 题意,一个以X围成的物体,每个边占用一个单位长度,求该物体的周长……
 
果断DFS,不解释!!!
 
代码:C语言: 高亮代码由发芽网提供
#include<stdio.h>
#include<string.h>
int
num,m,n,flag[22][22];
char
str[22][22];
int
dir[8][2]={0,1,0,-1,1,0,-1,0,1,-1,1,1,-1,1,-1,-1};
void
DFS(int x,int y){       int
i,a,b,ff=0;       if(flag[x][y])               return;       else
flag[x][y]=1;       a=x;b=y;       for(i=0;i<8;i++)       {               x=a+dir[i][0];               y=b+dir[i][1];               if(x<=0||x>m||y<=0||y>n||str[x][y]=='.')               {                       if(i<4)                               num++;               }               else                       DFS(x,y);       }
}
int
main()
{       int
x,y,i,j;       while(scanf("%d%d%d%d",&m,&n,&x,&y),m!=0||n!=0||x!=0||y!=0){       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: