首页 > 代码库 > hdu 1241Oil Deposits(BFS)

hdu 1241Oil Deposits(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13137    Accepted Submission(s): 7611


Problem 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 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.
 

 

Output
For 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 Output
0
1
2
2
 
题目大意:
这个题目就是要查找油田的个数,比如说第二组数据
3 5
*@*@*
**@**             注明:"@"这个与他的几个方向都是@都是可以相连的。所以称为一块油田。输出1。
*@*@*
 这种就很容易想到搜索。这里的一个技巧就是起点就从@开始,其次就是找到@就使其变成*;不过找过的还是要标记为1,否则就会重复,这样不连接的还可以继续搜~
东西还是要反复的咀嚼,不然就会很生,忘得差不多0.0
 
详见代码。
 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5  6 using namespace std; 7  8 int dir[8][2]= {0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1}; 9 char map[110][110];10 int a,b,vis[110][110],k;11 12 struct node13 {14     int x,y;15     int t;16 } s,ss;17 18 queue<node>q,qq;19 int bfs()20 {21     while (!q.empty())22     {23         s=q.front();24         q.pop();25         //vis[s.x][s.y]=1;26         for (int i=0; i<8; i++)27         {28             int x=s.x+dir[i][0];29             int y=s.y+dir[i][1];30             //int t=s.t+1;31             if (x>=0&&x<a&&y>=0&&y<b)32             {33                 if (!vis[x][y]&&map[x][y]==@)34                 {35                     ss.x=x;36                     ss.y=y;37                     map[ss.x][ss.y]=*;38                     //vis[x][y]=1;39                     q.push(ss);40                 }41 42             }43         }44     }45 }46 47 int main ()48 {49     while (scanf("%d%d",&a,&b)!=EOF)50     {51         memset(vis,0,sizeof(vis));52         if (a==0&&b==0)53             break;54         for (int i=0; i<a; i++)55         {56             getchar();57             for (int j=0; j<b; j++)58             {59                 scanf("%c",&map[i][j]);60             }61         }62         //int k=0;63         for (int i=k=0; i<a; i++)64         {65             for (int j=0; j<b; j++)66             {67                 if (map[i][j]==@)68                 {69                     k++;70                     s.x=i;71                     s.y=j;72                     map[s.x][s.y]=*;73                     vis[s.x][s.y]=1;74                     q.push(s);75                     bfs();76                 }77             }78         }79         printf ("%d\n",k);80     }81     return 0;82 }

 

 

hdu 1241Oil Deposits(BFS)