原题:
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..
..... ..... ..... .....
题意:
woc这题好长,题意是给出一个矩阵,然后给出一个X 的位置,找出与这个X八个方向相邻的所有X组成的图形的周长。
题解:
搜索X不是很难,但是怎么表示周长有点难度,借鉴了一篇博客,就是图形(内)边如果被两个X包围那么周长为2,不然周长为1。别的就是各个方向上的深搜了没什么好说的。(困死了不行了十二点了)
代码:AC
#include<iostream>
#include<cstring>
#define MAXN 30
using namespace std;
char _m[MAXN][MAXN];
bool mark[MAXN][MAXN];
int dir_f[4][2] = {0,1,0,-1,1,0,-1,0};
int dir_l[4][2] = {1,1,1,-1,-1,1,-1,-1};
int ans;
int r;
int c;
void DFS(int g_i,int g_j);
int main()
{
int i;
int j;
int g_r;
int g_c;
while(cin>>r>>c>>g_r>>g_c)
{
memset(mark,false,sizeof(mark));
if(!r && !c && !g_r && !g_c)
{
break;
}
for(i = 0; i < r; ++ i)
{
for(j = 0; j < c; ++ j)
{
cin>>_m[i][j];
}
}
-- g_r;
-- g_c;
ans = 0;
mark[g_r][g_c] = true;
DFS(g_r,g_c);
cout<<ans<<endl;
}
}
void DFS(int g_i,int g_j)
{
int i;
int j;
int tem_i;
int tem_j;
for(i = 0; i < 4; ++ i)
{
if(dir_f[i][0] + g_i >= 0 && dir_f[i][0] + g_i < r && dir_f[i][1] + g_j>= 0 && dir_f[i][1] + g_j < c && _m[dir_f[i][0] + g_i][dir_f[i][1] + g_j] == 'X' && !mark[dir_f[i][0] + g_i][dir_f[i][1] + g_j])
{
mark[dir_f[i][0] + g_i][dir_f[i][1] + g_j] = true;
DFS(dir_f[i][0] + g_i,dir_f[i][1] + g_j);
}
else if(!mark[dir_f[i][0] + g_i][dir_f[i][1] + g_j])
{
++ ans;
}
}
for(i = 0; i < 4; ++ i)
{
tem_i = dir_l[i][0] + g_i;
tem_j = dir_l[i][1] + g_j;
if(tem_i >= 0 && tem_i < r && tem_j >= 0 && tem_j < c && _m[tem_i][tem_j] == 'X' && !mark[tem_i][tem_j])
{
mark[tem_i][tem_j] = true;
DFS(tem_i,tem_j);
}
}
}