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[][];
  10. void obegin(int n,int m)
  11. {
  12. int i,j;
  13. for (i=;i<=n+;i++)
  14. for (j=;j<=m+;j++)
  15. {
  16. if (i*j == || i == n+ || j == m+)
  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=;
  27. int f;
  28. for (int i=;i<=n;i++)
  29. for (int j=;j<=m;j++)
  30. if (o[i][j] == '@')
  31. {
  32. f=;
  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=;k<=;k++)
  42. {
  43. switch(k)
  44. {
  45. case : if (o[sec.i+][sec.j] == '@')
  46. {
  47. fir.i=sec.i+;
  48. fir.j=sec.j;
  49. }break;
  50. case :if (o[sec.i-][sec.j] == '@')
  51. {
  52. fir.i=sec.i-;
  53. fir.j=sec.j;
  54. }break;
  55. case :if (o[sec.i][sec.j+] == '@')
  56. {
  57. fir.i=sec.i;
  58. fir.j=sec.j+;
  59. }break;
  60. case :if (o[sec.i][sec.j-] == '@')
  61. {
  62. fir.i=sec.i;
  63. fir.j=sec.j-;
  64. }break;
  65. case :if (o[sec.i+][sec.j+] == '@')
  66. {
  67. fir.i=sec.i+;
  68. fir.j=sec.j+;
  69. }break;
  70. case :if (o[sec.i+][sec.j-] == '@')
  71. {
  72. fir.i=sec.i+;
  73. fir.j=sec.j-;
  74. }break;
  75. case :if (o[sec.i-][sec.j-] == '@')
  76. {
  77. fir.i=sec.i-;
  78. fir.j=sec.j-;
  79. }break;
  80. case :if (o[sec.i-][sec.j+] == '@')
  81. {
  82. fir.i=sec.i-;
  83. fir.j=sec.j+;
  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/ == )
  94. {
  95. f=;
  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 ;
  112. }
 
 
 

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

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

    J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

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

    题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++ ...

  3. CSU-ACM2016暑期集训训练4-BFS(F - Oil Deposits)

    F - Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

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

    题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A ...

  5. 暑假集训(3)第四弹 -----Frogger(Poj2253)

    题意梗概:青蛙王子最近喜欢上了另一只经常坐在荷叶上的青蛙公主.不过这件事不小心走漏了风声,被某fff团团员知 道了,在青蛙王子准备倾述心意的那一天,fff团团员向湖泊中注入大量的充满诅咒力量的溶液.这 ...

  6. 暑假集训(4)第八弹——— 组合(hdu1524)

    题意概括:你已经赢得两局,最后一局是N个棋子往后移动,最后一个无法移动的玩家失败. 题目分析:有向无环图sg值游戏,尼姆游戏的抽象表达.得到每个棋子的sg值之后,把他们异或起来,考察异或值是否为0. ...

  7. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  8. 暑假集训(4)第四弹 -----排列,计数(hdu1465)

    题意概括:嗯,纵使你数次帮助小A脱离困境,但上一次,小A终于还是失败了.那数年的奔波与心血,抵不过轻轻一指,便彻底 湮灭,多年的友谊终归走向末路.这一切重击把小A彻底击溃! 不为什么,你到底还是要继续 ...

  9. 暑假集训(4)第三弹 -----递推(Hdu1799)

    问题描述:还记得正在努力脱团的小A吗? 他曾经最亲密的战友,趁他绘制贤者法阵期间,暗中设下鬼打墙将小A 围困,并准备破坏小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. ...

随机推荐

  1. Esper系列(二)时间窗口、长度窗口、cast、注解、自定义函数、静态方法

    长度窗口实现原理图 说明: 上图长度窗口为5,事件W1至W5进入引擎后属于NewEvents队列,事件W6进入引擎后,W2至W6就属于NewEvents队列,而事件W1就属于OldEvents队列了. ...

  2. SQL Server Data Tool 嘹解(了解)一下 SSDT -摘自网络

    Visual Studio 2010 的伺服器管理員可以用來連接 Sharepoint.伺服器,還可以透過資料連接連結至 SQL Server  等資料來源.以資料來源為例,您可以利用單一工具(Vis ...

  3. stream转byte数组几种方式

    第一种,写法最简单的.使用原生IO,一个字节一个字节读: //一个字符一个字符读,太慢 int i; while((i=in.read()) != -1){ i = in.read(); arr[j+ ...

  4. 12306火车票订票网站的一个Bug

    国庆节后回上海,大家都知道的火车票灰常火爆,卧铺票一票难求.登录http://www.12306.cn 铁路公司售票网站发现北京—>上海的卧铺已经售完,考虑到北京—>蚌埠这段还是在白天及晚 ...

  5. MINA之心跳协议运用

    转自:http://my.oschina.net/yjwxh/blog/174633 摘要 心跳协议,对基于CS模式的系统开发来说是一种比较常见与有效的连接检测方式,最近在用MINA框架,原本自己写了 ...

  6. 使用innobackupex备份mysql数据库

    1  因为使用perl脚本编写,安装前应先安装 yum install perl-Time-HiRes -y yum -y install perl-DBD-MySQL.x86_64 一起安装     ...

  7. SmartForms 小技巧

    1.添加空行,保证每一页有固定的打印的表格行数 上图,每页最多打印13行,数据只有11行,自动添加两个空行补齐 代码如下“ "定义变量: data: l_blank type i. &quo ...

  8. An existing PostgreSql installation has been found... 的解决

    PostgreSql卸载之后,重新安装时跳出如下信息: Anexisting PostgreSql installation has been found atC:\ProgramFiles\Post ...

  9. 开发XMPP IM

    Openfire 是一个用Java 实现的XMPP 服务器,客户端可以通过IQ的方式与其进行通信(其实就是XML),客户端和服务器之间的通信是依靠底层Smack 库提供的各种功能来完成的.其实利用插件 ...

  10. TCP并发server,每个客户一个子进程

    今天笔者带来的是server型号第一,这是最经常使用的模型的最基本的一个–TCP并发server,每个客户一个子进程. 首先简单介绍:TCP并发server,每个客户一个子进程,并发server调用f ...