POJ

来源:互联网 发布:js获取一个input的值 编辑:程序博客网 时间:2024/06/11 14:19

题目链接:http://poj.org/problem?id=1562点击打开链接


Oil Deposits
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18371 Accepted: 9727

Description

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.

Input

The input 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. 

Output

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 Output

0122

Source

Mid-Central USA 1997

广搜基础

八个方向将方向数组稍微改一下就行

#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <stack>#include <limits>#include <string>#include <string.h>#include <vector>#include <set>#include <map>#include <algorithm>#include <math.h>using namespace std;char mmap[111][111];int n;int m;struct xjy{    int x;    int y;};int dir[8][2]={0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1};queue<xjy> q;void bfs(int xx,int yy){    while(!q.empty())        q.pop();    xjy mid;    mid.x=xx;    mid.y=yy;    q.push(mid);    while(!q.empty())    {        mid=q.front();        q.pop();        xjy midmid;        for(int ii=0;ii<8;ii++)        {            midmid.x=mid.x+dir[ii][0];            midmid.y=mid.y+dir[ii][1];            if(mmap[midmid.x][midmid.y]=='@')            {                mmap[midmid.x][midmid.y]='*';                q.push(midmid);            }        }    }}int main(){   while(~scanf("%d%d",&n,&m)&&m)   {       getchar();       int ans=0;       memset(mmap,'*',sizeof(mmap));       for(int i=1;i<=n;i++)        {            scanf("%s",&mmap[i][1]);            getchar();        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)                if(mmap[i][j]=='@')                    {                        mmap[i][j]='*';                        bfs(i,j);                        ans++;                    }        }        printf("%d\n",ans);   }}