首页 > 代码库 > Cross - -

Cross - -

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

#define SIZE 500
static char map[10][SIZE][SIZE]={0};

int run_test(const char map[SIZE][SIZE]);


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\n",run_test(map[count]));
	return;
}

int run_test(const char map[SIZE][SIZE])

{
	int data[SIZE][SIZE];
	int max=0;
	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 rl=false;
				bool ud=false;
				if( j+1<SIZE && data[i][j+1]){
					rl=true;
				}
				if( j-1>=0 && data[i][j-1]){
					rl=true;
				}
				if(i+1<SIZE && data[i+1][j]){
					ud=true;
				}
				if(i-1>=0 && data[i-1][j]){
					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;
		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;
}

four lines intersect in a map of size 6*6.

the length of lines excluding intersections,You can see that the length of the longest line is 2.

the thickness of lines is always 1,and lines cross/intersect each other at right angles.

Cross - -