Title Link: http://poj.org/problem?id=3620
DFS Basic Questions ~
#include <cstdio> #include <iostream> #include <sstream> #include <cstdlib> #include < cstring> #include <string> #include <climits> #include <cmath> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <set>using namespace std; #define MAXN 100+5# Define INF 0x3f3f3f3fint n,m,k,fi,fj,sum;int map[maxn][maxn],vis[maxn][maxn];int dir[4][2]= {0,1,0,-1,1,0,-1,0};bool Find () {for (int i=1, i<=n; i++) for (int j=1; j<=m; J + +) if (Map[i][j]&&!vis[i][j]) {fi=i; Fj=j; return 1; } return 0;} void DFS (int i,int j) {for (int k=0; k<4; k++) {int a = i + dir[k][0]; int B = j + dir[k][1]; if (a>=1 && a<=n && b>=1 && b<=m &&!vis[a][b] && map[a][b]) { VIS[A][B] = 1; DFS (A, b); sum++; }}}int MAIn () {int i,j; while (~SCANF ("%d%d%d", &n,&m,&k)) {memset (map,0,sizeof (map)); memset (vis,0,sizeof (VIS)); int A, B; for (I=1; i<=k; i++) {scanf ("%d%d", &a,&b); Map[a][b]=1; } int result=-inf; while (find ()) {vis[fi][fj]=1; Sum=1; DFS (FI,FJ); Result=max (result,sum); } printf ("%d\n", result); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3620 Avoid the Lakes (DFS)