本来就是超水的一道DFS,但是自己还是做了很长时间,DFS一开始做,就是不知道怎么回溯或者怎么退出,真头疼,自己真水。。。。 本题求相邻最大 陆地潮湿面积 ,也就是一片潮湿陆地共有多少块 #include <stdio.h> #include <stdlib.h> #include <memory.h> #define max 103 int N,K,M; //bool visit[max][max]; int lakes[max][max]; //我用的bool ,poj老是CE int lakemax; int dir[4][2] = {0,1,0,-1,1,0,-1,0}; void dfs(int x,int y) { int i; if(!lakes[x][y]) return ; lakemax ++; lakes[x][y] = 0; for(i = 0;i < 4;i++) { int tx = x + dir[i][0]; int ty = y + dir[i][1]; dfs(tx,ty); } } int main() { int x,y, i, j; int result = 0; scanf("%d%d%d",&N,&M,&K); memset(lakes,0,sizeof(lakes)); for(i = 1;i <= K;i++) { scanf("%d%d",&x,&y); lakes[x][y] = 1; } for(i = 1;i <= N;i++) for(j =1;j <= M;j++) { if(lakes[i][j]) { lakemax =0; dfs(i,j); if(lakemax > result) result = lakemax; } } printf("%d/n",result); return 0; }