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

POJ 3051 Satellite Photographs

时间:2015-02-18 11:56:19      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:dfs   poj 3051 satellite   

Satellite Photographs
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wishes to determine the largest ‘contiguous‘ (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by traversing adjacent vertical or horizontal pixels that are part of the pasture. (It is easy to create pastures with very strange shapes, even circles that surround other circles.) 

Each photo has been digitally enhanced to show pasture area as an asterisk (‘*‘) and non-pasture area as a period (‘.‘). Here is a 10 x 5 sample satellite photo: 

..*.....** 
.**..***** 
.*...*.... 
..****.*** 
..****.***


This photo shows three contiguous pastures of 4, 16, and 6 pixels. Help FJ find the largest contiguous pasture in each of his satellite photos.

Input

* Line 1: Two space-separated integers: W and H 

* Lines 2..H+1: Each line contains W "*" or "." characters representing one raster line of a satellite photograph.

Output

* Line 1: The size of the largest contiguous field in the satellite photo.

Sample Input

10 5
..*.....**
.**..*****
.*...*....
..****.***
..****.***

Sample Output

16

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1005
#define inf 0x3f3f3f3f
int w,h;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char mp[maxn][maxn];
int sum;
bool judge(int x,int y){
    if(x>=0&&y>=0&&x<h&&y<w&&mp[x][y]=='*')return true;
    return false;
}
void dfs(int x,int y){
   mp[x][y]='.';
   sum++;
   for(int i=0;i<4;i++){
    int nx=x+dir[i][0];
    int ny=y+dir[i][1];
    if(judge(nx,ny)){
        dfs(nx,ny);
    }
   }
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&w,&h)!=EOF){
        for(int i=0;i<h;i++)
            scanf("%s",mp[i]);
            int mas=0;
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                if(mp[i][j]=='*'){
                        sum=0;
                        dfs(i,j);
                        mas=max(mas,sum);
                }
            }
            printf("%d\n",mas);
    }
}


POJ 3051 Satellite Photographs

标签:dfs   poj 3051 satellite   

原文地址:http://blog.csdn.net/u013497977/article/details/43876141

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