若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1082: Satellite Photographs

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,442

题目描述

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.

输入

* 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.

输出

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

样例输入

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

样例输出

16

参考代码

#include <stdio.h>
int n,m;
char map[51][51];
int dfs(int x,int y) 
{
    if(x<0||y<0||x>=m||y>=n)
            return 0; else if(map[x][y]=='.')
            return 0; else 
    {
        map[x][y]='.';
        return 1+dfs(x,y-1)+dfs(x-1,y)+dfs(x+1,y)+dfs(x,y+1);
    }
}
int main() 
{
    int i,j,h=0,max,a[10]={0};
    while(scanf("%d%d",&n,&m)!=EOF&&m) 
    {
        for (i=0;i<m;i++)
                scanf("%s",map[i]);
        for (i=0;i<m;i++) 
        {
            for (j=0;j<n;j++) 
            {
                if(map[i][j]=='*') 
                {
                    a[h]=dfs(i,j);
                    h++;
                }
            }
        }
        max=a[0];
        for (i=0;i<h;i++)
                max=max>a[i]?max:a[i];
        printf("%d",max);
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 Skip 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist