K
来源:互联网 发布:世界十大袜子品牌 知乎 编辑:程序博客网 时间:2024/04/28 13:29
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
Sample Output
0122
简单的BFS枚举油田即可
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int maxn = 100+10;const int INF = 0x3f3f3f3f;int dx[8] = {0,0,1,1,-1,-1,1,-1};int dy[8] = {1,-1,1,-1,1,-1,0,0};int n,m,cnt,ans,vist[maxn][maxn];char Map[maxn][maxn];int dist[maxn][maxn];struct node{ int x,y;}temp1,temp2,a[10010];void BFS(node a){ queue<node>Q; Q.push(a); vist[a.x][a.y] = 1; while(!Q.empty()){ temp1 = Q.front(); Q.pop(); for(int i = 0; i < 8; i++){ temp2.x = temp1.x+dx[i]; temp2.y = temp1.y+dy[i]; if(temp2.x>=0 && temp2.x<n && temp2.y>=0 && temp2.y<m&&!vist[temp2.x][temp2.y] && Map[temp2.x][temp2.y] == '@'){ vist[temp2.x][temp2.y] = 1; Q.push(temp2); } } }}int main(){ while(~scanf("%d %d",&n,&m) && (n||m)){ cnt = ans = 0; for(int i = 0; i < n; i++){ scanf("%s",Map[i]); for(int j = 0; j < m; j++){ if(Map[i][j] == '@'){ a[cnt].x = i; a[cnt++].y = j; } } } memset(vist,0,sizeof(vist)); for(int i = 0; i < cnt; i++){ if(!vist[a[i].x][a[i].y]){ ans++; BFS(a[i]); } } printf("%d\n",ans); } return 0;}