#include<iostream>
#include<fstream>
#include<queue>
#include<cstdlib>
#include<windows.h>
using namespace std;
#define _DEBUG
#ifdef _DEBUG
ifstream fin("C:\\data5.in");
ofstream fout("C:\\data5.out");
#endif
struct position{
position(int posx=0,int posy=0):x(posx),y(posy){}
position& operator=(const position& ps){x=ps.x;y=ps.y;return *this;}
int x;
int y;
};
const int MAXNUM=32767;
int chessboard[100][100];
int boarderLength;
int cnt=0;
int order=0;
queue<position> CenterNode;
int transMatrix[4][2]={{-1,-1},{-1,1},{1,-1},{1,1}};
void print();
int power(int n)
{
int ans=1;
for(int i=0;i<n;++i)
ans*=2;
return ans;
}
int exponent(int x)
{
x=x*3+1;
int exp=0;
while(x!=0)
{
++exp;
x/=4;
}
return exp-1;
}
bool hasBlackDot(int xLeftTop,int yLeftTop,int xRightBottom,int yRightBottom)
{
bool hasBlack=false;
for(int x=xLeftTop+1;x<=xRightBottom;++x)
{
for(int y=yLeftTop+1;y<=yRightBottom;++y)
{
if(chessboard[x][y]!=0)
{
hasBlack=true;
break;
}
}
}
return hasBlack;
}
void TurnToBlack(position pos,int length)
{
++cnt;
if(!hasBlackDot(pos.x-length,pos.y-length,pos.x,pos.y))
chessboard[pos.x][pos.y]=cnt;
if(!hasBlackDot(pos.x,pos.y-length,pos.x+length,pos.y))
chessboard[pos.x+1][pos.y]=cnt;
if(!hasBlackDot(pos.x-length,pos.y,pos.x,pos.y+length))
chessboard[pos.x][pos.y+1]=cnt;
if(!hasBlackDot(pos.x,pos.y,pos.x+length,pos.y+length))
chessboard[pos.x+1][pos.y+1]=cnt;
print();
Sleep(1000);
}
void PushIntoQueue(int x,int y)
{
position pos(x,y);
CenterNode.push(pos);
}
void BFS()
{
while(!CenterNode.empty())
{
position pos=CenterNode.front();
//下面一行可以化简
int length=boarderLength/(power(exponent(cnt-1))+1);
if(length==0)
break;
//涂黑
TurnToBlack(pos,length);
length/=2;
for(int i=0;i<4;++i)
PushIntoQueue(pos.x+length*transMatrix[i][0],pos.y+length*transMatrix[i][1]);
CenterNode.pop();
}
}
void Init()
{
int k;
fin>>k;
boarderLength=power(k);
for(int i=1;i<=boarderLength;++i)
for(int j=1;j<=boarderLength;++j)
chessboard[i][j]=0;
int blackposx,blackposy;
fin>>blackposx>>blackposy;
chessboard[blackposx][blackposy]=++cnt;
PushIntoQueue(boarderLength/2,boarderLength/2);
print();
Sleep(1000);
BFS();
}
void print()
{
cout<<"第"<<++order<<"次:"<<endl;
for(int i=1;i<=boarderLength;++i)
{
for(int j=1;j<=boarderLength;++j)
{
if(chessboard[i][j]!=0)
cout<<chessboard[i][j]<<"\t";
else
cout<<"□\t";
}
cout<<endl;
}
cout<<endl;
}
int main()
{
Init();
system("pause");
return 0;
}