P1911 L国的战斗之排兵布阵

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值