Rescue Oibh Headquarters (from http://acm.qust.edu.cn/problem.php?id=1101)
Oibh was flooded by the sudden floods. > .< Fortunately, Oibh headquarters has some walls in some important places, using the *, and a closed area of the * No.
Water is not going to go in ... Now give Oibh's wall-building map and ask how much of the important area where the OIBH headquarters is not flooded (indicated by "0").
Input
The first line is two digits, X and Y (x,y< =500) The second row and below is a graph of x*y composed of * and 0.
Output
Output the number of "0" of OIBH headquarters that are not flooded.
Sample input
5 4
00000
00*00
0*0*0
00*00
Sample output
1
Main.cpp//dfs-Save OIBH Headquarters///Created by Showlo on 2018/4/19. COPYRIGHT©2018 year Showlo.
All rights reserved.
#include <stdio.h> #include <algorithm> #include <math.h> using namespace std;
#define MAX 1000 int m,n;
Char A[max][max];
int Vis[max][max];
void Dfs (int x,int y) {int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1}; if (a[x][y]== ' * ' | | vis[x][y]==1| | x<0| | x>=m| | y<0| |
Y>=n) return;
else{//printf ("%d%d\n", x,y);
Vis[x][y]=1;
for (int i=0;i<4;i++) DFS (X+dx[i], y+dy[i]);
} return;
int main () {int i,j,ans=0;
scanf ("%d%d", &n,&m);
memset (Vis, 0, sizeof (VIS));
For (i=0 i<m; i++) {scanf ("%s", A[i]);
For (i=0 i<m; i++) {if (a[i][0]!= ' * ' &&vis[i][0]==0) DFS (i,0);
if (a[i][n-1]!= ' * ' &&vis[i][n-1]==0) DFS (i,n-1); For (I=1 i<n-2; i++) {if (a[0][i]!= ' * ' &&vis[0][i]==0) DFS (0,i);
if (a[m-1][i]!= ' * ' &&vis[m-1][i]==0) DFS (m-1,i);
For (i=0 i<n; i++) {for (j=0; j<m; J + +) {if (a[i][j]== ' 0 ' &&vis[i][j]==0) {
ans++;
} printf ("%d\n", ans);
return 0;
}