The GeoSurvComp geologic survey company is 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 numerous square plots.
It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large
and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int sum;
int n , m;
int v[105][105];
char Map[105][105];
int d[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
struct node{
int x, y;
}pre, nex;
void bfs(int x,int y) {
queue<node> q;
pre.x = x;
pre.y = y;
v[x][y] = 1;
q.push(pre);
while(!q.empty()) {
nex = q.front();
q.pop();
for(int i = 0; i < 8; i++) {
int dx = d[i][0] + nex.x;
int dy = d[i][1] + nex.y;
if(dx >= 0 && dy >= 0 && dx < n && dy < m) {
if(Map[dx][dy] == '@' && v[dx][dy] == 0) {
pre.x = dx;
pre.y = dy;
v[dx][dy] = 1;
q.push(pre);
}
}
}
}
return ;
}
int main() {
while(~scanf("%d%d",&n,&m) && (m+n)) {
sum = 0;
for(int i = 0; i < n; i++)
scanf("%s",Map[i]);
memset(v, 0, sizeof(v));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(Map[i][j] == '@' && v[i][j] == 0) {
bfs(i,j);
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
油田探测算法
129

被折叠的 条评论
为什么被折叠?



