求解啊 大神 想了好久啊

lxtlst521 2013-04-16 10:24:58
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
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-04-18
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xs[10000];
int ys[10000];
int i=0,xx,yy;
int fc,bc;
void push(int x,int y) {
    xs[i]=x;
    ys[i]=y;
    if (i<10000-1) {
        i=i+1;
    } else {
        printf("stack overflow!\n");
        exit(1);
    }
}
void pop(void) {
    i=i-1;
    xx=xs[i];
    yy=ys[i];
}
int check(int x,int y) {
    int c;

    c=getpixel(x,y); /* 获取当前点的颜色 */
    return ((c!=bc)&&(c!=fc));/* 如果颜色为边界色或填充色则不填充 */
}
void seedfilling(int x,int y,int fill_color,int boundary_color) {
    fc=fill_color;
    bc=boundary_color;
    push(x,y);
    while (1) {
        if (i<=0) return;
        pop();
        if (check(xx,yy)) {
            putpixel(xx, yy, 14);getch();  /* 加上这行显示当前填充状态 */

            putpixel(xx, yy, fill_color); /* 画点 */

            if (check(xx-1,yy  )) push(xx-1,yy  );
            if (check(xx  ,yy+1)) push(xx  ,yy+1);
            if (check(xx  ,yy-1)) push(xx  ,yy-1);
            if (check(xx+1,yy  )) push(xx+1,yy  );
            /* 去掉下面四句就是四连通 */
            if (check(xx-1,yy-1)) push(xx-1,yy-1);
            if (check(xx-1,yy+1)) push(xx-1,yy+1);
            if (check(xx+1,yy-1)) push(xx+1,yy-1);
            if (check(xx+1,yy+1)) push(xx+1,yy+1);
        }
    }
}
void main() {
    int a,b,color;
    int gdriver = DETECT, gmode, errorcode;
    int poly[10];

    initgraph(&gdriver, &gmode, "d:\\bc\\bgi");
    a=150;
    b=140;
    color=4;
    poly[0]=110;/* 第一个点的x坐标以及y坐标 */
    poly[1]=110;
    poly[2]=200;/* 第二点 */
    poly[3]=105;
    poly[4]=170;/* 第三点 */
    poly[5]=120;
    poly[6]=150;/* 第四点 */
    poly[7]=170;
    poly[8]=110;/* 多边形的起点与终点一样 */
    poly[9]=110;
    drawpoly(5,poly);/* 显示各点连接起来的多边形 */

    /* 保证边界对八连通是封闭的 */
    setviewport(0,1,600,300,0);
    drawpoly(5,poly);/* 显示各点连接起来的多边形 */
    setviewport(1,0,600,300,0);
    drawpoly(5,poly);/* 显示各点连接起来的多边形 */
    setviewport(1,1,600,300,0);
    drawpoly(5,poly);/* 显示各点连接起来的多边形 */

    /* 恢复默认viewport */
    setviewport(0,0,600,300,0);

    seedfilling(a,b,color,15);  /* 种子填充多边形 */

    getch();
    closegraph();
}
hello_world_ww 2013-04-17
  • 打赏
  • 举报
回复
把这个矩阵看做图(上下左右相邻的'*'为连接的点,忽略'.'),显然就是求该图的最大连通分量 一遍扫描,用并查集叫可以出来了吧
ZHONGCONGWUJIANG 2013-04-17
  • 打赏
  • 举报
回复
忘了,第4步中队首元素出队后要判断该位置是否为‘*’,是就进行下边的处理,否则就跳过该步
ZHONGCONGWUJIANG 2013-04-17
  • 打赏
  • 举报
回复
1、m=0 2、计数器n置0,初始化队列(栈也可以) 3、从第一行开始遍历,将遇到的第一个*坐标入队(栈的话则压栈),如果不存在*跳转至第7步 4、将队首元素出队(弹出栈顶元素),n自增,将该坐标位置置为‘.’,再将与该元素相邻的*坐标以某种顺序入队(压栈) 5、若队(栈)空,比较m和n,若m<n,则令m=n 6、跳转置第2步 7、输出m

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧