POJ 3051 Satellite Photographs

来源:互联网 发布:游戏笔记本电脑知乎 编辑:程序博客网 时间:2024/04/29 10:47
Satellite Photographs
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3051

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 0x3f3f3f3fint 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);    }}


0 0
原创粉丝点击