暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

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

问题分析:依旧是用的bfs,虽然说dfs更好。。。。。,但是用bfs的话要注意让它搜完再进行下一次搜索,直到把所有油田走完,。

注意:此题描述的退出条件有误,应该是吗n>0才会退出,而且“An oil deposit will not contain more than 100 pockets.”这句描述无意义。

  1 #include "iostream"
  2 #include "queue"
  3 using namespace std;
  4 struct person
  5 {
  6     int i;
  7     int j;
  8 };
  9 char o[105][105];
 10 void obegin(int n,int m)
 11 {
 12    int i,j;
 13     for (i=0;i<=n+1;i++)
 14      for (j=0;j<=m+1;j++)
 15     {
 16         if (i*j == 0 || i == n+1 || j == m+1)
 17             o[i][j] = ‘*‘;
 18         else
 19             cin>>o[i][j];
 20     }
 21 }
 22 int dfs(int n,int m)
 23 {
 24    queue <person> p;
 25    person fir,sec;
 26    int c=0;
 27    int f;
 28    for (int i=1;i<=n;i++)
 29     for (int j=0;j<=m;j++)
 30       if (o[i][j] == ‘@‘)
 31      {
 32          f=0;
 33          fir.i = i;
 34          fir.j = j;
 35          c++;
 36          p.push(fir);
 37          while (!p.empty())
 38          {
 39             sec = p.front();
 40             p.pop();
 41             for (int k=1;k<=8;k++)
 42             {
 43                 switch(k)
 44                 {
 45                   case 1: if (o[sec.i+1][sec.j] == ‘@‘)
 46                               {
 47                                   fir.i=sec.i+1;
 48                                   fir.j=sec.j;
 49                               }break;
 50                   case 2:if (o[sec.i-1][sec.j] == ‘@‘)
 51                               {
 52                                   fir.i=sec.i-1;
 53                                   fir.j=sec.j;
 54                               }break;
 55                   case 3:if (o[sec.i][sec.j+1] == ‘@‘)
 56                               {
 57                                   fir.i=sec.i;
 58                                   fir.j=sec.j+1;
 59                               }break;
 60                   case 4:if (o[sec.i][sec.j-1] == ‘@‘)
 61                               {
 62                                   fir.i=sec.i;
 63                                   fir.j=sec.j-1;
 64                               }break;
 65                   case 5:if (o[sec.i+1][sec.j+1] == ‘@‘)
 66                               {
 67                                   fir.i=sec.i+1;
 68                                   fir.j=sec.j+1;
 69                               }break;
 70                   case 6:if (o[sec.i+1][sec.j-1] == ‘@‘)
 71                               {
 72                                   fir.i=sec.i+1;
 73                                   fir.j=sec.j-1;
 74                               }break;
 75                   case 7:if (o[sec.i-1][sec.j-1] == ‘@‘)
 76                               {
 77                                   fir.i=sec.i-1;
 78                                   fir.j=sec.j-1;
 79                               }break;
 80                   case 8:if (o[sec.i-1][sec.j+1] == ‘@‘)
 81                               {
 82                                   fir.i=sec.i-1;
 83                                   fir.j=sec.j+1;
 84
 85                               }break;
 86                 }
 87                 if (o[fir.i][fir.j] == ‘@‘)
 88                 {
 89                     o[fir.i][fir.j]=‘#‘;
 90                     p.push(fir);
 91                     f++;
 92                 }
 93                 if (f/100 == 1)
 94                 {
 95                     f=0;
 96                     c++;
 97                 }
 98             }
 99          }
100      }
101      return c;
102 }
103 int main()
104 {
105    int n,m;
106    while (cin>>m>>n && n)
107    {
108         obegin(m,n);
109         cout<<dfs(m,n)<<endl;
110    }
111     return 0;
112 }

时间: 2024-08-06 03:46:06

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)的相关文章

暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了. 作为球迷,一定想看尽量

暑假集训(4)第七弹——— 组合(hdu1850)

题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++. 1 #include "cstdio" 2 int M[104]; 3 int main() 4 { 5 int m,sum,f; 6 while (scanf ("%d",&m) && m) 7 { 8 sum=f=0; 9 for (in

暑假集训(2)第五弹 ----- Who&#39;s in the Middle(poj2388)

G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median

暑假集训(4)第五弹——— 数论(hdu1222)

题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A又迟迟不现身,FFF团团长连下七道圣火令追杀你们,最先赶到地,机械化部队,它们除了智能不高外,可以说是无懈可击.这正是你要利用的一点,利用他们的行动轨迹,躲藏起来. 问题分析:首先用辗转相除法求得gcd(n,m),若n>m 则gcd(n,m)为一可逃反之,非一可逃. 1 #include "c

暑假集训(1)第八弹 -----Catch the cow(Poj3984)

Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. Input 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. Output 左上角到右下角的最短路径,格式如样例所示. Sa

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

暑假集训(1)第六弹 -----简单计算器(Hdoj1237)

Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔.没有非法表达式.当一行中只有0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位. Sample Input 1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output 3.00 13.36 问题分析:难

暑假集训(1)第五弹 -----Rails(Uva514)

PopPush城市有一座著名的火车站.这个国家到处都是丘陵.而这个火车站是建于上一个世纪.不幸的是,那时的资金有限.所以只能建立起一条路面铁轨.而且,这导致这个火车站在同一个时刻只能一个轨道投入使用,因为它缺少空间,两列火车将无路可走.具体看下图.   当地的惯例是每一列火车从A方向驶向B方向时候,会用某种方式将车厢重组.假设火车将要到达A方向,拥有N个车厢(N<=1000),这些车厢按照递增顺序标记为1到N.负责从组车厢的领导,必须知道是否能从组车厢让它驶出B,而这个重组的序列就是a1\a2\

暑假集训(1)第四弹 -----Find a way(Hdu2612)

Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the