题目描述
小哼通过秘密方法得到一张不完整的钓鱼岛航拍地图。钓鱼岛由一个主岛和一些附属岛屿组成,小哼决定去钓鱼岛探险。下面这个10*10的二维矩阵就是钓鱼岛的航拍地图。图中数字表示海拔,0表示海洋,1~9都表示陆地。小哼的飞机将会降落在(6,8)处,现在需要计算出小哼降落所在岛的面积(即有多少个格子)。注意此处我们把与小哼降落点上下左右相链接的陆地均视为同一岛屿。
输入
多组输入n,m,x,y
n<=100
m<=100
0<x<=n
0<y<=m
其后n*m个数字
输出
输出面积
样例输入复制
10 10 6 8 1 2 1 0 0 0 0 0 2 3 3 0 2 0 1 2 1 0 1 2 4 0 1 0 1 2 3 2 0 1 3 2 0 0 0 1 2 4 0 0 0 0 0 0 0 0 1 5 3 0 0 1 2 1 0 1 5 4 3 0 0 1 2 3 1 3 6 2 1 0 0 0 3 4 8 9 7 5 0 0 0 0 0 3 7 8 6 0 1 2 0 0 0 0 0 0 0 0 1 0
样例输出复制
38
#include<iostream>
#include<cstring>
#include <queue>
using namespace std;
const int maxn = 100+5;
int map[maxn][maxn];
int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};
int n,m,x,y,area;
void DFS(int a,int b){
area++;
map[a][b]=0;
for(int i=0;i<4;i++){
int nx = a+dx[i];
int ny = b+dy[i];
if(nx>=n || nx<0 || ny>=m || ny<0 || map[nx][ny]==0)continue;
DFS(nx,ny);
}
}
int main(){
while(cin>>n>>m>>x>>y){
area = 0;
memset(map, 0, sizeof(map));
for (int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin >> map[i][j];
DFS(--x,--y);
cout<<area<<endl;
}
return 0;
}