码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1204 深搜

时间:2015-01-29 12:36:55      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

  1. #include <iostream>
  2. #include <string>
  3. #define MAX 110
  4. #define OIL true
  5. #define BLANK false
  6. using namespace std;
  7. bool oil_map[MAX][MAX];
  8. int dir_map[8][2]={ {1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1} };
  9. void set_oil_map()
  10. {
  11. memset(oil_map,BLANK,sizeof(oil_map) );
  12. }
  13. void input_oil_map(int x,int y)
  14. {
  15. for(int i=1 ;i<=x ;i++)
  16. {
  17. string temp;
  18. cin>>temp;
  19. for(int j=1 ;j<=y ;j++)
  20. {
  21. if(temp[j-1]==‘@‘ )
  22. oil_map[i][j]=OIL;
  23. }
  24. }
  25. }
  26. void search_oil_map(int x ,int y)
  27. {
  28. //if(oil_map[x][y]==OIL )
  29. oil_map[x][y]=BLANK;
  30. int search_x,search_y;
  31. for(int i=0 ;i<8 ;i++)
  32. {
  33. search_x=x+dir_map[i][0];
  34. search_y=y+dir_map[i][1];
  35. if(oil_map[search_x][search_y]==OIL )
  36. search_oil_map(search_x ,search_y );
  37. }
  38. }
  39. int main()
  40. {
  41. int n,m;
  42. while(cin>>m>>n &&(m+n)!=0 )
  43. {
  44. set_oil_map();
  45. input_oil_map(m,n);
  46. int count=0;
  47. for(int i=1 ;i<=m ;i++)
  48. for(int j=1 ;j<=n ;j++)
  49. {
  50. if(oil_map[i][j]==OIL )
  51. {
  52. search_oil_map(i,j);
  53. count++;
  54. }
  55. }
  56. cout<<count<<endl;
  57. }
  58. return 0;
  59. }
  60. /*Problem Description
  61. The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.
  62. GeoSurvComp works with one large rectangular region of land at a time,
  63. and creates a grid that divides the land into numerous square plots.
  64. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
  65. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit.
  66. Oil deposits can be quite large and may contain numerous pockets.
  67. Your job is to determine how many different oil deposits are contained in a grid.
  68. Input
  69. The input file contains one or more grids.
  70. Each grid begins with a line containing m and n,
  71. the number of rows and columns in the grid, separated by a single space.
  72. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100.
  73. Following this are m lines of n characters each (not counting the end-of-line characters).
  74. Each character corresponds to one plot, and is either `*‘,
  75. representing the absence of oil, or `@‘, representing an oil pocket.
  76. Output
  77. For each grid, output the number of distinct oil deposits.
  78. Two different pockets are part of the same oil deposit if they are adjacent horizontally,
  79. vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
  80. */





附件列表

     

    hdu 1204 深搜

    标签:

    原文地址:http://www.cnblogs.com/sober-reflection/p/20c27f529a9ad7a43d348fb4a120d0c7.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!