#include "iostream.h"
#include "fstream.h"
#define max 512
int map[max][max] = { 0 };
struct mRect
{
int left;
int top;
int right;
int bottom;
};
bool divbox( int deep , mRect rect );
bool drawRect( mRect rect );
void drawMap( mRect rect );
void main()
{
int deep = 3;
mRect rect;
rect.top = 0;
rect.left = 0;
rect.right = 1;
rect.bottom = 1;
for( int i = 0 ; i < deep ; i++ )
{
rect.right *= 3;
rect.bottom *= 3;
}
rect.right -= 1;
rect.bottom -= 1;
divbox( deep , rect );
drawMap( rect );
}
void drawMap( mRect rect )
{
ofstream fout("divmap.txt");
int i , j;
for( i = rect.top ; i <= rect.bottom ; i++ )
{
for( j = rect.left ; j <= rect.right ; j++ )
{
if( map[i][j] == 0 )
fout<<" ";
else if( map[i][j] == 1 )
fout<<"*";
}
fout<<endl;
}
}
bool drawRect( mRect rect )
{
//if size of rect is not equal to 3, return false
if( rect.right - rect.left != 2 || rect.bottom - rect.top != 2 )
return false;
int top = rect.top;
int left = rect.left;
int bottom = rect.bottom;
int right = rect.right;
map[top][left] = 1;
map[top][right] = 1;
map[bottom][left] = 1;
map[bottom][right] = 1;
map[(top+bottom)/2][(left+right)/2] = 1;
return true;
}
bool divbox( int deep , mRect rect )
{
if( deep == 1 )
{
return drawRect( rect );
}
else
{
bool flag = true;
mRect rects[9];
int i , j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 3 ; j++ )
{
int size = ( rect.right - rect.left + 1 ) / 3;
rects[3*i+j].left = rect.left + j * size;
rects[3*i+j].right = rect.left + ( j+1 ) * size - 1;
rects[3*i+j].top = rect.top + i * size;
rects[3*i+j].bottom = rect.top + ( i+1 ) * size - 1;
}
}
for( i = 0 ; i < 10 ; i += 2 )
{
flag = divbox( deep - 1 , rects[i] );
if( !flag )
return false;
}
return true;
}
}