Image Perimeters
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 |
题意:先输入一个矩阵,再输入一个X的位置,然后输出与这个X在八个方向能够连起来的所有X组成的图形的周长。
解题思路:拿到这个题下意识的想到了用DFS。我的解法是先用DFS去遍历网格,然后给连通起来的八连块分别附上标号(属于同一个八连块的赋一样的值),这个题目也不需要将所有的网格都扫描一遍,因为题目说的很清楚,只需要求给出的那个点所在的八连块的周长。DFS遍历一遍后,那个点所在的八连块flag一定是1。所以,我再开个双重循环扫描一次flag数组中为1的元素周边,如果是非1,就周长自加。这里,有个边界问题(坑了我无数次WA),我取巧写的,在存入字符串的时候,我是先给所有的元素赋值'.',然后从1开始接收(用%c的时候要注意回车),扫描的时候也是从1开始扫描,这样判断的时候就不用考虑越界的情况了。
这个题目是本人写出的第一个DFS题,还是有一些成就感的,WA四次终于AC。附上代码
#include <stdio.h> #include <string.h> #include <memory.h> #define maxn 25 int m,n,flag[maxn][maxn]; char pic[maxn][maxn]; void dfs(int r,int s,int num) { if(r<0||r>=m+1||s<0||s>=n+1) return; if(pic[r][s]!='X'||flag[r][s]>0) return; flag[r][s]=num; for(int i=-1; i<=1; i++) for(int j=-1; j<=1; j++) if(i!=0||j!=0) dfs(r+i,s+j,num); } int main() { //freopen("in.txt","r",stdin); int a,b,num,sum; while(scanf("%d%d%d%d",&m,&n,&a,&b)!=EOF) { getchar(); if(m==0&&n==0&&a==0&&b==0) return 0; memset(flag,0,sizeof(flag)); for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) pic[i][j]='.'; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) scanf("%c",&pic[i][j]); getchar(); } num=sum=0; for(int i=a; i<=m; i++) for(int j=b; j<=n; j++) if(flag[i][j]==0&&pic[i][j]=='X') dfs(i,j,++num); for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) { if(flag[i][j]==1) { if(pic[i+1][j]=='.') sum++; if(pic[i-1][j]=='.') sum++; if(pic[i][j+1]=='.') sum++; if(pic[i][j-1]=='.') sum++; } } printf("%d\n",sum); } return 0; }