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){
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){
相关文章推荐
- poj&nbsp;Light&nbsp;Up&nbsp;dfs
- ACM: dfs题 poj 1321 (n皇后问题)
- ZZULI 1497 Reading Books
- ZZULI_SummerPractice(4) POJ 2312…
- ZZULI_TEAM_PRACTICE(1) POJ 2259…
- ZOJ 1539(ZZULI 1606) LOT (递…
- bzoj 1597 [Usaco2008 Mar]土地购…
- bzoj 1597 [Usaco2008 Mar]土地购…
- ZZULI_SummerPractice(3) POJ 12…
- HDU 1051(ZZULI 1609) Wooden St…
- POJ-3026--Borg&nbsp;Maze
- 求欧拉回路的路径 dfs的一种新运用
- ZZULI_SummerPractice(3) POJ 3984…
- HDU 4160 (ZZULI 1598) Dolls(二分…
- poj&nbsp;1655&nbsp;balancing&nbsp;act&nbsp;dfs
- ZZULI Greedy Gift Givers Greedy…
- ZZULI_SummerPractice(3) POJ 3598…
- ZZULI_SummerPractice(6) POJ 296…
- ZZULI_SUMMER_PRACTICE(8) POJ 10…
- ZZULI_SummerPractice(3) HDU 1…