本帖最后由 黑豆 于 2016-11-19 20:44 编辑
可参考我刚写的代码:[mw_shl_code=applescript,true]#include <stdio.h>
#include <stdlib.h>
#define MAX 101
//Map
int data[MAX][MAX] = {0};
//whether be searched
int flag[MAX][MAX] = {0};
int n = 0,m =0;
int moveX[4] = {0, 0, -1, 1};
int moveY[4] = {-1, 1, 0, 0};
int result = 0;
void DFS(int x, int y) {
int i;
int tempx,tempy;
for (i = 0; i < 4; i++){
tempx = x + moveX;
tempy = y + moveY;
if (flag[tempy][tempx] != 1 && data[tempy][tempx] > 0) {//not mark and it is land
result++;
flag[tempy][tempx] = 1;
DFS(tempx, tempy);
}
}
}
int main()
{
int i,j,x,y;
//freopen("input.txt", "r", stdin);
//freopen("output.txt","w",stdout);
scanf("%d%d%d%d", &n,&m,&x,&y);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++){
scanf("%d", &data[j]);
}
//(6,8),8应该是上往下算第8行
DFS(y,x);
printf("%d", result);
return 0;
}
[/mw_shl_code]
|