宝岛探险

#include <iostream>
using namespace std;
bool book[101][101];
int a[101][101],n,m,step=1;
void dfs(int x,int y)
{
     int next[4][2]={{0,1}, 
                     {1,0},
                     {0,-1},
                     {-1,0}};
     int tx,ty;
     for(int i=0;i<4;i++)
     {
          tx=x+next[i][0];
          ty=y+next[i][1];
          if(tx<1 || tx>n || ty<1 || ty>m)
              continue;
          if(a[tx][ty]>0 && book[tx][ty]==0)
          {
              step++;
              book[tx][ty]=1;
              dfs(tx,ty);
          }
     }
     return;
}
int main()
{ 
    int sx,sy;
    cin>>n>>m>>sx>>sy;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    book[sx][sy]=1;
    dfs(sx,sy);
    cout<<step;

}
    
View Code

 

试题描述
 YSF 通过秘密方法得到一张不完整的航拍地图。钓鱼岛由一个主岛和一些附属岛组成。数字表示海拔高度。0 表示海洋,1 到 9 都表示陆地。现在要计算出 YSF 跳伞所在岛的面积。此处我们把 YSF 的跳伞点上下左右相连接陆地均视为同一岛屿。
输入
第一行四个整数:n,m,startx,starty,表示地图有 n 行 m 列,降落点的初始坐标为(startx,starty),接下来的 n 行 m 列为地图各点的海拔高度。
输出
一个数,表示 YSF 跳伞所在岛的面积。
输入示例
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
其他说明
数据范围:地图的大小不超过50*50。
posted @ 2017-03-18 11:25  Dijkstra·Liu  阅读(302)  评论(0编辑  收藏  举报