Cross

#include <stdio.h>
#include <stdlib.h>

#define SIZE 1000

static char map[10][SIZE][SIZE];

/*
int data[SIZE][SIZE];
int run_test(const char map[SIZE][SIZE])
{
    for(int i=0;i<SIZE;i++){
        for(int j=0;j<SIZE;j++){
            data[i][j]=map[i][j];
        }
    }
    for(int i=0;i<SIZE;i++){
        for(int j=0;j<SIZE;j++){
            if(data[i][j]==1){
                bool ud=false;
                bool rl=false;
                if(i>0&&data[i-1][j]==1)rl=true;
                if(i+1<SIZE&&data[i+1][j]==1)rl=true;
                if(j>0&&data[i][j-1]==1)ud=true;
                if(j+1<SIZE&&data[i][j+1]==1)ud=true;
                if(rl&&ud==1)data[i][j]=0;
            }
        }
    }
    
    int max=0;
    for(int i=0;i<SIZE;i++){
        for(int j=0;i<SIZE;j++){
            int x=0;
            int y=0;
            if(data[i][j]==1){
                while(data[i+x][j]!=0)x++;
                while(data[i][j+y]!=0)y++;
                if(x>max)max=x;
                if(y>max)max=y;
            }
        }
    }
    return max; // 가장 긴 선분의 길이
}
/*
int run_test(const char map[SIZE][SIZE])
{    
    int l=0;
    int max1=0;
    int max2=0;
    int b=0;
    for(int i=0;i<SIZE;i++)
    {
        for(int j=0;j<SIZE;j++)
        {
            if(map[i][j]==0||map[i-1][j]==1||map[i+1][j]==1)
            {
                l=0;
            }
            else
            {
                l++;
                if(l>max1)max1=l;
            }
        }
    }
    for(int j=0;j<SIZE;j++)
    {
        for(int i=0;i<SIZE;i++)
        {
            if(map[i][j]==0||map[i][j-1]==1||map[i][j+1]==1)
            {
                l=0;
            }
            else
            {
                l++;
                if(l>max2)max2=l;
            }
        }
    }
    if(max1>=max2) b=max1;
    else b=max2;
    return b;
}
*/
int data[SIZE][SIZE];
int run_test(  const char map[SIZE][SIZE])
{
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++) 
    {
        data[i][j]=map[i][j];
    }
    int len=0;
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++) 
    {
        if(data[i][j]==1)
        {
        bool UD=false;
        bool RL=false;
        if(j+1<SIZE&&data[i][j+1]==1) {RL=true;}
        if(data[i][j-1]==1&&j>0) {RL=true;}
        if(data[i+1][j]==1&&j+1<SIZE){UD=true;}
        if(data[i-1][j]==1&&i>0) {UD=true;}
        if(RL&&UD==1) data[i][j]=0;
        }
    }
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
    {
        int x=0;
        int y=0;
        if(data[i][j]==1)
        while(data[i+x][j]!=0) x++;                                                              
        while(data[i][j+y]!=0) y++;
        if(x>len) len=x;
        if(y>len) len=y;


    }

    return len; // 가장 긴 선분의 길이
}


void build_map(void)
{
    for (int c = 0; c < 10; c++)
    {
        for (int y = 0; y < SIZE; y++)
            for(int x = 0; x < SIZE; x++)
                map[c][x][y] = 0;

        for (int x = rand() % 10; x < SIZE; x += 2 + rand() % 8)
            for (int sy = rand() % SIZE, ey = rand() % SIZE; sy < ey; )
                map[c][x][sy++] = 1;

        for (int y = rand() % 10; y < SIZE; y += 2 + rand() % 8)
            for (int sx = rand() % SIZE, ex = rand() % SIZE; sx < ex; )
                map[c][sx++][y] = 1;
    }
}

void main(void)
{
    build_map();

    for (int count = 0; count < 10; count++)
        printf("%d
", run_test(map[count]));
}
原文地址:https://www.cnblogs.com/ZzznOoooo/p/6628063.html