https://www.luogu.org/problem/P1911
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
struct SPoint{
int x;
int y;
int v;
SPoint():x(0),y(0),v(0)
{
}
void set(int x1, int y1)
{
x=x1;
y=y1;
}
//friend bool operator < (const SPoint& p1, const SPoint& p2)
//{
// return (p1.x < p2.x || p1.x == p2.x && p1.y < p2.y);
//}
bool operator < (const SPoint& p2) const
{
return (x < p2.x || x == p2.x && y < p2.y);
}
//{
// if (x < p1.x
// || x == p1.x && y < p1.y)
// {
// return true;
// }
// else
// {
// return false;
// }
//}
};
SPoint pLeader;
struct SLForm
{
SPoint a;
SPoint b;
SPoint c;
void setValue(int v)
{
a.v = b.v = c.v = v;
}
bool operator < (const SLForm& p2) const
{
return this->a < p2.a;
}
};
vector<SLForm> lList;
//计算2的n次方
int pow2(int n)
{
if (0 == n)
{
return 1;
}
//else if (1 == n)
//{
// return 2;
//}
else
{
return 2 << (n-1);
}
}
//将L放入队列
void inputL(const SLForm& ll)
{
lList.push_back(ll);
}
//绘制L型,2*2的方块,左上角坐标x0,y0,特殊点x,y
//注意:a b c排序依据:x升序,x相同时y升序
void getL(const int x0, const int y0, const int x, const int y)
{
SLForm ll;
if (x0 == x && y0 == y)
{
ll.a.set(x0, y0+1);
ll.b.set(x0+1, y0);
ll.c.set(x0+1, y0+1);
}
else if (x0 == x && y0 != y)
{
ll.a.set(x0, y0);
ll.b.set(x0+1, y0);
ll.c.set(x0+1, y0+1);
}
else if (x0 != x && y0 == y)
{
ll.a.set(x0, y0);
ll.b.set(x0, y0+1);
ll.c.set(x0+1, y0+1);
}
else
{
ll.a.set(x0, y0);
ll.b.set(x0, y0+1);
ll.c.set(x0+1, y0);
}
inputL(ll);
}
//处理矩形,矩形边长2^n,左上角坐标x0,y0,特殊点x,y
void procRectangle(int n, int x0, int y0, int x, int y)
{
//cout << __FUNCTION__ << ":n="<< n << ",("<< x0 << ","<<y0<<"),("<<x<<","<<y<<")"<<endl;
if (n == 1)
{
getL(x0, y0, x, y);
}
else
{
//while (n > 1)
{
int half = pow2(n-1);
int xHalf = x0 + half;
int yHalf = y0 + half;
int nn = n-1;
//左上角
int xx0 = x0;
int yy0 = y0;
int xx = xHalf-1;
int yy = yHalf-1;
if (x<xHalf && y<yHalf)
{
procRectangle(nn, xx0, yy0, x, y);
getL(xHalf-1, yHalf-1, xx, yy);
}
else
{
procRectangle(n-1, xx0, yy0, xx, yy);
}
//右上角
xx0 = x0;
yy0 = yHalf;
xx = xHalf-1;
yy = yHalf;
if (x<xHalf && y>=yHalf)
{
procRectangle(nn, xx0, yy0, x, y);
getL(xHalf-1, yHalf-1, xx, yy);
}
else
{
procRectangle(n-1, xx0, yy0, xx, yy);
}
//左下角
xx0 = xHalf;
yy0 = y0;
xx = xHalf;
yy = yHalf-1;
if (x>=xHalf && y<yHalf)
{
procRectangle(nn, xx0, yy0, x, y);
getL(xHalf-1, yHalf-1, xx, yy);
}
else
{
procRectangle(n-1, xx0, yy0, xx, yy);
}
//右下角
xx0 = xHalf;
yy0 = yHalf;
xx = xHalf;
yy = yHalf;
if (x>=xHalf && y>=yHalf)
{
procRectangle(nn, xx0, yy0, x, y);
getL(xHalf-1, yHalf-1, xx, yy);
}
else
{
procRectangle(n-1, xx0, yy0, xx, yy);
}
//n--;
}
}
}
// 打印所有点位
void printAll(int n)
{
set<SPoint> pList;
pList.insert(pLeader);
sort(lList.begin(), lList.end());
{
vector<SLForm>::iterator iter = lList.begin();
int v = 1;
while (iter != lList.end())
{
iter->setValue(v);
pList.insert(iter->a);
pList.insert(iter->b);
pList.insert(iter->c);
iter++;
v++;
}
}
{
const int len = pow2(n);
int i = 1;
set<SPoint>::iterator iter = pList.begin();
while (iter != pList.end())
{
if (1 == i)
{
cout << iter->v;
}
else
{
cout << " " << iter->v;
}
if (len == i)
{
cout << endl;
i = 0;
}
++i;
++iter;
}
}
}
int main()
{
int n(2),x(2),y(1);
cin>>n>>x>>y;
pLeader.set(x,y);
procRectangle(n, 1, 1, x, y);
int len = lList.size();
printAll(n);
return 0;
}