#include <bits/stdc++.h> using namespace std; using ll = long long; class Action { public: virtual ~Action() {} virtual void run() = 0; virtual bool isMove() = 0; }; class Move : public Action { int x, y; public: Move(int _x, int _y) : x(_x), y(_y) {} void run() { if (x == 1 && y == 0) printf("A"); else if (x == 1 && y == -1) printf("B"); else if (x == 0 && y == -1) printf("C"); else if (x == -1 && y == 0) printf("D"); else if (x == -1 && y == 1) printf("E"); else if (x == 0 && y == 1) printf("F"); else abort(); } bool isMove() { return true; } }; class Sequence : public Action { vector<Action*> actions; public: Sequence() : actions() {} Sequence(const vector<Action*> &_actions) : actions(_actions) {} ~Sequence() { for (auto action : actions) { if (!action->isMove()) delete action; } } void run() { for (auto action : actions) action->run(); } bool isMove() { return false; } void append(Action *action) { actions.push_back(action); } }; class Repeat : public Action { Action *action; ll n; public: Repeat(Action *_action, ll _n) : action(_action), n(_n) {} ~Repeat() { if (!action->isMove()) delete action; } void run() { ll m = n; int i = 0; while (m > 0) { if (m % 9 == 1) { action->run(); } else if (m % 9 > 0) { printf("%lld", m % 9); if (!action->isMove()) printf("["); action->run(); if (!action->isMove()) printf("]"); } m /= 9; if (m > 0) { i++; printf("9["); } } for (int j = 0; j < i; j++) printf("]"); } bool isMove() { return false; } }; Move *L, *R, *U, *D, *DR, *UL; // starts and ends in top corner void makeTriangle(ll size, Sequence *out); // without left and bottom border, starts in top corner, ends in right corner void makeBorderlessTriangle(ll size, Sequence *out); // starts in bottom-right corner, ends in top-left corner void makeSquare(ll size, Sequence *out); void makeTriangle(ll size, Sequence *out) { if (size == 1) { out->append(DR); out->append(L); out->append(U); } else { Sequence *half = new Sequence(); makeBorderlessTriangle(size / 2, half); out->append(new Repeat(half, 2)); if (size % 2 == 1) { out->append(DR); out->append(new Repeat(L, size)); out->append(U); out->append(new Repeat(new Sequence({ DR, U }), size - 1)); } out->append(new Repeat(L, size / 2)); makeSquare(size / 2, out); out->append(new Repeat(U, size / 2)); } } void makeBorderlessTriangle(ll size, Sequence *out) { if (size == 1) { out->append(DR); } else if (size == 2) { out->append(DR); out->append(L); out->append(DR); out->append(U); out->append(DR); } else { out->append(DR); makeTriangle(size - 2, out); out->append(new Repeat(new Sequence({ L, DR }), size - 1)); out->append(new Repeat(new Sequence({ U, DR }), size - 1)); } } void makeSquare(ll size, Sequence *out) { Sequence *row = new Sequence(); row->append(new Repeat(L, size)); row->append(U); row->append(new Repeat(new Sequence({ DR, U }), size)); out->append(new Repeat(row, size)); out->append(new Repeat(L, size)); } int main() { L = new Move(-1, 0); R = new Move(1, 0); D = new Move(0, -1); U = new Move(0, 1); DR = new Move(1, -1); UL = new Move(-1, 1); ll n; scanf("%lld", &n); Sequence *sequence = new Sequence(); makeTriangle(n, sequence); sequence->run(); //printf("%lld\n", sequence->size()); puts(""); delete sequence; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #include <bits/stdc++.h> using namespace std; using ll = long long; class Action { public: virtual ~Action() {} virtual void run() = 0; virtual bool isMove() = 0; }; class Move : public Action { int x, y; public: Move(int _x, int _y) : x(_x), y(_y) {} void run() { if (x == 1 && y == 0) printf("A"); else if (x == 1 && y == -1) printf("B"); else if (x == 0 && y == -1) printf("C"); else if (x == -1 && y == 0) printf("D"); else if (x == -1 && y == 1) printf("E"); else if (x == 0 && y == 1) printf("F"); else abort(); } bool isMove() { return true; } }; class Sequence : public Action { vector<Action*> actions; public: Sequence() : actions() {} Sequence(const vector<Action*> &_actions) : actions(_actions) {} ~Sequence() { for (auto action : actions) { if (!action->isMove()) delete action; } } void run() { for (auto action : actions) action->run(); } bool isMove() { return false; } void append(Action *action) { actions.push_back(action); } }; class Repeat : public Action { Action *action; ll n; public: Repeat(Action *_action, ll _n) : action(_action), n(_n) {} ~Repeat() { if (!action->isMove()) delete action; } void run() { ll m = n; int i = 0; while (m > 0) { if (m % 9 == 1) { action->run(); } else if (m % 9 > 0) { printf("%lld", m % 9); if (!action->isMove()) printf("["); action->run(); if (!action->isMove()) printf("]"); } m /= 9; if (m > 0) { i++; printf("9["); } } for (int j = 0; j < i; j++) printf("]"); } bool isMove() { return false; } }; Move *L, *R, *U, *D, *DR, *UL; // starts and ends in top corner void makeTriangle(ll size, Sequence *out); // without left and bottom border, starts in top corner, ends in right corner void makeBorderlessTriangle(ll size, Sequence *out); // starts in bottom-right corner, ends in top-left corner void makeSquare(ll size, Sequence *out); void makeTriangle(ll size, Sequence *out) { if (size == 1) { out->append(DR); out->append(L); out->append(U); } else { Sequence *half = new Sequence(); makeBorderlessTriangle(size / 2, half); out->append(new Repeat(half, 2)); if (size % 2 == 1) { out->append(DR); out->append(new Repeat(L, size)); out->append(U); out->append(new Repeat(new Sequence({ DR, U }), size - 1)); } out->append(new Repeat(L, size / 2)); makeSquare(size / 2, out); out->append(new Repeat(U, size / 2)); } } void makeBorderlessTriangle(ll size, Sequence *out) { if (size == 1) { out->append(DR); } else if (size == 2) { out->append(DR); out->append(L); out->append(DR); out->append(U); out->append(DR); } else { out->append(DR); makeTriangle(size - 2, out); out->append(new Repeat(new Sequence({ L, DR }), size - 1)); out->append(new Repeat(new Sequence({ U, DR }), size - 1)); } } void makeSquare(ll size, Sequence *out) { Sequence *row = new Sequence(); row->append(new Repeat(L, size)); row->append(U); row->append(new Repeat(new Sequence({ DR, U }), size)); out->append(new Repeat(row, size)); out->append(new Repeat(L, size)); } int main() { L = new Move(-1, 0); R = new Move(1, 0); D = new Move(0, -1); U = new Move(0, 1); DR = new Move(1, -1); UL = new Move(-1, 1); ll n; scanf("%lld", &n); Sequence *sequence = new Sequence(); makeTriangle(n, sequence); sequence->run(); //printf("%lld\n", sequence->size()); puts(""); delete sequence; } |