题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
Story
--------
AtCoder's CEO Takahashi prepares for Halloween tomorrow.
At AtCoder's Halloween party, Takahashi will dress up in disguise and receive a piece of candy from 100 employees in turn by saying, "trick or treat!"
He prepares a square box that can contain $10\times 10$ pieces of candy in a grid pattern, and each employee puts a piece of candy in an empty space so that no candies overlap.
There are $3$ types of candies: strawberry, watermelon, and pumpkin flavors.
He knows which flavor of candy each employee will put in by preliminary survey, but he doesn't know where each employee will put it.
Since he is a clean freak, he will move the pieces of candy by tilting the box forward, backward, left, or right, just once for each piece of candy received, and eventually wants to make sure that the same type of candy is clustered together as much as possible.
Please help him by writing a program to determine the direction to tilt.
Problem Statement
--------
There is a box that can contain $10\times 10$ pieces of candy in a grid pattern.
The box is initially empty, and $100$ pieces of candy will be placed in order.
There are $3$ flavors of candy, and we know in advance the flavor $f_t (1\leq f_t\leq 3)$ of the $t$-th candy.
On the other hand, we do not know in advance to which cell each candy will be placed, and it will be chosen uniformly at random among the empty cells.
You cannot change the order in which the pieces of candy are received.
Each time you receive one piece of candy, you must tilt the box forward, backward, left, or right exactly once.
When you tilt the box, each piece of candy moves in that direction simultaneously until it reaches the edge or hits another candy.
For example, tilting the box forward packs the candies toward the front of each column while preserving their order within that column.
Raw task objective
--------
The formulas in this section define the raw reward V, not final benchmark points. The Scoring section specifies the per-file award and its aggregation.
We define the connectivity of pieces of candy as follows and consider the connected components.
> Two pieces of candy are connected if and only if they are of the same flavor and can reach each other through only pieces of candy of the same flavor in the four directions (front, back, left, right).
Let $n_1,\cdots,n_k$ be the list of sizes of connected components in the final state after receiving 100 pieces of candy, and let $d_i$ be the total number of pieces of candy of flavor $i$.
The raw task reward V is
\\[\mathrm{round}\left(10^6\times\frac{\sum_{i=1}^k n_i^2}{\sum_{i=1}^3 d_i^2}\right)\\]
Your task is to write a program to determine the tilting directions so that you can get as high a score as possible.
Input and Output
--------
First, the flavor of each piece of candy is given from Standard Input in the following format.
~~~
$f_1$ $f_2$ $\cdots$ $f_{100}$
~~~
Each $f_t$ is an integer value between $1$ and $3$, representing the flavor of the $t$-th piece of candy.
After reading the above information, repeat the following process $100$ times.
In the $t$-th process ($1\leq t\leq 100$), a single integer $p_t$ between $1$ and $101-t$ is given from Standard Input.
Number the empty cells from $1$ to $101-t$ by scanning rows from front to back and, within each row, from left to right, skipping occupied cells.
Then the $t$-th piece of candy is placed in the $p_t$-th empty cell.
After reading $p_t$, by representing forward, backward, left, and right by `F`, `B`, `L`, and `R`, respectively, output a single character to Standard Output to indicate which direction to tilt the box.
<font color="red">**The output must be followed by a new line, and you have to flush Standard Output.**</font>
Otherwise, the submission might be judged as <span class='label label-warning' data-toggle='tooltip' data-placement='top' title="Time Limit Exceeded">TLE</span>.
<font color="red">**Note that $p_{t+1}$ will not be given until you output the $t$-th direction.**</font>
Since nothing happens at the 100th tilt, you may skip the output.
#### Example
<table class="table table-bordered">
<thead>
<tr>
<th>$t$</th>
<th>Input</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td>Prior information</td>
<td><pre>2 2 1 3 1 2 1 2 1 $\cdots$ 3</pre></td>
<td></td>
</tr>
<tr>
<td>1</td>
<td><pre>3</pre></td>
<td><pre>R</pre></td>
</tr>
<tr>
<td>2</td>
<td><pre>98</pre></td>
<td><pre>B</pre></td>
</tr>
<tr>
<td>$\vdots$</td>
<td></td>
<td></td>
</tr>
<tr>
<td>100</td>
<td><pre>1</pre></td>
<td><pre>L</pre></td>
</tr>
</tbody>
</table>
<a href="https://img.atcoder.jp/ahc015/b639c75d.html?lang=en&seed=0&output=R%0D%0AB%0D%0AB%0D%0AR%0D%0AF%0D%0AR%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AL%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AB%0D%0AF%0D%0AB%0D%0AL%0D%0AL%0D%0AL%0D%0AL%0D%0AB%0D%0AF%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AL%0D%0AL%0D%0AB%0D%0AL%0D%0AL%0D%0AL%0D%0AB%0D%0AL%0D%0AR%0D%0AF%0D%0AL%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AF%0D%0AL%0D%0AL%0D%0AR%0D%0AB%0D%0AB%0D%0AF%0D%0AL%0D%0AF%0D%0AR%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AR%0D%0AL%0D%0AR%0D%0AL%0D%0AR%0D%0AR%0D%0AB%0D%0AR%0D%0AB%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AF%0D%0AB%0D%0AF%0D%0AR%0D%0AB%0D%0AF%0D%0AF%0D%0AF%0D%0AB%0D%0AB%0D%0AL%0D%0AL%0D%0AR%0D%0AF%0D%0AB%0D%0AL%0D%0AB%0D%0AF%0D%0AB%0D%0AR%0D%0AF%0D%0AF%0D%0AL%0D%0AL%0D%0A">Show example</a>
Input Generation
--------
Let $\mathrm{rand}(L,U)$ be a function that generates a uniform random integer between $L$ and $U$, inclusive.
Each $f_t$ is generated by $\mathrm{rand}(1,3)$.
Each $p_t$ is generated by $\mathrm{rand}(1,101-t)$.
Tools (Input generator and visualizer)
--------
- <a href="https://img.atcoder.jp/ahc015/b639c75d.html?lang=en">Web version</a>: This is more powerful than the local version providing animations and manual play.
- <a href="https://img.atcoder.jp/ahc015/b639c75d.zip">Local version</a>: You need a compilation environment of <a href="https://www.rust-lang.org/">Rust language</a>.
- <a href="https://img.atcoder.jp/ahc015/b639c75d_windows.zip">Pre-compiled binary for Windows</a>: If you are not familiar with the Rust language environment, please use this instead.
Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited.
## Scoring
All feasibility and interaction limits apply before scoring; invalid output receives zero. Points are averaged over test files. For the logarithmic branch, write x for the original maximizing quantity or quality and a for its physical 60-point reference. The improvement factor is r=x/a, and the upper-band award is P=min(100,60+ln(r)). Here ln is natural: r=1 corresponds to 60 points, and on the uncapped logarithmic upper band, r=exp(P-60). Each additional point multiplies the physical improvement factor by e (divides a minimizing objective by e or multiplies a maximizing objective by e, as applicable). The lower-band, zero-objective, and completion rules below still apply.
Let V be the raw task reward defined in the problem, and let B and R be the stored baseline and reference rewards. They are calibration values, not assertions of optimality. The stored reward reference R is distinct from the physical reference a used in the logarithm; a is defined by the inverse or stage-specific formula below. If R<=B, set U=1000000, L=100*max(0,(V-B)/(U-B)), and T=B+0.6*(U-B). Otherwise set L=60*max(0,(V-B)/(R-B)) and T=R. For nonzero minimizing objectives, and all maximizing objectives, keep L exactly whenever L<=60. The following describes the upper band and any valid zero-cost optimum.
Let X be the integer sum of squared same-color connected-component sizes, and D the sum of squared total populations of each color. Its physical reference is a=A=T*D/1e6. When L>60, take x=X, a=A, and r=x/a; award min(100,60+ln(r)).
The checker reports points/100.
结果
Kimi K3.1 ProKimi
max
4.1
平均得分 ↑
- 4.1采样 1 · 部分分
- 23.9k平均输出 token(含思考) ↓
- 29.8输出速度 tokens/s ↑
- 805 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
12.9
平均得分 ↑
- 12.9采样 1 · 部分分
- 12.8k平均输出 token(含思考) ↓
- 26.4输出速度 tokens/s ↑
- 488 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
2.6
平均得分 ↑
- 2.6采样 1 · 部分分
- 24.4k平均输出 token(含思考) ↓
- 174.1输出速度 tokens/s ↑
- 141 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
0.0
平均得分 ↑
- 0.0采样 1 · 编译错误
- 44.3k平均输出 token(含思考) ↓
- 98.8输出速度 tokens/s ↑
- 449 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 387 行
#include <bits/stdc++.h> using namespace std; using Board = array<uint8_t, 100>; static const char DIR_CHAR[4] = {'F', 'B', 'L', 'R'}; Board tiltBoard(const Board &b, int dir) { Board out{}; if (dir == 0) { for (int c = 0; c < 10; ++c) { int w = 0; for (int r = 0; r < 10; ++r) { uint8_t v = b[r * 10 + c]; if (v) out[w++ * 10 + c] = v; } } } else if (dir == 1) { for (int c = 0; c < 10; ++c) { int w = 9; for (int r = 9; r >= 0; --r) { uint8_t v = b[r * 10 + c]; if (v) out[w-- * 10 + c] = v; } } } else if (dir == 2) { for (int r = 0; r < 10; ++r) { int w = 0; for (int c = 0; c < 10; ++c) { uint8_t v = b[r * 10 + c]; if (v) out[r * 10 + w++] = v; } } } else { for (int r = 0; r < 10; ++r) { int w = 9; for (int c = 9; c >= 0; --c) { uint8_t v = b[r * 10 + c]; if (v) out[r * 10 + w--] = v; } } } return out; } void putCandy(Board &b, int flavor, int rank) { for (int i = 0; i < 100; ++i) { if (b[i] == 0) { if (rank == 0) { b[i] = static_cast<uint8_t>(flavor); return; } --rank; } } } int componentScore(const Board &b) { array<uint8_t, 100> used{}; int st[100]; int result = 0; for (int s = 0; s < 100; ++s) { if (b[s] == 0 || used[s]) continue; uint8_t color = b[s]; int top = 0; int sz = 0; st[top++] = s; used[s] = 1; while (top) { int x = st[--top]; ++sz; int r = x / 10; int c = x % 10; if (r > 0) { int y = x - 10; if (!used[y] && b[y] == color) { used[y] = 1; st[top++] = y; } } if (r < 9) { int y = x + 10; if (!used[y] && b[y] == color) { used[y] = 1; st[top++] = y; } } if (c > 0) { int y = x - 1; if (!used[y] && b[y] == color) { used[y] = 1; st[top++] = y; } } if (c < 9) { int y = x + 1; if (!used[y] && b[y] == color) { used[y] = 1; st[top++] = y; } } } result += sz * sz; } return result; } int heuristic(const Board &b) { int score = componentScore(b); int proximity = 0; for (int r = 0; r < 10; ++r) { for (int c = 0; c < 10; ++c) { uint8_t v = b[r * 10 + c]; if (!v) continue; if (r < 9) { if (c > 0 && b[(r + 1) * 10 + c - 1] == v) ++proximity; if (c < 9 && b[(r + 1) * 10 + c + 1] == v) ++proximity; } if (r < 8 && b[(r + 2) * 10 + c] == v) ++proximity; if (c < 8 && b[r * 10 + c + 2] == v) ++proximity; } } return score * 16 + proximity; } uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } struct RNG { uint64_t s; explicit RNG(uint64_t seed) : s(seed ? seed : 0x123456789abcdefULL) {} uint64_t next() { uint64_t x = s; x ^= x >> 12; x ^= x << 25; x ^= x >> 27; s = x; return x * 2685821657736338717ULL; } int integer(int n) { return static_cast<int>(next() % static_cast<uint64_t>(n)); } }; double exactValue(const Board &b, int turn, const array<int, 100> &flavors) { if (turn >= 100) return componentScore(b); int emptyCount = 100 - turn; double total = 0.0; for (int rank = 0; rank < emptyCount; ++rank) { Board placed = b; putCandy(placed, flavors[turn], rank); if (turn == 99) { total += componentScore(placed); } else { double best = -1.0; for (int d = 0; d < 4; ++d) { Board nb = tiltBoard(placed, d); best = max(best, exactValue(nb, turn + 1, flavors)); } total += best; } } return total / emptyCount; } double greedyRollout(const Board &start, int nextTurn, const array<int, 100> &flavors, uint64_t seed) { if (100 - nextTurn <= 2) return exactValue(start, nextTurn, flavors); RNG placeRng(splitmix64(seed)); RNG policyRng(splitmix64(seed ^ 0xd1b54a32d192ed03ULL)); Board b = start; for (int turn = nextTurn; turn <= 97; ++turn) { int rank = placeRng.integer(100 - turn); Board placed = b; putCandy(placed, flavors[turn], rank); if (turn == 97) { double best = -1.0; for (int d = 0; d < 4; ++d) { Board nb = tiltBoard(placed, d); best = max(best, exactValue(nb, 98, flavors)); } return best; } int bestValue = -1; int ties = 0; Board bestBoard{}; for (int d = 0; d < 4; ++d) { Board nb = tiltBoard(placed, d); int value = heuristic(nb); if (value > bestValue) { bestValue = value; bestBoard = nb; ties = 1; } else if (value == bestValue) { ++ties; if (policyRng.integer(ties) == 0) bestBoard = nb; } } b = bestBoard; } return exactValue(b, 98, flavors); } double beamRollout(const Board &start, int nextTurn, const array<int, 100> &flavors, uint64_t seed) { struct Node { Board b; int value; }; int horizon = 100 - nextTurn; int width = (horizon <= 5 ? 8 : 4); RNG rng(splitmix64(seed)); array<int, 100> ranks{}; for (int turn = nextTurn; turn <= 97; ++turn) { ranks[turn] = rng.integer(100 - turn); } vector<Node> current; current.push_back({start, 0}); for (int turn = nextTurn; turn <= 97; ++turn) { if (turn == 97) { double best = -1.0; for (const Node &node : current) { Board placed = node.b; putCandy(placed, flavors[turn], ranks[turn]); for (int d = 0; d < 4; ++d) { Board nb = tiltBoard(placed, d); best = max(best, exactValue(nb, 98, flavors)); } } return best; } vector<Node> next; next.reserve(current.size() * 4); for (const Node &node : current) { Board placed = node.b; putCandy(placed, flavors[turn], ranks[turn]); for (int d = 0; d < 4; ++d) { Board nb = tiltBoard(placed, d); next.push_back({nb, heuristic(nb)}); } } sort(next.begin(), next.end(), [](const Node &a, const Node &b) { return a.value > b.value; }); if (static_cast<int>(next.size()) > width) next.resize(width); current = move(next); } return exactValue(current.front().b, 98, flavors); } double rollout(const Board &start, int nextTurn, const array<int, 100> &flavors, uint64_t seed) { int horizon = 100 - nextTurn; if (horizon <= 4) return exactValue(start, nextTurn, flavors); if (horizon <= 6) return beamRollout(start, nextTurn, flavors, seed); return greedyRollout(start, nextTurn, flavors, seed); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); array<int, 100> flavors{}; for (int i = 0; i < 100; ++i) { if (!(cin >> flavors[i])) return 0; } const auto startTime = chrono::steady_clock::now(); const double TIME_BUDGET = 1.80; Board board{}; for (int turn = 0; turn < 100; ++turn) { int p; if (!(cin >> p)) return 0; putCandy(board, flavors[turn], p - 1); if (turn == 99) return 0; Board candidates[4]; int candidateHeuristic[4]; for (int d = 0; d < 4; ++d) { candidates[d] = tiltBoard(board, d); candidateHeuristic[d] = heuristic(candidates[d]); } int nextTurn = turn + 1; int horizon = 100 - nextTurn; double values[4] = {0, 0, 0, 0}; if (horizon <= 4) { for (int d = 0; d < 4; ++d) { values[d] = exactValue(candidates[d], nextTurn, flavors); } } else { double sums[4] = {0, 0, 0, 0}; int iterations = 0; double elapsed = chrono::duration<double>( chrono::steady_clock::now() - startTime).count(); double remaining = TIME_BUDGET - elapsed; int decisionsLeft = 99 - turn; if (remaining > 0.01) { double localEnd = elapsed + remaining / decisionsLeft; uint64_t baseSeed = 0x6a09e667f3bcc909ULL ^ (static_cast<uint64_t>(turn + 1) * 0x9e3779b97f4a7c15ULL); while (true) { if (iterations > 0) { double now = chrono::duration<double>( chrono::steady_clock::now() - startTime).count(); if (now >= localEnd) break; } uint64_t seed = splitmix64( baseSeed + static_cast<uint64_t>(iterations + 1) * 0xbf58476d1ce4e5b9ULL); for (int d = 0; d < 4; ++d) { sums[d] += rollout( candidates[d], nextTurn, flavors, seed); } ++iterations; } } if (iterations > 0) { for (int d = 0; d < 4; ++d) { values[d] = sums[d] / iterations; } } else { for (int d = 0; d < 4; ++d) { values[d] = candidateHeuristic[d]; } } } int bestDir = 0; for (int d = 1; d < 4; ++d) { if (values[d] > values[bestDir] + 1e-9 || (abs(values[d] - values[bestDir]) <= 1e-9 && candidateHeuristic[d] > candidateHeuristic[bestDir])) { bestDir = d; } } board = candidates[bestDir]; cout << DIR_CHAR[bestDir] << '\n' << flush; } return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 264 行
#include <bits/stdc++.h> using namespace std; struct RNG { uint32_t x; explicit RNG(uint32_t seed = 2463534242u) : x(seed ? seed : 1u) {} inline uint32_t next() { x ^= x << 13; x ^= x >> 17; x ^= x << 5; return x; } }; struct State { array<uint8_t, 100> a{}; }; class Solver { static constexpr char DIR_CHAR[4] = {'F', 'B', 'L', 'R'}; int flavor[100]; RNG rng; static void place(State &s, int f, int p) { for (int i = 0; i < 100; ++i) { if (s.a[i] == 0 && --p == 0) { s.a[i] = static_cast<uint8_t>(f); return; } } } static void tilt(State &s, int dir) { uint8_t tmp[10]; if (dir == 0) { for (int c = 0; c < 10; ++c) { int n = 0; for (int r = 0; r < 10; ++r) { uint8_t v = s.a[r * 10 + c]; if (v) tmp[n++] = v; } for (int r = 0; r < 10; ++r) s.a[r * 10 + c] = r < n ? tmp[r] : 0; } } else if (dir == 1) { for (int c = 0; c < 10; ++c) { int n = 0; for (int r = 9; r >= 0; --r) { uint8_t v = s.a[r * 10 + c]; if (v) tmp[n++] = v; } for (int r = 9, k = 0; r >= 0; --r, ++k) s.a[r * 10 + c] = k < n ? tmp[k] : 0; } } else if (dir == 2) { for (int r = 0; r < 10; ++r) { int n = 0; for (int c = 0; c < 10; ++c) { uint8_t v = s.a[r * 10 + c]; if (v) tmp[n++] = v; } for (int c = 0; c < 10; ++c) s.a[r * 10 + c] = c < n ? tmp[c] : 0; } } else { for (int r = 0; r < 10; ++r) { int n = 0; for (int c = 9; c >= 0; --c) { uint8_t v = s.a[r * 10 + c]; if (v) tmp[n++] = v; } for (int c = 9, k = 0; c >= 0; --c, ++k) s.a[r * 10 + c] = k < n ? tmp[k] : 0; } } } static int score(const State &s) { uint8_t seen[100] = {}; int q[100]; int result = 0; for (int start = 0; start < 100; ++start) { if (seen[start] || s.a[start] == 0) continue; const uint8_t color = s.a[start]; int head = 0, tail = 0; q[tail++] = start; seen[start] = 1; while (head < tail) { int v = q[head++]; int r = v / 10, c = v % 10; if (r > 0) { int u = v - 10; if (!seen[u] && s.a[u] == color) { seen[u] = 1; q[tail++] = u; } } if (r < 9) { int u = v + 10; if (!seen[u] && s.a[u] == color) { seen[u] = 1; q[tail++] = u; } } if (c > 0) { int u = v - 1; if (!seen[u] && s.a[u] == color) { seen[u] = 1; q[tail++] = u; } } if (c < 9) { int u = v + 1; if (!seen[u] && s.a[u] == color) { seen[u] = 1; q[tail++] = u; } } } result += tail * tail; } return result; } double expectedValue(const State &s, int turn) const { if (turn >= 99) { State end = s; place(end, flavor[99], 1); return score(end); } if (turn == 98) { double total = 0.0; for (int p = 1; p <= 2; ++p) { State inserted = s; place(inserted, flavor[98], p); int best = -1; for (int d = 0; d < 4; ++d) { State next = inserted; tilt(next, d); place(next, flavor[99], 1); best = max(best, score(next)); } total += best; } return total * 0.5; } const int empty = 100 - turn; double total = 0.0; for (int p = 1; p <= empty; ++p) { State inserted = s; place(inserted, flavor[turn], p); double best = -1.0; for (int d = 0; d < 4; ++d) { State next = inserted; tilt(next, d); best = max(best, expectedValue(next, turn + 1)); } total += best; } return total / empty; } double rollout(State s, int turn, uint32_t seed) const { RNG local(seed); while (turn < 98) { int p = static_cast<int>(local.next() % (100 - turn)) + 1; place(s, flavor[turn], p); tilt(s, static_cast<int>(local.next() & 3u)); ++turn; } return expectedValue(s, 98); } public: void run() { const auto programStart = chrono::steady_clock::now(); for (int i = 0; i < 100; ++i) cin >> flavor[i]; const auto timeEnd = programStart + chrono::milliseconds(1750); State current; for (int turn = 0; turn < 100; ++turn) { int p; cin >> p; if (turn == 99) break; place(current, flavor[turn], p); int chosen = 0; if (turn == 0) { chosen = 0; } else if (turn >= 94) { double bestValue = -1.0; int bestImmediate = -1; for (int d = 0; d < 4; ++d) { State next = current; tilt(next, d); double value = expectedValue(next, turn + 1); int immediate = score(next); if (value > bestValue + 1e-9 || (abs(value - bestValue) <= 1e-9 && immediate > bestImmediate)) { bestValue = value; bestImmediate = immediate; chosen = d; } } } else { State candidates[4]; int immediate[4]; for (int d = 0; d < 4; ++d) { candidates[d] = current; tilt(candidates[d], d); immediate[d] = score(candidates[d]); } double sums[4] = {}; long long simulations = 0; auto now = chrono::steady_clock::now(); auto turnDeadline = now; if (now < timeEnd) { turnDeadline = now + (timeEnd - now) / (94 - turn); } while (chrono::steady_clock::now() < turnDeadline) { uint32_t seed = rng.next(); for (int d = 0; d < 4; ++d) sums[d] += rollout(candidates[d], turn + 1, seed); ++simulations; } if (simulations == 0) { chosen = static_cast<int>(max_element(immediate, immediate + 4) - immediate); } else { chosen = 0; for (int d = 1; d < 4; ++d) { if (sums[d] > sums[chosen] + 1e-9 || (abs(sums[d] - sums[chosen]) <= 1e-9 && immediate[d] > immediate[chosen])) { chosen = d; } } } } tilt(current, chosen); cout << DIR_CHAR[chosen] << endl; } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solver solver; solver.run(); return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 253 行
#include <bits/stdc++.h> using namespace std; int F[100]; int nei[100][4]; struct Board { unsigned char g[100]; }; static inline void initNei() { memset(nei, -1, sizeof(nei)); for (int r = 0; r < 10; r++) { for (int c = 0; c < 10; c++) { int idx = r * 10 + c, cnt = 0; if (r > 0) nei[idx][cnt++] = idx - 10; if (r < 9) nei[idx][cnt++] = idx + 10; if (c > 0) nei[idx][cnt++] = idx - 1; if (c < 9) nei[idx][cnt++] = idx + 1; while (cnt < 4) nei[idx][cnt++] = -1; } } } static inline void tilt(Board &b, int dir) { unsigned char *g = b.g; if (dir == 0) { // F for (int c = 0; c < 10; c++) { int w = 0; for (int r = 0; r < 10; r++) { int idx = r * 10 + c; if (g[idx]) { g[w * 10 + c] = g[idx]; if (w != r) g[idx] = 0; w++; } } for (int r = w; r < 10; r++) g[r * 10 + c] = 0; } } else if (dir == 1) { // B for (int c = 0; c < 10; c++) { int w = 9; for (int r = 9; r >= 0; r--) { int idx = r * 10 + c; if (g[idx]) { g[w * 10 + c] = g[idx]; if (w != r) g[idx] = 0; w--; } } for (int r = 0; r <= w; r++) g[r * 10 + c] = 0; } } else if (dir == 2) { // L for (int r = 0; r < 10; r++) { int w = 0; for (int c = 0; c < 10; c++) { int idx = r * 10 + c; if (g[idx]) { g[r * 10 + w] = g[idx]; if (w != c) g[idx] = 0; w++; } } for (int c = w; c < 10; c++) g[r * 10 + c] = 0; } } else { // R for (int r = 0; r < 10; r++) { int w = 9; for (int c = 9; c >= 0; c--) { int idx = r * 10 + c; if (g[idx]) { g[r * 10 + w] = g[idx]; if (w != c) g[idx] = 0; w--; } } for (int c = 0; c <= w; c++) g[r * 10 + c] = 0; } } } static inline int calcScore(const Board &b) { unsigned char vis[100]; memset(vis, 0, 100); int total = 0; const unsigned char *g = b.g; for (int s = 0; s < 100; s++) { if (g[s] && !vis[s]) { unsigned char flav = g[s]; int stack[100], top = 0; stack[top++] = s; vis[s] = 1; int cnt = 0; while (top) { int v = stack[--top]; cnt++; for (int k = 0; k < 4; k++) { int u = nei[v][k]; if (u != -1 && !vis[u] && g[u] == flav) { vis[u] = 1; stack[top++] = u; } } } total += cnt * cnt; } } return total; } static inline int evalPolicy(const Board &b) { int sc = calcScore(b); int adj = 0; const unsigned char *g = b.g; for (int r = 0; r < 10; r++) { for (int c = 0; c < 10; c++) { int idx = r * 10 + c; if (!g[idx]) continue; if (c < 9 && g[idx] == g[idx + 1]) adj++; if (r < 9 && g[idx] == g[idx + 10]) adj++; } } return sc * 1000 + adj; } uint32_t rng_state = 123456789; static inline uint32_t rnd() { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17; rng_state ^= rng_state << 5; return rng_state; } int simulateFrom(Board b, int nextStep, const int *pvals) { for (int s = nextStep; s < 100; s++) { int flav = F[s]; int k = pvals[s] - 1; for (int idx = 0; idx < 100; idx++) { if (b.g[idx] == 0) { if (k == 0) { b.g[idx] = flav; break; } k--; } } if (s == 99) break; int bestDir = 0; int bestVal = -1; for (int d = 0; d < 4; d++) { Board nb = b; tilt(nb, d); int val = evalPolicy(nb); if (val > bestVal || (val == bestVal && (((s * 13 + d * 7) & 1) == 0))) { bestVal = val; bestDir = d; } } tilt(b, bestDir); } return calcScore(b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); initNei(); for (int i = 0; i < 100; i++) cin >> F[i]; Board board; memset(board.g, 0, sizeof(board.g)); for (int t = 0; t < 100; t++) { int p; cin >> p; int k = p - 1; for (int idx = 0; idx < 100; idx++) { if (board.g[idx] == 0) { if (k == 0) { board.g[idx] = F[t]; break; } k--; } } if (t == 99) break; int M; if (t < 30) M = 6; else if (t < 60) M = 10; else if (t < 90) M = 16; else M = 30; vector<array<int, 100>> pvals(M); for (int r = 0; r < M; r++) { for (int s = t + 1; s < 100; s++) { int rem = 100 - s; pvals[r][s] = (int)(rnd() % rem) + 1; } } int bestDir = 0; double bestAvg = -1.0; for (int d = 0; d < 4; d++) { Board start = board; tilt(start, d); long long sum = 0; for (int r = 0; r < M; r++) { Board b = start; sum += simulateFrom(b, t + 1, pvals[r].data()); } double avg = (double)sum / M; if (avg > bestAvg) { bestAvg = avg; bestDir = d; } } const char dirChar[4] = {'F', 'B', 'L', 'R'}; cout << dirChar[bestDir] << '\n' << flush; tilt(board, bestDir); } return 0; }
MiniMax M3.1 Flash · 采样 1 · 424 行
#include <bits/stdc++.h> using namespace std; using Board = array<unsigned char, 100>; constexpr int N = 10; constexpr int CELLS = 100; constexpr int TRIALS = 12; constexpr int HORIZON = 12; int flavor[CELLS]; mt19937_64 rng(0xC0FFEE15AHC015ULL); enum Direction { LEFT, RIGHT, FORWARD, BACKWARD }; const char directionChar[4] = {'L', 'R', 'F', 'B'}; Board tiltBoard(const Board& input, int direction) { Board board = input; if (direction == LEFT) { for (int r = 0; r < N; ++r) { int write = 0; for (int c = 0; c < N; ++c) { int pos = r * N + c; if (board[pos] != 0) { int dest = r * N + write; if (dest != pos) { board[dest] = board[pos]; board[pos] = 0; } ++write; } } } } else if (direction == RIGHT) { for (int r = 0; r < N; ++r) { int write = N - 1; for (int c = N - 1; c >= 0; --c) { int pos = r * N + c; if (board[pos] != 0) { int dest = r * N + write; if (dest != pos) { board[dest] = board[pos]; board[pos] = 0; } --write; } } } } else if (direction == FORWARD) { for (int c = 0; c < N; ++c) { int write = 0; for (int r = 0; r < N; ++r) { int pos = r * N + c; if (board[pos] != 0) { int dest = write * N + c; if (dest != pos) { board[dest] = board[pos]; board[pos] = 0; } ++write; } } } } else { for (int c = 0; c < N; ++c) { int write = N - 1; for (int r = N - 1; r >= 0; --r) { int pos = r * N + c; if (board[pos] != 0) { int dest = write * N + c; if (dest != pos) { board[dest] = board[pos]; board[pos] = 0; } --write; } } } } return board; } struct Evaluation { int componentScore = 0; int sameEdges = 0; int allEdges = 0; int projection = 0; }; int componentScore(const Board& board) { bool visited[CELLS] = {}; int stack[CELLS]; int result = 0; constexpr int dr[4] = {-1, 1, 0, 0}; constexpr int dc[4] = {0, 0, -1, 1}; for (int start = 0; start < CELLS; ++start) { if (board[start] == 0 || visited[start]) continue; int top = 0; int size = 0; stack[top++] = start; visited[start] = true; unsigned char color = board[start]; while (top > 0) { int pos = stack[--top]; ++size; int r = pos / N; int c = pos % N; for (int k = 0; k < 4; ++k) { int nr = r + dr[k]; int nc = c + dc[k]; if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue; int next = nr * N + nc; if (board[next] == color && !visited[next]) { visited[next] = true; stack[top++] = next; } } } result += size * size; } return result; } Evaluation evaluateBoard(const Board& board) { Evaluation result; bool visited[CELLS] = {}; int stack[CELLS]; int rowCount[3][N] = {}; int columnCount[3][N] = {}; for (int pos = 0; pos < CELLS; ++pos) { if (board[pos] == 0) continue; int color = board[pos] - 1; ++rowCount[color][pos / N]; ++columnCount[color][pos % N]; } for (int color = 0; color < 3; ++color) { for (int i = 0; i < N; ++i) { result.projection += rowCount[color][i] * rowCount[color][i]; result.projection += columnCount[color][i] * columnCount[color][i]; } } constexpr int dr[4] = {-1, 1, 0, 0}; constexpr int dc[4] = {0, 0, -1, 1}; for (int start = 0; start < CELLS; ++start) { if (board[start] == 0 || visited[start]) continue; int top = 0; int size = 0; stack[top++] = start; visited[start] = true; unsigned char color = board[start]; while (top > 0) { int pos = stack[--top]; ++size; int r = pos / N; int c = pos % N; for (int k = 0; k < 4; ++k) { int nr = r + dr[k]; int nc = c + dc[k]; if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue; int next = nr * N + nc; if (board[next] != 0) { ++result.allEdges; if (board[next] == color) ++result.sameEdges; } if (board[next] == color && !visited[next]) { visited[next] = true; stack[top++] = next; } } } result.componentScore += size * size; } result.sameEdges /= 2; result.allEdges /= 2; return result; } bool betterEvaluation(const Evaluation& a, const Evaluation& b) { if (a.componentScore != b.componentScore) return a.componentScore > b.componentScore; if (a.sameEdges != b.sameEdges) return a.sameEdges > b.sameEdges; if (a.projection != b.projection) return a.projection > b.projection; return a.allEdges > b.allEdges; } int greedyTilt(const Board& board) { int bestDirection = LEFT; Evaluation best; bool first = true; for (int d = 0; d < 4; ++d) { Board next = tiltBoard(board, d); Evaluation current = evaluateBoard(next); if (first || betterEvaluation(current, best)) { first = false; best = current; bestDirection = d; } } return bestDirection; } double recursiveValue(const Board& board, int nextIndex, int depth) { if (depth == 0 || nextIndex >= CELLS) return componentScore(board); int emptyCount = 0; for (unsigned char cell : board) { if (cell == 0) ++emptyCount; } if (emptyCount == 0) return componentScore(board); double total = 0.0; for (int pos = 0; pos < CELLS; ++pos) { if (board[pos] != 0) continue; Board next = board; next[pos] = static_cast<unsigned char>(flavor[nextIndex]); if (nextIndex == CELLS - 1) { total += componentScore(next); } else { double best = -1.0; for (int d = 0; d < 4; ++d) { Board tilted = tiltBoard(next, d); best = max(best, recursiveValue(tilted, nextIndex + 1, depth - 1)); } total += best; } } return total / emptyCount; } int rolloutValue(const Board& initial, int nextIndex, const array<int, HORIZON>& ranks) { Board board = initial; for (int step = 0; step < HORIZON; ++step) { int index = nextIndex + step; int rank = ranks[step]; int currentRank = 0; int destination = -1; for (int pos = 0; pos < CELLS; ++pos) { if (board[pos] != 0) continue; if (currentRank++ == rank) { destination = pos; break; } } if (destination < 0) break; board[destination] = static_cast<unsigned char>(flavor[index]); if (index == CELLS - 1) break; int d = greedyTilt(board); board = tiltBoard(board, d); } return componentScore(board); } double meanOf(const array<double, 4>& values) { return (values[0] + values[1] + values[2] + values[3]) / 4.0; } double meanAbsoluteDeviation(const array<double, 4>& values, double mean) { double result = 0.0; for (double value : values) result += abs(value - mean); return result / 4.0; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); for (int i = 0; i < CELLS; ++i) cin >> flavor[i]; Board board{}; for (int turn = 0; turn < CELLS; ++turn) { int p; cin >> p; int rank = 0; int destination = -1; for (int pos = 0; pos < CELLS; ++pos) { if (board[pos] != 0) continue; if (++rank == p) { destination = pos; break; } } if (destination < 0) return 0; board[destination] = static_cast<unsigned char>(flavor[turn]); if (turn == CELLS - 1) break; array<Board, 4> candidates; array<Evaluation, 4> immediate; array<double, 4> recursive; int remaining = 0; for (unsigned char cell : board) { if (cell == 0) ++remaining; } for (int d = 0; d < 4; ++d) { candidates[d] = tiltBoard(board, d); immediate[d] = evaluateBoard(candidates[d]); } int depth; if (remaining <= 5) { depth = remaining; } else if (remaining <= 7) { depth = 3; } else if (remaining <= 15) { depth = 2; } else { depth = 1; } for (int d = 0; d < 4; ++d) { recursive[d] = recursiveValue(candidates[d], turn + 1, depth); } int bestDirection = LEFT; if (remaining <= 5) { for (int d = 1; d < 4; ++d) { if (recursive[d] > recursive[bestDirection] || (recursive[d] == recursive[bestDirection] && betterEvaluation(immediate[d], immediate[bestDirection]))) { bestDirection = d; } } } else { int horizon = min(HORIZON, remaining); array<double, 4> rollout{}; array<int, HORIZON> ranks{}; for (int trial = 0; trial < TRIALS; ++trial) { for (int step = 0; step < horizon; ++step) { int count = remaining - step; ranks[step] = static_cast<int>(rng() % count); } for (int d = 0; d < 4; ++d) { rollout[d] += rolloutValue(candidates[d], turn + 1, ranks); } } for (int d = 0; d < 4; ++d) rollout[d] /= TRIALS; double rolloutMean = meanOf(rollout); double recursiveMean = meanOf(recursive); array<double, 4> immediateValue{}; for (int d = 0; d < 4; ++d) immediateValue[d] = immediate[d].componentScore; double immediateMean = meanOf(immediateValue); double rolloutMad = meanAbsoluteDeviation(rollout, rolloutMean); double recursiveMad = meanAbsoluteDeviation(recursive, recursiveMean); double immediateMad = meanAbsoluteDeviation(immediateValue, immediateMean); double recursiveWeight = remaining <= 15 ? 1.5 : 1.0; array<double, 4> combined{}; for (int d = 0; d < 4; ++d) { if (rolloutMad > 1e-12) combined[d] += (rollout[d] - rolloutMean) / rolloutMad; if (recursiveMad > 1e-12) combined[d] += recursiveWeight * (recursive[d] - recursiveMean) / recursiveMad; if (immediateMad > 1e-12) combined[d] += 0.25 * (immediateValue[d] - immediateMean) / immediateMad; } for (int d = 1; d < 4; ++d) { if (combined[d] > combined[bestDirection] + 1e-9 || (abs(combined[d] - combined[bestDirection]) <= 1e-9 && (recursive[d] > recursive[bestDirection] || (recursive[d] == recursive[bestDirection] && betterEvaluation(immediate[d], immediate[bestDirection]))))) { bestDirection = d; } } } cout << directionChar[bestDirection] << '\n' << flush; } return 0; }