File size: 5,924 Bytes
5fed0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
#include <bits/stdc++.h>
using namespace std;

static const string CH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

char rndCh() {
    return CH[rng() % CH.size()];
}

struct Preset {
    int r, c;
    vector<string> mat;
};

int main() {
    const int T = 10;
    const int n = 20, m = 20, k = 20;

    for (int tc = 1; tc <= T; ++tc) {
        // 1. Random target
        vector<string> target(n, string(m, 'A'));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                target[i][j] = rndCh();

        // 2. Random presets
        vector<Preset> pres(k + 1);
        for (int p = 1; p <= k; ++p) {
            int pr = uniform_int_distribution<int>(1, n)(rng);
            int pc = uniform_int_distribution<int>(1, m)(rng);
            pres[p].r = pr;
            pres[p].c = pc;
            pres[p].mat.assign(pr, string(pc, 'A'));
            for (int i = 0; i < pr; ++i)
                for (int j = 0; j < pc; ++j)
                    pres[p].mat[i][j] = rndCh();
        }

        // 3. Generate random operation sequence (forward), apply in reverse
        //    Constraints: total ops <= 4e5, preset <= 400
        //    For randomness, length can be random between 2e5~4e5
        int maxOps = 400000;
        int wantOps = uniform_int_distribution<int>(200000, 400000)(rng);

        struct Op { int op, x, y; };
        vector<Op> ops;
        ops.reserve(wantOps);

        int presetUsed = 0;

        // Start from target and work backwards
        vector<string> cur = target;

        auto doBackwardRotate = [&](int x, int y) {
            // Reverse: undo clockwise rotation, equivalent to counter-clockwise
            // indices: (x,y), (x,y+1), (x+1,y+1), (x+1,y)
            // Original clockwise: new = {d, a, b, c}
            // Reverse: old = {b, c, d, a}
            char ny  = cur[x][y+1];
            char nyy = cur[x+1][y+1];
            char nyyy= cur[x+1][y];
            char nyyyy= cur[x][y];
            // Restore old
            cur[x][y]     = ny;
            cur[x][y+1]   = nyy;
            cur[x+1][y+1] = nyyy;
            cur[x+1][y]   = nyyyy;
        };

        for (int i = 0; i < wantOps; ++i) {
            // Choose a random operation for variety
            // 0:-4,1:-3,2:-2,3:-1,4:rotate(0),5:preset
            int typ = uniform_int_distribution<int>(0, 5)(rng);
            int op, x, y;

            if (typ == 5 && presetUsed >= 400) {
                typ = uniform_int_distribution<int>(0, 4)(rng);
            }

            if (typ == 0) { // -4: swap with up
                if (n <= 1) { --i; continue; }
                x = uniform_int_distribution<int>(2, n)(rng);
                y = uniform_int_distribution<int>(1, m)(rng);
                op = -4;
                // backward: same swap
                swap(cur[x-1][y-1], cur[x-2][y-1]);
            } else if (typ == 1) { // -3: swap with down
                if (n <= 1) { --i; continue; }
                x = uniform_int_distribution<int>(1, n-1)(rng);
                y = uniform_int_distribution<int>(1, m)(rng);
                op = -3;
                swap(cur[x-1][y-1], cur[x][y-1]);
            } else if (typ == 2) { // -2: swap with left
                if (m <= 1) { --i; continue; }
                x = uniform_int_distribution<int>(1, n)(rng);
                y = uniform_int_distribution<int>(2, m)(rng);
                op = -2;
                swap(cur[x-1][y-1], cur[x-1][y-2]);
            } else if (typ == 3) { // -1: swap with right
                if (m <= 1) { --i; continue; }
                x = uniform_int_distribution<int>(1, n)(rng);
                y = uniform_int_distribution<int>(1, m-1)(rng);
                op = -1;
                swap(cur[x-1][y-1], cur[x-1][y]);
            } else if (typ == 4) { // rotate
                if (n <= 1 || m <= 1) { --i; continue; }
                x = uniform_int_distribution<int>(1, n-1)(rng);
                y = uniform_int_distribution<int>(1, m-1)(rng);
                op = 0;
                // backward rotate
                doBackwardRotate(x-1, y-1);
            } else {
                // preset
                int pid = uniform_int_distribution<int>(1, k)(rng);
                int pr = pres[pid].r;
                int pc = pres[pid].c;
                x = uniform_int_distribution<int>(1, n - pr + 1)(rng);
                y = uniform_int_distribution<int>(1, m - pc + 1)(rng);
                op = pid;
                presetUsed++;

                // backward: fill this region with random chars
                for (int ii = 0; ii < pr; ++ii)
                    for (int jj = 0; jj < pc; ++jj)
                        cur[x - 1 + ii][y - 1 + jj] = rndCh();
            }

            ops.push_back({op, x, y});
        }

        // Now cur is the initial state
        vector<string> initial = cur;

        // 4. Write to file
        {
            string fname = to_string(tc) + ".in";
            ofstream fout(fname);
            fout << n << " " << m << " " << k << "\n";
            for (int i = 0; i < n; ++i) fout << initial[i] << "\n";
            fout << "\n";
            for (int i = 0; i < n; ++i) fout << target[i] << "\n";
            for (int p = 1; p <= k; ++p) {
                fout << "\n";
                fout << pres[p].r << " " << pres[p].c << "\n";
                for (int i = 0; i < pres[p].r; ++i)
                    fout << pres[p].mat[i] << "\n";
            }
            fout.close();
        }

        // 5. Output empty ans file
        {
            string fname = to_string(tc) + ".ans";
            ofstream fout(fname);
            // empty
            fout.close();
        }

        cerr << "generated " << tc << ".in/.out, ops=" << ops.size()
             << ", presetsUsed=" << presetUsed << "\n";
    }

    return 0;
}