Oil Deposits HDU 1241 基础BFS

L – Oil DepositsTime Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 1241DescriptionThe 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.InputThe input file contains one or more grids. Each grid begins with a line containing m and 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 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters 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. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output0 1 2 2//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<vector>#include<stack>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=105;const int inf=200000;#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define For(i,n) for(int i=0;i<(n);i++)template<class T>inline T read(T&x){char c;while((c=getchar())<=32);bool ok=false;if(c=='-')ok=true,c=getchar();for(x=0; c>32; c=getchar())x=x*10+c-'0';if(ok)x=-x;return x;}template<class T> inline void read_(T&x,T&y){read(x);read(y);}template<class T> inline void write(T x){if(x<0)putchar('-'),x=-x;if(x<10)putchar(x+'0');else write(x/10),putchar(x%10+'0');}template<class T>inline void writeln(T x){write(x);putchar('\n');}// ——-IO template——struct node{int x,y;node(int a,int b){x=a;y=b;}};int dx[]={0,0,1,-1,1,-1,1,-1};int dy[]={-1,1,0,0,-1,1,1,-1};bool vis[maxn][maxn];char a[maxn][maxn];int n,m;void bfs(int x,int y){queue<node> q;vis[x][y]=true;q.push(node(x,y));while(!q.empty()){node s=q.front();q.pop();For(i,8){int xx=dx[i]+s.x;int yy=dy[i]+s.y;if(xx>=n||xx<0||yy>=m||yy<0||a[xx][yy]!='@'||vis[xx][yy])continue;q.push(node(xx,yy));vis[xx][yy]=true;}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endif // ONLINE_JUDGEint i,j,k,t;while(~scanf("%d%d",&n,&m)&&(n||m)){For(i,n)scanf("%s",a[i]);int ans=0;memset(vis,0,sizeof(vis));For(i,n)For(j,m){if(!vis[i][j]&&a[i][j]=='@'){bfs(i,j);ans++;}}writeln(ans);}return 0;}

,人生好如足球赛,看自家总是无奈,对人家总是优待,

Oil Deposits HDU 1241 基础BFS

相关文章:

你感兴趣的文章:

标签云: