ACM_魔仙岛探险(深搜)

魔仙岛探险

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

小敏通过秘密方法得到一张不完整的魔仙岛航拍地图。魔仙岛由一个主岛和一些附属岛屿组成,小敏决定去魔仙岛探险。
二维矩阵10*10代表魔仙岛的航拍地图。图中数字表示海拔,0表示海洋,1~9都表示陆地。
小敏的飞机将会降落在二维矩阵x行y列处(数据保证一定会降落在岛屿上),现在需要计算出小敏降落所在岛的面积(即有多少个格子)。
注意此处我们把与小敏降落点上下左右相链接的陆地均视为同一岛屿。

Input:

输入有多组数据
第一行4个整数,前两个整数n, m表示二维矩阵n行m列,后两个整数x, y表示降落的坐标x行y列。(1 <= x <= n <= 100, 1 <= y <= m <= 100)
接下来的第二行开始输入一个二维矩阵。

Output:

一个整数表示岛屿的面积,每组输出占一行。

Sample Input:

10 10 5 1
1 2 1 0 5 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
5 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

Sample Output:

10
解题思路:简单的搜索,水过。
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,x,y,cnt,s[105][105],dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
 4 void dfs(int x,int y){
 5     if(x<0||x>=n||y<0||y>=m||s[x][y]==0)return;//边界条件
 6     s[x][y]=0;cnt++;
 7     for(int i=0;i<4;++i)
 8         dfs(x+dir[i][0],y+dir[i][1]);
 9 }
10 int main(){
11     while(~scanf("%d%d%d%d",&n,&m,&x,&y)){
12         for(int i=0;i<n;++i)
13             for(int j=0;j<m;++j)
14                 scanf("%d",&s[i][j]);
15         cnt=0;dfs(x-1,y-1);
16         cout<<cnt<<endl;
17     }
18     return 0;
19 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9169235.html
今日推荐