原题链接

题目大意:鼠标点击一块,求与之联通的所有区域的边长之和。

解法:广度优先搜索。从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记。如果该点上下左右的一个或多个方向没有相邻的点,边长+1。代码BSF函数中,有两个数组存放相邻8个点的坐标,这一段代码感觉很简洁,是从其他地方学习来的。

 

参考代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

int r,c,x,y,i,j,ans;
int visited[22][22],grid[22][22];
char s;
int BFS(int i,int j);

struct Node{
	int x,y;
}p,q;

int main(){
	

	while(cin>>r>>c>>x>>y){
		if(r==0&&c==0&&x==0&&y==0)break;
		memset(grid,0,sizeof(grid));
		memset(visited,0,sizeof(visited));
		ans=0;
		for(i=1;i<=r;i++){
			for(j=1;j<=c;j++){
				cin>>s;
				if(s=='X')grid[i][j]=1;
				if(s=='.')grid[i][j]=0;
			}
			getchar();
		}								//first fill the matrix with 0 or 1
		ans=BFS(x,y);					// do broad first search in this matrix
		cout<<ans<<endl;
	}
	
	return 0;
}

int BFS(int i,int j){
	int k,num,x0,y0;
	int sx[9]={0,-1,1,0,0,-1,-1,1,1};
	int sy[9]={0,0,0,-1,1,-1,1,-1,1};
	queue<Node> point;

	p.x=i;
	p.y=j;
	point.push(p);			//push the first node in queue
	visited[x][y]=1;

	while(!point.empty()){		//if the queue is not empty, pop the front, visit it and push its neighbours
		q=point.front();
		point.pop();
		num=0;
		for(k=1;k<=8;k++){
			x0=q.x+sx[k];
			y0=q.y+sy[k];
			if(grid[x0][y0]){
				if(k<=4)num++;		//another 'X' is next to this side
				if(!visited[x0][y0]){
					visited[x0][y0]=1;
					p.x=x0;
					p.y=y0;
					point.push(p);
				}
			}					//until the queue is empty
		}
		ans+=4-num;
	}
	return ans;
}