UVA-572
Oil deposits
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
The Geosurvcomp Geologic Survey company are responsible for detecting underground oil deposits. Geosurvcomp works with one large rectangular region of land at a time, and creates a grid that divides the land into Numer OUs square plots. It then analyzes each plot separately, using a sensing equipment to determine whether or not of the plot contains oil.
A plot containing oil is called a pocket. If the pockets is adjacent, then they is part of the same oil deposit. Oil deposits can is quite large and may contain numerous pockets. Your job is to determine how many different oil deposits be contained in a grid.
InputThe input file contains one or more grids. Each grid begins with a line containing
mand
N, the number of rows and columns in the grid, separated by a single space. If
m= 0 It signals the end of the input; otherwise and. Following this is
mLines of
NCharacters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either '*', representing the absence of oil, or '@', representing an oil pocket.
OutputFor each grid, output the number of distinct oil deposits. The different pockets is part of the same oil deposit if they is adjacent horizontally, vertically, or diagonally. An oil deposit won't contain more than pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
Miguel A. Revilla
1998-03-10
Source
Root:: Competitive programming:increasing The Lower Bound of programming contests (Steven & Felix Halim):: Chapter 4. Graph:: Depth First Search:: Finding Connected Components/flood Fill
Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 6. Data Structures:: Examples
Root:: AOAPC i:beginning algorithm Contests (Rujia Liu):: Volume 2. Data Structures:: Graphs
Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Graph:: Graph traversal:: Flood fill/finding Connected components
Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Graph:: Grap H Traversal:: Flood fill/finding Connected components
Use DFS, and then recursively
AC Code:
#include <cstdio> #include <cstring> #include <algorithm> using namespace Std;char map[105][105];int vis[105][105];void DFS (int i, int j) {if (map[i][j] = = ' * ' | | vis[i][j]) RETURN;VIS[I][J] = 1;dfs (i-1, j-1); Dfs (I-1, j);
dfs (i-1, j+1);d FS (i, j-1); DFS (i, j+1);d FS (i+1, j-1); DFS (I+1, j); DFS (i+1, j+1);} int main () {int m, n;while (scanf ("%d%d", &m, &n), M | | n) {memset (Vis, 0, sizeof (VIS)); All grids have not been visited for (int i=0; i<105; i++) //The outer edge of the initialization graph is ' * ' {for (int j=0; j<105; J + +) {Map[i][j] = ' * ';}} char a[105];for (int i=1; i<=m; i++) //input {scanf ("%s", a), for (int j=1; j<=n; J + +) {Map[i][j] = a[j-1];}} int count = 0;for (int i=1; i<=m; i++) //dfs application {for (int j=1; j<=n; J + +) {if (!vis[i][j] && map[i][j] = = ' @ ') {count++;d fs (i, j);}}} printf ("%d\n", count);} return 0;}
Uva-572-oil deposits (figure of the dfs! )