注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

UVA 572 Oil Deposits  

2012-07-05 05:09:35|  分类: uva |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |



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 \le m \le 100$ and $1 \le n \le 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 
赤果果的一道DFS求区域块的题目= =就不多说什么了,直接代码:
#include<iostream> 
#include<stdio.h>  
using namespace std; 
int n,m; char map[300][300]; 
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};   
void DFS(int x,int y) 
{     
 int i,xx,yy;     
 map[x][y]='*';     
 for(i=0;i<8;i++)     
 {         
  xx=x+dir[i][0];         
  yy=y+dir[i][1];         
  if(xx >= n || yy >= m || xx < 0 || yy < 0)   continue;            if(map[xx][yy] == '@')         DFS(xx,yy);     
 } 
} 

int main() 
{     
 while(~scanf("%d%d",&n,&m))     
 {     
  int i,j;      
  int count = 0;      
  if(n == 0 && m == 0) break;          
   cin.get();                  
  for(i = 0;i < n;i ++)         
  {            
   for(j = 0;j < m;j ++)             
   scanf("%c",&map[i][j]);             
   cin.get();         
  }                  
  for(i = 0;i <= n;i++)         
  {             
   for(j = 0;j <= m;j++)             
   {                 
    if(map[i][j] == '@')                 
    {                    
      DFS(i,j);//从(i,j)位置进行DFS;                          ++ count;                
     }             
   }         
  }         
  cout<<count<<endl;     
  }    
   return 0; 
 } 


  评论这张
 
阅读(127)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018