题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
Story
--------
AtCoder's CEO, Takahashi, loves animals and has a number of pets running free in the AtCoder office.
AtCoder's employees have trouble with the pets interrupting their work, so they have decided to place partitions in the office to create a space where pets cannot come in.
Please create as large a space as possible.
Problem Statement
--------
There are $N$ pets and $M$ people in a room with a floor of $30 \times 30$ squares.
All squares are initially passable, and outside of the $30 \times 30$ squares are impassable.
Let $(x, y)$ be the coordinates of the square in row $x$ from the top ($1\leq x\leq 30$) and column $y$ from the left ($1\leq y\leq 30$).
Repeat the following process for $300$ turns.
First, you choose each person's action from the following three types, and perform each action simultaneously.
- Do nothing and stay in the current position.
- Choose a square adjacent to the current position and make it impassable. You cannot choose a square that contains pets or humans at the start of this turn. <b>You cannot choose a square whose adjacent square contains a pet, either.</b> If you choose a square that is already impassable, nothing happens.
- Move to an adjacent passable square. It is not possible to move to a square that becomes impassable by another person's action in this turn.
After all the people have completed their actions for that turn, each pet moves independently.
Rules for pet movement depend on the type of pet, and some pets may move multiple squares in a single turn.
Details are described later.
Squares containing humans or pets are also passable, and each square can contain any number of humans and pets.
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.
At the end of $300$ turn, for each $i=1,\cdots,M$, let $R_i$ be the set of squares reachable from the final position of person $i$ through only passable squares, and $n_i$ be the number of pets whose final position is in $R_i$.
Then, person $i$ obtains satisfaction of $s_i=\frac{|R_i|}{900}\times 2^{-n_i}$.
The raw task reward V is $\mathrm{round}\left(10^8\times\frac{1}{M}\sum_{i=1}^M s_i\right)$.
Note that if your program terminates abnormally, it may be judged as <span class='label label-warning' data-toggle='tooltip' data-placement='top' title="Wrong Answer">WA</span> instead of <span class='label label-warning' data-toggle='tooltip' data-placement='top' title="Runtime Error">RE</span>.
#### About execution time
Execution time may vary slightly from run to run.
Please measure the execution time in your program to terminate the process, or have enough margin in the execution time.
Input and Output
--------
First, the initial position and type of each pet, and the initial position of each person are given from Standard Input in the following format
~~~
$N$
$px_1$ $py_1$ $pt_1$
$\vdots$
$px_N$ $py_N$ $pt_N$
$M$
$hx_1$ $hy_1$
$\vdots$
$hx_M$ $hy_M$
~~~
$N$ is an integer between $10$ and $20$ representing the number of pets.
$(px_i,py_i)$ represents the coordinates of the initial position of the $i$-th pet, and $pt_i$ is an integer between $1$ and $5$ representing the type of the $i$-th pet.
$M$ is an integer between $5$ and $10$ representing the number of humans.
$(hx_i,hy_i)$ represents the coordinates of the initial position of the $i$-th human.
The initial positions of all pets and humans are guaranteed to be distinct.
After reading the above information, repeat the following process $300$ turns.
First, output a string of length $M$ where the $i$-th character represents the action of the $i$th person as follows on a single line to Standard Output.
<font color="red">**After the output, 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' tit
le="Time Limit Exceeded">TLE</span> .
- `.`: Do nothing and stay in the current position.
- `u`, `d`, `l`, `r`: Let $(x,y)$ be the current position. Make the square $(x-1,y)$, $(x+1,y)$, $(x,y-1)$, or $(x,y+1)$ impassable, respectively.
- `U`, `D`, `L`, `R`: Let $(x,y)$ be the current position. Move to the the square $(x-1,y)$, $(x+1,y)$, $(x,y-1)$, or $(x,y+1)$, respectively.
After the output, $N$ strings are given to Standard Input in a single line, separated by spaces.
The $i$-th string represents movement of the $i$-th pet in that turn.
If the pet does not move, the string is `.`.
If it does move, the string is a sequence of characters `U`, `D`, `L`, and `R` representing the movement of one square up, down, left, and right, respectively.
<a href="https://img.atcoder.jp/ahc008/f828b9475ffb41d54f05619db6ccbd4f.html?lang=en&show=example">Show example</a>
Pets Movement Rules
--------
We define a basic move as follows: move to a square chosen at random among the adjacent passable squares. From the condition of the squares that can be made impassable, such squares always exist.
Each pet $i$ performs the following moves depending on $pt_i$, an integer value between $1$ and $5$ representing its type.
1. Cow: Perform one basic move.
2. Pig: Perform two basic moves.
3. Rabbit: Perform three basic moves.
4. Dog: Move toward a target person as follows. The first turn starts with no target. If it has no target, the target person is in the current position, or there exists no path to the target person, then it selects one person uniformly at random among those reachable from the current position, excluding those in the current position. If there is no such person, reset to no target and perform one basic move. Otherwise, move to an adjacent passable square that shortens the shortest distance to the target person (if there are multiple such squares, choose one of them uniformly at random), and then perform one basic move. If it reaches the destination after the first or the second move, reset to no target.
5. Cat: Move toward a target square as follows. The first turn starts with no target. If it has no target or there exists no path to the target square, then it selects one square uniformly at random among those reachable from the current position, excluding the current position. If there exists no such square, do nothing. Otherwise, move to an adjacent passable square that shortens the shortest distance to the target square (if there are multiple such squares, choose one of them uniformly at random), and then perform one basic move. If it reaches the destination after the first or the second move, reset to no target.
Input Generation
--------
Let $\mathrm{rand}(L,U)$ be a function that generates a uniform random integer between $L$ and $U$, inclusive.
We generate the number of pets by $N=\mathrm{rand}(10, 20)$.
The initial position of each pet is chosen uniformly at random from the coordinates that have not been chosen yet.
We generate the type of each pet by $pt_i=\mathrm{rand}(1, 5)$.
We generate the number of humans by $M=\mathrm{rand}(5, 10)$.
The initial position of each human is chosen uniformly at random from the coordinates that have not been chosen yet.
Tools
--------
- <a href="https://img.atcoder.jp/ahc008/tools_v3.zip">Local tester</a>: You need a compilation environment of <a href="https://www.rust-lang.org/">Rust language</a>.
- For those who are not familiar with the Rust language environment, we have prepared a pre-compiled binary for Windows. <a href="https://img.atcoder.jp/ahc008/tools_x86_64-pc-windows-gnu_v3.zip">tools_x86_64-pc-windows-gnu.zip</a>
- <font color="red">The first version contained a bug in the cat's movement, which has been fixed at 130 minutes after the contest started. Please re-download it.</font>
- We have added more examples in README. If you don't know how to use the tools, please refer to README. Also, as stated in the rules, you are free to share information on how to run the provided tools.
- <a href="https://img.atcoder.jp/ahc008/f828b9475ffb41d54f05619db6ccbd4f.html?lang=en">Web visualizer</a>: By pasting the output generated by the local tester into the Output field, you can display the animation of the execution result.
<font color="red">You are allowed to share output images (png or gif) of the provided visualizer for seed=0 on twitter during the contest.</font> You have to use the specified hashtag and public account. You can only share visualization results and scores for seed=0. Do not share scores for other seeds or mention solutions or discussions. <a href="https://twitter.com/search?q=%23AHC008%20%23visualizer&src=typed_query&f=live">List of shared images.</a>
#### Specification of input/output files used by the tools
Input files for the local tester consist of the prior information (the initial position and type of each pet, and the initial position of each person) followed by a random seed value to generate pet movements.
Since the pet's movement depends on human actions, the input file contains only the random seed value and not specific movements.
The local tester writes outputs from your program directly to the output file.
Your program may output comment lines starting with `#`.
The web version of the visualizer displays the comment lines at the time they are output, which may be useful for debugging and analysis.
Since the judge program ignores all comment lines, you can submit a program that outputs comment lines as is.
## 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. Set L=60*max(0,(V-B)/(R-B)), with 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 unrounded mean, over humans, of (reachable area/900)*2^(-number of reachable pets). Its physical reference is a=A=R/1e8. 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
0.0
平均得分 ↑
- 0.0采样 1 · 答案错误
- 33.2k平均输出 token(含思考) ↓
- 25.3输出速度 tokens/s ↑
- 1314 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
0.0
平均得分 ↑
- 0.0采样 1 · 答案错误
- 15.6k平均输出 token(含思考) ↓
- 23.1输出速度 tokens/s ↑
- 678 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
0.6
平均得分 ↑
- 0.6采样 1 · 部分分
- 43.0k平均输出 token(含思考) ↓
- 215.1输出速度 tokens/s ↑
- 200 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
0.2
平均得分 ↑
- 0.2采样 1 · 部分分
- 84.5k平均输出 token(含思考) ↓
- 90.9输出速度 tokens/s ↑
- 932 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 532 行
#include <bits/stdc++.h> using namespace std; static constexpr int H = 30; static constexpr int W = 30; static constexpr int CELLS = H * W; static constexpr int INF = 1e9; struct Pet { int x, y, type; }; struct BFSResult { array<int, CELLS> dist; array<int, CELLS> prev; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const char buildChar[4] = {'u', 'd', 'l', 'r'}; const char moveChar[4] = {'U', 'D', 'L', 'R'}; auto cellId = [](int x, int y) { return x * W + y; }; auto cellX = [](int v) { return v / W; }; auto cellY = [](int v) { return v % W; }; auto inside = [](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; }; int N; cin >> N; vector<Pet> pets(N); for (auto &p : pets) { cin >> p.x >> p.y >> p.type; --p.x; --p.y; } int M; cin >> M; vector<pair<int, int>> humans(M); for (auto &h : humans) { cin >> h.first >> h.second; --h.first; --h.second; } vector<int> lines = {4, 9, 14, 19, 24}; vector<char> onLine(CELLS, 0); for (int x = 0; x < H; ++x) { for (int y = 0; y < W; ++y) { bool v = find(lines.begin(), lines.end(), x) != lines.end(); v |= find(lines.begin(), lines.end(), y) != lines.end(); onLine[cellId(x, y)] = v; } } auto makeIntervals = [&](const vector<int> &ls) { vector<pair<int, int>> res; int start = 0; for (int l : ls) { if (start <= l - 1) res.push_back({start, l - 1}); start = l + 1; } res.push_back({start, 29}); return res; }; vector<pair<int, int>> rowIntervals = makeIntervals(lines); vector<pair<int, int>> colIntervals = makeIntervals(lines); const int K = (int)rowIntervals.size(); const int ROOMS = K * K; auto bandOf = [&](const vector<pair<int, int>> &intervals, int v) { for (int i = 0; i < (int)intervals.size(); ++i) { if (intervals[i].first <= v && v <= intervals[i].second) return i; } return -1; }; vector<int> roomId(CELLS, -1); for (int x = 0; x < H; ++x) { for (int y = 0; y < W; ++y) { int a = bandOf(rowIntervals, x); int b = bandOf(colIntervals, y); if (a >= 0 && b >= 0) roomId[cellId(x, y)] = a * K + b; } } struct Door { int cell; int a, b; }; vector<Door> doors; vector<int> doorAt(CELLS, -1); auto addDoor = [&](int x, int y, int a, int b) { int id = (int)doors.size(); int c = cellId(x, y); doors.push_back({c, a, b}); doorAt[c] = id; }; for (int c : lines) { for (auto [l, r] : rowIntervals) { int x = (l + r) / 2; addDoor(x, c, roomId[cellId(x, c - 1)], roomId[cellId(x, c + 1)]); } } for (int r : lines) { for (auto [l, rr] : colIntervals) { int y = (l + rr) / 2; addDoor(r, y, roomId[cellId(r - 1, y)], roomId[cellId(r + 1, y)]); } } vector<char> baseWall(CELLS, 0); int baseLeft = 0; for (int c = 0; c < CELLS; ++c) { if (onLine[c] && doorAt[c] < 0) { baseWall[c] = 1; ++baseLeft; } } vector<char> blocked(CELLS, 0); vector<char> tainted(ROOMS, 0); vector<int> assigned(M, -1); vector<int> failed(M, 0); vector<array<int, CELLS>> avoidUntil(M); for (auto &a : avoidUntil) a.fill(0); auto runBFS = [&](int start) { BFSResult b; b.dist.fill(-1); b.prev.fill(-1); queue<int> q; b.dist[start] = 0; b.prev[start] = start; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); int x = cellX(v), y = cellY(v); for (int d = 0; d < 4; ++d) { int nx = x + dx[d], ny = y + dy[d]; if (!inside(nx, ny)) continue; int to = cellId(nx, ny); if (blocked[to] || b.dist[to] >= 0) continue; b.dist[to] = b.dist[v] + 1; b.prev[to] = v; q.push(to); } } return b; }; auto markPetRoom = [&](const Pet &p, vector<char> &bad) { int c = cellId(p.x, p.y); if (roomId[c] >= 0) { bad[roomId[c]] = 1; return; } if (doorAt[c] >= 0) { const Door &d = doors[doorAt[c]]; if (d.a >= 0) bad[d.a] = 1; if (d.b >= 0) bad[d.b] = 1; return; } for (int k = 0; k < 4; ++k) { int nx = p.x + dx[k], ny = p.y + dy[k]; if (!inside(nx, ny)) continue; int r = roomId[cellId(nx, ny)]; if (r >= 0) bad[r] = 1; } }; for (int turn = 0; turn < 300; ++turn) { bool closure = (baseLeft == 0); vector<char> badRoom(ROOMS, 0); if (closure) { for (const auto &p : pets) markPetRoom(p, tainted); badRoom = tainted; bool anyGood = false; for (char v : badRoom) { if (!v) { anyGood = true; break; } } if (!anyGood) { fill(badRoom.begin(), badRoom.end(), 0); for (const auto &p : pets) markPetRoom(p, badRoom); } } vector<int> desired; vector<char> desiredSet(CELLS, 0); if (!closure) { for (int c = 0; c < CELLS; ++c) { if (baseWall[c] && !blocked[c]) { desired.push_back(c); desiredSet[c] = 1; } } } else { for (const Door &d : doors) { if (blocked[d.cell]) continue; if (d.a < 0 || d.b < 0) continue; if (badRoom[d.a] != badRoom[d.b]) { desired.push_back(d.cell); desiredSet[d.cell] = 1; } } } for (int h = 0; h < M; ++h) { int t = assigned[h]; if (t >= 0 && (!desiredSet[t] || blocked[t])) { assigned[h] = -1; failed[h] = 0; } } vector<int> owner(CELLS, -1); for (int h = 0; h < M; ++h) { int t = assigned[h]; if (t < 0) continue; if (owner[t] >= 0) { assigned[h] = -1; } else { owner[t] = h; } } auto goodCell = [&](int c) { return roomId[c] >= 0 && !badRoom[roomId[c]]; }; auto chooseStand = [&](int target, const BFSResult &bfs) { int requiredRoom = -1; if (closure) { int di = doorAt[target]; if (di < 0) return -1; const Door &d = doors[di]; if (badRoom[d.a] == badRoom[d.b]) return -1; requiredRoom = !badRoom[d.a] ? d.a : d.b; } int tx = cellX(target), ty = cellY(target); int best = -1; int bestDist = INF; for (int k = 0; k < 4; ++k) { int sx = tx + dx[k], sy = ty + dy[k]; if (!inside(sx, sy)) continue; int s = cellId(sx, sy); if (blocked[s] || bfs.dist[s] < 0) continue; if (closure && roomId[s] != requiredRoom) continue; if (bfs.dist[s] < bestDist) { bestDist = bfs.dist[s]; best = s; } } return best; }; for (int h = 0; h < M; ++h) { if (assigned[h] >= 0) continue; int start = cellId(humans[h].first, humans[h].second); BFSResult bfs = runBFS(start); int bestTarget = -1; int bestScore = INF; for (int t : desired) { if (owner[t] >= 0 || blocked[t]) continue; int s = chooseStand(t, bfs); if (s < 0) continue; int score = bfs.dist[s]; if (avoidUntil[h][t] > turn) score += 50; if (score < bestScore) { bestScore = score; bestTarget = t; } } if (bestTarget >= 0) { assigned[h] = bestTarget; owner[bestTarget] = h; failed[h] = 0; } } auto basicCanBuild = [&](int h, int target) { int hx = humans[h].first, hy = humans[h].second; int tx = cellX(target), ty = cellY(target); if (abs(hx - tx) + abs(hy - ty) != 1) return false; if (blocked[target]) return false; for (const auto &p : pets) { int dist = abs(p.x - tx) + abs(p.y - ty); if (dist <= 1) return false; } for (const auto &hp : humans) { if (cellId(hp.first, hp.second) == target) return false; } return true; }; auto safeToClose = [&](int target) { if (!closure || turn >= 295) return true; int di = doorAt[target]; if (di < 0) return true; const Door &d = doors[di]; int bad = badRoom[d.a] ? d.a : d.b; int tx = cellX(target), ty = cellY(target); int start = -1; for (int k = 0; k < 4; ++k) { int nx = tx + dx[k], ny = ty + dy[k]; if (!inside(nx, ny)) continue; int c = cellId(nx, ny); if (roomId[c] == bad) { start = c; break; } } if (start < 0) return true; blocked[target] = 1; vector<char> seen(CELLS, 0); queue<int> q; seen[start] = 1; q.push(start); bool hasGood = false; bool hasHuman = false; while (!q.empty()) { int v = q.front(); q.pop(); if (goodCell(v)) hasGood = true; for (const auto &hp : humans) { if (cellId(hp.first, hp.second) == v) hasHuman = true; } int x = cellX(v), y = cellY(v); for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (!inside(nx, ny)) continue; int to = cellId(nx, ny); if (blocked[to] || seen[to]) continue; seen[to] = 1; q.push(to); } } blocked[target] = 0; return hasGood || !hasHuman; }; vector<char> action(M, '.'); vector<int> buildTarget(M, -1); vector<int> moveTarget(M, -1); for (int h = 0; h < M; ++h) { int target = assigned[h]; if (target < 0) continue; int start = cellId(humans[h].first, humans[h].second); BFSResult bfs = runBFS(start); int stand = chooseStand(target, bfs); if (stand < 0) { assigned[h] = -1; failed[h] = 0; continue; } if (stand == start) { if (basicCanBuild(h, target)) { if (safeToClose(target)) { for (int k = 0; k < 4; ++k) { int nx = humans[h].first + dx[k]; int ny = humans[h].second + dy[k]; if (inside(nx, ny) && cellId(nx, ny) == target) { action[h] = buildChar[k]; buildTarget[h] = target; break; } } failed[h] = 0; } } else { ++failed[h]; if (failed[h] >= 2) { avoidUntil[h][target] = turn + 5; assigned[h] = -1; failed[h] = 0; } } } else { failed[h] = 0; int cur = stand; bool valid = true; while (cur != start && bfs.prev[cur] != start) { cur = bfs.prev[cur]; if (cur < 0) { valid = false; break; } } if (!valid || cur < 0) continue; int nx = cellX(cur), ny = cellY(cur); for (int k = 0; k < 4; ++k) { if (humans[h].first + dx[k] == nx && humans[h].second + dy[k] == ny) { action[h] = moveChar[k]; moveTarget[h] = cur; break; } } } } for (int h = 0; h < M; ++h) { if (action[h] != '.' || assigned[h] >= 0) continue; int start = cellId(humans[h].first, humans[h].second); if (!closure) { if (!desiredSet[start]) continue; for (int k = 0; k < 4; ++k) { int nx = humans[h].first + dx[k]; int ny = humans[h].second + dy[k]; if (!inside(nx, ny)) continue; int to = cellId(nx, ny); if (blocked[to] || desiredSet[to]) continue; action[h] = moveChar[k]; moveTarget[h] = to; break; } } else { if (goodCell(start)) continue; BFSResult bfs = runBFS(start); int goal = -1; int bestDist = INF; for (int c = 0; c < CELLS; ++c) { if (bfs.dist[c] >= 0 && bfs.dist[c] < bestDist && goodCell(c)) { bestDist = bfs.dist[c]; goal = c; } } if (goal < 0 || goal == start) continue; int cur = goal; while (cur != start && bfs.prev[cur] != start) { cur = bfs.prev[cur]; if (cur < 0) break; } if (cur < 0 || cur == start) continue; int nx = cellX(cur), ny = cellY(cur); for (int k = 0; k < 4; ++k) { if (humans[h].first + dx[k] == nx && humans[h].second + dy[k] == ny) { action[h] = moveChar[k]; moveTarget[h] = cur; break; } } } } vector<char> willBlock(CELLS, 0); for (int h = 0; h < M; ++h) { if (buildTarget[h] >= 0) willBlock[buildTarget[h]] = 1; } for (int h = 0; h < M; ++h) { if (moveTarget[h] < 0) continue; int to = moveTarget[h]; if (blocked[to] || willBlock[to]) { moveTarget[h] = -1; action[h] = '.'; } } for (int h = 0; h < M; ++h) { int t = buildTarget[h]; if (t < 0) continue; blocked[t] = 1; if (baseWall[t]) --baseLeft; } for (int h = 0; h < M; ++h) { if (moveTarget[h] >= 0) { humans[h].first = cellX(moveTarget[h]); humans[h].second = cellY(moveTarget[h]); } } string output(M, '.'); for (int h = 0; h < M; ++h) output[h] = action[h]; cout << output << endl; for (int i = 0; i < N; ++i) { string s; if (!(cin >> s)) return 0; for (char c : s) { if (c == 'U') --pets[i].x; else if (c == 'D') ++pets[i].x; else if (c == 'L') --pets[i].y; else if (c == 'R') ++pets[i].y; } } } return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 398 行
#include <bits/stdc++.h> using namespace std; struct Pet { int x, y, type; }; struct Gate { int x, y; int a, b; }; static const int dx[4] = {-1, 1, 0, 0}; static const int dy[4] = {0, 0, -1, 1}; bool inside(int x, int y) { return 1 <= x && x <= 30 && 1 <= y && y <= 30; } bool wallLine(int v) { return v == 6 || v == 12 || v == 18 || v == 24; } int cellIndex(int v) { if (v <= 5) return 0; if (v <= 11) return 1; if (v <= 17) return 2; if (v <= 23) return 3; return 4; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<Pet> pets(N); for (auto &p : pets) cin >> p.x >> p.y >> p.type; int M; cin >> M; vector<pair<int, int>> humans(M); for (auto &h : humans) cin >> h.first >> h.second; bool blocked[31][31] = {}; bool planned[31][31] = {}; bool isGate[31][31] = {}; int gateId[31][31]; memset(gateId, -1, sizeof(gateId)); vector<Gate> gates; const int center[5] = {3, 9, 15, 21, 27}; for (int i = 1; i <= 4; ++i) { for (int j = 0; j < 5; ++j) { int id = (int)gates.size(); int x = 6 * i, y = center[j]; gates.push_back({x, y, (i - 1) * 5 + j, i * 5 + j}); isGate[x][y] = true; gateId[x][y] = id; } } for (int i = 0; i < 5; ++i) { for (int j = 1; j <= 4; ++j) { int id = (int)gates.size(); int x = center[i], y = 6 * j; gates.push_back({x, y, i * 5 + (j - 1), i * 5 + j}); isGate[x][y] = true; gateId[x][y] = id; } } for (int x = 1; x <= 30; ++x) { for (int y = 1; y <= 30; ++y) { if ((wallLine(x) || wallLine(y)) && !isGate[x][y]) { planned[x][y] = true; } } } auto normalCell = [&](int x, int y) { return !wallLine(x) && !wallLine(y); }; auto canBuild = [&](int x, int y) { if (!inside(x, y) || blocked[x][y]) return false; for (const auto &p : pets) { if (p.x == x && p.y == y) return false; } for (const auto &h : humans) { if (h.first == x && h.second == y) return false; } for (const auto &p : pets) { if (abs(p.x - x) + abs(p.y - y) == 1) return false; } return true; }; auto bfsDist = [&](int sx, int sy) { vector<vector<int>> d(31, vector<int>(31, -1)); queue<pair<int, int>> q; d[sx][sy] = 0; q.push({sx, sy}); while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (!inside(nx, ny) || blocked[nx][ny] || d[nx][ny] != -1) continue; d[nx][ny] = d[x][y] + 1; q.push({nx, ny}); } } return d; }; auto distanceToTask = [&](const vector<vector<int>> &d, int tx, int ty) { int res = 1000000; for (int k = 0; k < 4; ++k) { int nx = tx + dx[k], ny = ty + dy[k]; if (inside(nx, ny) && !blocked[nx][ny] && d[nx][ny] != -1) { res = min(res, d[nx][ny]); } } return res; }; auto pathToTask = [&](int sx, int sy, int tx, int ty, const bool extra[31][31]) { vector<vector<int>> d(31, vector<int>(31, -1)); vector<vector<pair<int, int>>> par(31, vector<pair<int, int>>(31, {-1, -1})); queue<pair<int, int>> q; d[sx][sy] = 0; q.push({sx, sy}); pair<int, int> goal = {-1, -1}; auto isGoal = [&](int x, int y) { return abs(x - tx) + abs(y - ty) == 1; }; if (isGoal(sx, sy)) goal = {sx, sy}; while (!q.empty() && goal.first == -1) { auto [x, y] = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (!inside(nx, ny) || d[nx][ny] != -1) continue; if (blocked[nx][ny] || extra[nx][ny]) continue; if (nx == tx && ny == ty) continue; d[nx][ny] = d[x][y] + 1; par[nx][ny] = {x, y}; if (isGoal(nx, ny)) { goal = {nx, ny}; break; } q.push({nx, ny}); } } if (goal.first == -1 || goal == make_pair(sx, sy)) { return make_pair(0, 0); } pair<int, int> cur = goal; while (par[cur.first][cur.second] != make_pair(sx, sy)) { cur = par[cur.first][cur.second]; if (cur.first == -1) return make_pair(0, 0); } return cur; }; auto pathToSafeCell = [&](int sx, int sy, const int petCell[25], const bool extra[31][31]) { vector<vector<int>> d(31, vector<int>(31, -1)); vector<vector<pair<int, int>>> par(31, vector<pair<int, int>>(31, {-1, -1})); queue<pair<int, int>> q; d[sx][sy] = 0; q.push({sx, sy}); pair<int, int> goal = {-1, -1}; auto safe = [&](int x, int y) { if (!normalCell(x, y)) return false; int id = cellIndex(x) * 5 + cellIndex(y); return petCell[id] == 0; }; if (safe(sx, sy)) goal = {sx, sy}; while (!q.empty() && goal.first == -1) { auto [x, y] = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (!inside(nx, ny) || d[nx][ny] != -1) continue; if (blocked[nx][ny] || extra[nx][ny]) continue; d[nx][ny] = d[x][y] + 1; par[nx][ny] = {x, y}; if (safe(nx, ny)) { goal = {nx, ny}; break; } q.push({nx, ny}); } } if (goal.first == -1 || goal == make_pair(sx, sy)) { return make_pair(0, 0); } pair<int, int> cur = goal; while (par[cur.first][cur.second] != make_pair(sx, sy)) { cur = par[cur.first][cur.second]; if (cur.first == -1) return make_pair(0, 0); } return cur; }; vector<int> tx(M, -1), ty(M, -1); bool previousClosure = false; for (int turn = 1; turn <= 300; ++turn) { int petCell[25] = {}; bool petOnGate[40] = {}; for (const auto &p : pets) { int gid = gateId[p.x][p.y]; if (gid != -1) { petOnGate[gid] = true; petCell[gates[gid].a]++; petCell[gates[gid].b]++; } else if (normalCell(p.x, p.y)) { petCell[cellIndex(p.x) * 5 + cellIndex(p.y)]++; } } bool allBuilt = true; for (int x = 1; x <= 30; ++x) { for (int y = 1; y <= 30; ++y) { if (planned[x][y] && !blocked[x][y]) allBuilt = false; } } bool closure = allBuilt; if (closure != previousClosure) { fill(tx.begin(), tx.end(), -1); fill(ty.begin(), ty.end(), -1); previousClosure = closure; } bool requiredGate[40] = {}; bool protectedCell[25] = {}; vector<char> evacuate(M, false); if (closure) { for (int g = 0; g < (int)gates.size(); ++g) { requiredGate[g] = petOnGate[g] || petCell[gates[g].a] > 0 || petCell[gates[g].b] > 0; } for (int i = 0; i < M; ++i) { int x = humans[i].first, y = humans[i].second; if (normalCell(x, y)) { int c = cellIndex(x) * 5 + cellIndex(y); if (petCell[c] > 0) { protectedCell[c] = true; evacuate[i] = true; } } else { int g = gateId[x][y]; if (g != -1 && requiredGate[g] && !blocked[x][y]) { evacuate[i] = true; } } } } bool allowedTask[31][31] = {}; if (!closure) { for (int x = 1; x <= 30; ++x) { for (int y = 1; y <= 30; ++y) { if (planned[x][y] && !blocked[x][y]) allowedTask[x][y] = true; } } } else { for (int g = 0; g < (int)gates.size(); ++g) { const auto &gate = gates[g]; if (!requiredGate[g] || blocked[gate.x][gate.y]) continue; if (protectedCell[gate.a] || protectedCell[gate.b]) continue; allowedTask[gate.x][gate.y] = true; } } bool assigned[31][31] = {}; for (int i = 0; i < M; ++i) { if (evacuate[i]) { tx[i] = ty[i] = -1; continue; } auto dist = bfsDist(humans[i].first, humans[i].second); if (tx[i] != -1) { if (!allowedTask[tx[i]][ty[i]] || assigned[tx[i]][ty[i]] || distanceToTask(dist, tx[i], ty[i]) >= 1000000) { tx[i] = ty[i] = -1; } } if (tx[i] == -1) { int best = 1000000; int bx = -1, by = -1; for (int x = 1; x <= 30; ++x) { for (int y = 1; y <= 30; ++y) { if (!allowedTask[x][y] || assigned[x][y]) continue; int d = distanceToTask(dist, x, y); if (d >= 1000000) continue; int cost = d; if (d == 0 && !canBuild(x, y)) cost += 4; if (cost < best) { best = cost; bx = x; by = y; } } } tx[i] = bx; ty[i] = by; } if (tx[i] != -1) assigned[tx[i]][ty[i]] = true; } string answer(M, '.'); bool buildCells[31][31] = {}; for (int i = 0; i < M; ++i) { if (evacuate[i] || tx[i] == -1) continue; int hx = humans[i].first, hy = humans[i].second; if (abs(hx - tx[i]) + abs(hy - ty[i]) == 1 && canBuild(tx[i], ty[i])) { if (tx[i] < hx) answer[i] = 'u'; else if (tx[i] > hx) answer[i] = 'd'; else if (ty[i] < hy) answer[i] = 'l'; else answer[i] = 'r'; buildCells[tx[i]][ty[i]] = true; } } for (int i = 0; i < M; ++i) { if (answer[i] != '.') continue; pair<int, int> nxt = {0, 0}; if (evacuate[i]) { nxt = pathToSafeCell(humans[i].first, humans[i].second, petCell, buildCells); } else if (tx[i] != -1) { nxt = pathToTask(humans[i].first, humans[i].second, tx[i], ty[i], buildCells); } if (nxt.first == 0) continue; int nx = nxt.first, ny = nxt.second; int hx = humans[i].first, hy = humans[i].second; if (!inside(nx, ny) || blocked[nx][ny] || buildCells[nx][ny]) continue; if (abs(nx - hx) + abs(ny - hy) != 1) continue; if (nx < hx) answer[i] = 'U'; else if (nx > hx) answer[i] = 'D'; else if (ny < hy) answer[i] = 'L'; else answer[i] = 'R'; } cout << answer << '\n'; cout.flush(); for (int i = 0; i < M; ++i) { char c = answer[i]; if (c == 'u' || c == 'd' || c == 'l' || c == 'r') { blocked[tx[i]][ty[i]] = true; } else if (c == 'U') humans[i].first--; else if (c == 'D') humans[i].first++; else if (c == 'L') humans[i].second--; else if (c == 'R') humans[i].second++; } for (int i = 0; i < N; ++i) { string movement; cin >> movement; if (movement == ".") continue; for (char c : movement) { if (c == 'U') pets[i].x--; else if (c == 'D') pets[i].x++; else if (c == 'L') pets[i].y--; else if (c == 'R') pets[i].y++; } } } return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 390 行
#include <bits/stdc++.h> using namespace std; int N, M; int pet_x[20], pet_y[20], pet_t[20]; int human_x[10], human_y[10]; bool wall[31][31]; bool inside[31][31]; vector<pair<int,int>> all_wall_cells; vector<pair<int,int>> unbuilt_walls; pair<int,int> door; int door_inside_x, door_inside_y; bool door_built = false; bool sealed = false; bool can_place(int x, int y) { if (x < 1 || x > 30 || y < 1 || y > 30) return false; if (wall[x][y]) return false; for (int i = 0; i < M; ++i) { if (human_x[i] == x && human_y[i] == y) return false; } for (int i = 0; i < N; ++i) { if (pet_x[i] == x && pet_y[i] == y) return false; } int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; for (int d = 0; d < 4; ++d) { int nx = x + dx[d], ny = y + dy[d]; if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30) { for (int i = 0; i < N; ++i) { if (pet_x[i] == nx && pet_y[i] == ny) return false; } } } return true; } char bfs_move(int sx, int sy, int tx, int ty, bool blocked[31][31]) { if (sx == tx && sy == ty) return '.'; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; pair<int,int> prev[31][31]; bool vis[31][31] = {false}; queue<pair<int,int>> q; q.push({sx, sy}); vis[sx][sy] = true; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); if (x == tx && y == ty) break; for (int d = 0; d < 4; ++d) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 1 || nx > 30 || ny < 1 || ny > 30) continue; if (blocked[nx][ny]) continue; if (vis[nx][ny]) continue; vis[nx][ny] = true; prev[nx][ny] = {x, y}; q.push({nx, ny}); } } if (!vis[tx][ty]) return '.'; int cx = tx, cy = ty; while (!(prev[cx][cy].first == sx && prev[cx][cy].second == sy)) { auto p = prev[cx][cy]; cx = p.first; cy = p.second; } int ddx = cx - sx; int ddy = cy - sy; if (ddx == -1 && ddy == 0) return 'U'; if (ddx == 1 && ddy == 0) return 'D'; if (ddx == 0 && ddy == -1) return 'L'; if (ddx == 0 && ddy == 1) return 'R'; return '.'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 0; i < N; ++i) { cin >> pet_x[i] >> pet_y[i] >> pet_t[i]; } cin >> M; for (int i = 0; i < M; ++i) { cin >> human_x[i] >> human_y[i]; } double best_score = -1e18; int best_corner = -1, best_H = -1, best_W = -1; pair<int,int> best_door; int best_x1, best_x2, best_y1, best_y2; vector<pair<int,int>> best_wall_cells; for (int corner = 0; corner < 4; ++corner) { for (int H = 1; H <= 30; ++H) { for (int W = 1; W <= 30; ++W) { int x1, x2, y1, y2; if (corner == 0) { x1 = 1; x2 = H; y1 = 1; y2 = W; } else if (corner == 1) { x1 = 1; x2 = H; y1 = 31 - W; y2 = 30; } else if (corner == 2) { x1 = 31 - H; x2 = 30; y1 = 1; y2 = W; } else { x1 = 31 - H; x2 = 30; y1 = 31 - W; y2 = 30; } bool pet_inside = false; for (int i = 0; i < N; ++i) { if (pet_x[i] >= x1 && pet_x[i] <= x2 && pet_y[i] >= y1 && pet_y[i] <= y2) { pet_inside = true; break; } } if (pet_inside) continue; vector<pair<int,int>> wall_cells; if (corner == 0) { if (H < 30) for (int y = 1; y <= W; ++y) wall_cells.push_back({H + 1, y}); if (W < 30) for (int x = 1; x <= H; ++x) wall_cells.push_back({x, W + 1}); } else if (corner == 1) { if (H < 30) for (int y = 31 - W; y <= 30; ++y) wall_cells.push_back({H + 1, y}); if (W < 30) for (int x = 1; x <= H; ++x) wall_cells.push_back({x, 30 - W}); } else if (corner == 2) { if (H < 30) for (int y = 1; y <= W; ++y) wall_cells.push_back({30 - H, y}); if (W < 30) for (int x = 31 - H; x <= 30; ++x) wall_cells.push_back({x, W + 1}); } else { if (H < 30) for (int y = 31 - W; y <= 30; ++y) wall_cells.push_back({30 - H, y}); if (W < 30) for (int x = 31 - H; x <= 30; ++x) wall_cells.push_back({x, 30 - W}); } if (wall_cells.empty()) continue; int area = H * W; int perimeter = wall_cells.size(); int inside_humans = 0; for (int i = 0; i < M; ++i) { if (human_x[i] >= x1 && human_x[i] <= x2 && human_y[i] >= y1 && human_y[i] <= y2) inside_humans++; } double best_door_score = -1e18; pair<int,int> best_door_cand; for (auto &wp : wall_cells) { int wx = wp.first, wy = wp.second; int max_human_dist = 0; for (int i = 0; i < M; ++i) { int d = abs(human_x[i] - wx) + abs(human_y[i] - wy); if (d > max_human_dist) max_human_dist = d; } int min_pet_dist = 1e9; for (int i = 0; i < N; ++i) { int d = abs(pet_x[i] - wx) + abs(pet_y[i] - wy); if (d < min_pet_dist) min_pet_dist = d; } if (min_pet_dist == 1e9) min_pet_dist = 0; double door_score = (min_pet_dist + 1.0) / (max_human_dist + 1.0); if (door_score > best_door_score) { best_door_score = door_score; best_door_cand = {wx, wy}; } } if (best_door_score == -1e18) continue; int wx = best_door_cand.first, wy = best_door_cand.second; int max_human_dist = 0; for (int i = 0; i < M; ++i) { int d = abs(human_x[i] - wx) + abs(human_y[i] - wy); if (d > max_human_dist) max_human_dist = d; } int min_pet_dist = 1e9; for (int i = 0; i < N; ++i) { int d = abs(pet_x[i] - wx) + abs(pet_y[i] - wy); if (d < min_pet_dist) min_pet_dist = d; } if (min_pet_dist == 1e9) min_pet_dist = 0; int pet_on_wall = 0; for (auto &wp : wall_cells) { for (int i = 0; i < N; ++i) { if (pet_x[i] == wp.first && pet_y[i] == wp.second) pet_on_wall++; } } double score = area * (1.0 + inside_humans * 0.5) * (1.0 + min_pet_dist / 10.0) / (1.0 + max_human_dist / 10.0) / (1.0 + perimeter / 10.0); score *= pow(0.8, pet_on_wall); if (score > best_score) { best_score = score; best_corner = corner; best_H = H; best_W = W; best_door = best_door_cand; best_x1 = x1; best_x2 = x2; best_y1 = y1; best_y2 = y2; best_wall_cells = wall_cells; } } } } int corner = best_corner; int H = best_H, W = best_W; int room_x1 = best_x1, room_x2 = best_x2, room_y1 = best_y1, room_y2 = best_y2; for (int x = 1; x <= 30; ++x) for (int y = 1; y <= 30; ++y) inside[x][y] = (x >= room_x1 && x <= room_x2 && y >= room_y1 && y <= room_y2); all_wall_cells = best_wall_cells; door = best_door; auto get_inside_neighbor = [&](int wx, int wy) -> pair<int,int> { int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; for (int d = 0; d < 4; ++d) { int nx = wx + dx[d], ny = wy + dy[d]; if (nx >= 1 && nx <= 30 && ny >= 1 && ny <= 30 && inside[nx][ny]) { return {nx, ny}; } } return {-1, -1}; }; auto di = get_inside_neighbor(door.first, door.second); door_inside_x = di.first; door_inside_y = di.second; if (door_inside_x == -1) { door_inside_x = door.first; door_inside_y = door.second; } unbuilt_walls.clear(); for (auto &wp : all_wall_cells) { if (wp != door) unbuilt_walls.push_back(wp); } door_built = false; sealed = false; memset(wall, 0, sizeof(wall)); for (int turn = 0; turn < 300; ++turn) { vector<char> actions(M, '.'); set<pair<int,int>> claimed; vector<pair<int,int>> temp_walls; bool all_humans_inside = true; for (int i = 0; i < M; ++i) { if (!inside[human_x[i]][human_y[i]]) { all_humans_inside = false; break; } } bool no_pets_inside = true; for (int i = 0; i < N; ++i) { if (inside[pet_x[i]][pet_y[i]]) { no_pets_inside = false; break; } } bool all_walls_built = unbuilt_walls.empty(); bool sealing_mode = all_humans_inside && all_walls_built && !door_built && (no_pets_inside || turn > 250); if (sealing_mode) { bool placed = false; for (int i = 0; i < M; ++i) { int hx = human_x[i], hy = human_y[i]; if (abs(hx - door.first) + abs(hy - door.second) == 1) { if (can_place(door.first, door.second)) { char act; if (door.first == hx - 1 && door.second == hy) act = 'u'; else if (door.first == hx + 1 && door.second == hy) act = 'd'; else if (door.first == hx && door.second == hy - 1) act = 'l'; else if (door.first == hx && door.second == hy + 1) act = 'r'; else continue; actions[i] = act; claimed.insert(door); temp_walls.push_back(door); placed = true; break; } } } } else { for (int i = 0; i < M; ++i) { if (actions[i] != '.') continue; int hx = human_x[i], hy = human_y[i]; for (auto &wp : unbuilt_walls) { int wx = wp.first, wy = wp.second; if (claimed.count(wp)) continue; if (abs(hx - wx) + abs(hy - wy) == 1) { if (can_place(wx, wy)) { char act; if (wx == hx - 1 && wy == hy) act = 'u'; else if (wx == hx + 1 && wy == hy) act = 'd'; else if (wx == hx && wy == hy - 1) act = 'l'; else if (wx == hx && wy == hy + 1) act = 'r'; else continue; actions[i] = act; claimed.insert(wp); temp_walls.push_back(wp); break; } } } } } bool blocked[31][31]; for (int x = 1; x <= 30; ++x) for (int y = 1; y <= 30; ++y) blocked[x][y] = wall[x][y]; for (auto &wp : temp_walls) { blocked[wp.first][wp.second] = true; } for (int i = 0; i < M; ++i) { if (actions[i] != '.') continue; if (sealed) { actions[i] = '.'; continue; } int hx = human_x[i], hy = human_y[i]; bool is_inside = inside[hx][hy]; int tx = -1, ty = -1; if (sealing_mode) { tx = door_inside_x; ty = door_inside_y; } else { if (is_inside) { if (unbuilt_walls.empty()) { actions[i] = '.'; continue; } int best_d = 1e9; int best_tx = -1, best_ty = -1; for (auto &wp : unbuilt_walls) { int wx = wp.first, wy = wp.second; int nx = -1, ny = -1; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; for (int d = 0; d < 4; ++d) { int ax = wx + dx[d], ay = wy + dy[d]; if (ax >= 1 && ax <= 30 && ay >= 1 && ay <= 30 && inside[ax][ay]) { nx = ax; ny = ay; break; } } if (nx == -1) continue; int dist = abs(hx - nx) + abs(hy - ny); if (dist < best_d) { best_d = dist; best_tx = nx; best_ty = ny; } } tx = best_tx; ty = best_ty; } else { tx = door_inside_x; ty = door_inside_y; } } if (tx == -1) { actions[i] = '.'; continue; } char move = bfs_move(hx, hy, tx, ty, blocked); actions[i] = move; if (move == 'U') human_x[i]--; else if (move == 'D') human_x[i]++; else if (move == 'L') human_y[i]--; else if (move == 'R') human_y[i]++; } string out = ""; for (int i = 0; i < M; ++i) out += actions[i]; cout << out << endl; cout.flush(); for (auto &wp : temp_walls) { wall[wp.first][wp.second] = true; if (wp == door) { door_built = true; } else { for (auto it = unbuilt_walls.begin(); it != unbuilt_walls.end(); ++it) { if (*it == wp) { unbuilt_walls.erase(it); break; } } } } if (door_built) sealed = true; for (int i = 0; i < N; ++i) { string s; cin >> s; if (s == ".") continue; for (char c : s) { if (c == 'U') pet_x[i]--; else if (c == 'D') pet_x[i]++; else if (c == 'L') pet_y[i]--; else if (c == 'R') pet_y[i]++; } } } return 0; }
MiniMax M3.1 Flash · 采样 1 · 681 行
#include <bits/stdc++.h> using namespace std; constexpr int L = 30; constexpr int V = L * L; struct Pet { int x, y, type; }; struct Rect { int top = 0, bottom = L - 1, left = 0, right = L - 1; int area() const { return (bottom - top + 1) * (right - left + 1); } bool contains(int x, int y) const { return top <= x && x <= bottom && left <= y && y <= right; } }; enum Phase { BUILD, GATHER, CLOSE, DONE, RETRY }; struct Target { int x = -1, y = -1; int ix = -1, iy = -1; bool built = false; bool gate = false; }; struct Stage { Rect container; Rect region; vector<Target> targets; Phase phase = BUILD; int depth = 0; }; struct BFSResult { vector<int> dist; vector<int> parent; }; int N, M; vector<Pet> pets; vector<pair<int, int>> humans; array<bool, V> blocked{}; Stage stage; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int id(int x, int y) { return x * L + y; } bool insideBoard(int x, int y) { return 0 <= x && x < L && 0 <= y && y < L; } bool petAt(int x, int y) { if (!insideBoard(x, y)) return false; for (const Pet &p : pets) { if (p.x == x && p.y == y) return true; } return false; } bool humanAt(int x, int y) { for (auto [hx, hy] : humans) { if (hx == x && hy == y) return true; } return false; } bool adjacentToPet(int x, int y) { for (int d = 0; d < 4; ++d) { if (petAt(x + dx[d], y + dy[d])) return true; } return false; } bool wallLegal(int targetIndex) { const Target &t = stage.targets[targetIndex]; if (t.built || blocked[id(t.x, t.y)]) return false; if (humanAt(t.x, t.y) || petAt(t.x, t.y)) return false; if (adjacentToPet(t.x, t.y)) return false; return true; } int manhattan(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); } int distanceToRect(int x, int y, const Rect &r) { int a = 0, b = 0; if (x < r.left) a = r.left - x; else if (x > r.right) a = x - r.right; if (y < r.top) b = r.top - y; else if (y > r.bottom) b = y - r.bottom; return a + b; } Rect findCandidate(const Rect &container, int depth) { int pref[L + 1][L + 1] = {}; for (int x = 0; x < L; ++x) { for (int y = 0; y < L; ++y) { pref[x + 1][y + 1] = pref[x][y + 1] + pref[x + 1][y] - pref[x][y] + (petAt(x, y) ? 1 : 0); } } auto petCount = [&](const Rect &r) { return pref[r.bottom + 1][r.right + 1] - pref[r.top][r.right + 1] - pref[r.bottom + 1][r.left] + pref[r.top][r.left]; }; double factor = 1.0; if (depth == 1) factor = 0.70; else if (depth == 2) factor = 0.52; else if (depth >= 3) factor = 0.42; int maxArea = max(1, static_cast<int>(container.area() * factor)); double bestScore = -1e100; int bestArea = -1; Rect best; for (int top = container.top; top <= container.bottom; ++top) { for (int bottom = top; bottom <= container.bottom; ++bottom) { if (bottom - top + 1 == container.bottom - container.top + 1 && top == container.top && bottom == container.bottom) { continue; } for (int left = container.left; left <= container.right; ++left) { for (int right = left; right <= container.right; ++right) { if (right - left + 1 == container.right - container.left + 1 && left == container.left && right == container.right) { continue; } Rect r{top, bottom, left, right}; int area = r.area(); if (area > maxArea || petCount(r) != 0) continue; int walls = 0; if (top > container.top) walls += right - left + 1; if (bottom < container.bottom) walls += right - left + 1; if (left > container.left) walls += bottom - top + 1; if (right < container.right) walls += bottom - top + 1; if (walls == 0) continue; int distanceSum = 0; int humansInside = 0; for (auto [hx, hy] : humans) { int d = distanceToRect(hx, hy, r); distanceSum += d; if (d == 0) ++humansInside; } int nearbyPets = 0; for (const Pet &p : pets) { if (!r.contains(p.x, p.y) && distanceToRect(p.x, p.y, r) <= 1) { ++nearbyPets; } } int exteriorSides = 0; if (top - container.top >= 2) ++exteriorSides; if (container.bottom - bottom >= 2) ++exteriorSides; if (left - container.left >= 2) ++exteriorSides; if (container.right - right >= 2) ++exteriorSides; double score = area - 0.50 * walls - 0.12 * distanceSum - 2.0 * nearbyPets - 5.0 * humansInside + 3.0 * exteriorSides; if (score > bestScore + 1e-9 || (abs(score - bestScore) < 1e-9 && area > bestArea)) { bestScore = score; bestArea = area; best = r; } } } } } return best; } bool setupStage(const Rect &container, const Rect ®ion, int depth) { Stage next; next.container = container; next.region = region; next.depth = depth; next.phase = BUILD; auto addTarget = [&](int x, int y, int ix, int iy) { next.targets.push_back({x, y, ix, iy, false, false}); }; if (region.top > container.top) { for (int y = region.left; y <= region.right; ++y) { addTarget(region.top - 1, y, region.top, y); } } if (region.right < container.right) { for (int x = region.top; x <= region.bottom; ++x) { addTarget(x, region.right + 1, x, region.right); } } if (region.bottom < container.bottom) { for (int y = region.right; y >= region.left; --y) { addTarget(region.bottom + 1, y, region.bottom, y); } } if (region.left > container.left) { for (int x = region.bottom; x >= region.top; --x) { addTarget(x, region.left - 1, x, region.left); } } if (next.targets.empty()) return false; int bestGate = -1; long long bestCost = LLONG_MAX; for (int i = 0; i < (int)next.targets.size(); ++i) { const Target &t = next.targets[i]; if (petAt(t.x, t.y)) continue; long long cost = 0; for (auto [hx, hy] : humans) { cost += manhattan(hx, hy, t.ix, t.iy); } for (const Pet &p : pets) { int d = manhattan(p.x, p.y, t.x, t.y); if (d <= 1) cost += 12; else cost += d / 5; } if (cost < bestCost) { bestCost = cost; bestGate = i; } } if (bestGate == -1) bestGate = 0; next.targets[bestGate].gate = true; stage = std::move(next); return true; } BFSResult bfsFrom(int start, const vector<bool> &extraBlocked) { BFSResult result; result.dist.assign(V, -1); result.parent.assign(V, -1); queue<int> q; int s = id(start / L, start % L); result.dist[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); int x = v / L, y = v % L; for (int d = 0; d < 4; ++d) { int nx = x + dx[d], ny = y + dy[d]; if (!insideBoard(nx, ny)) continue; int u = id(nx, ny); if (blocked[u] || extraBlocked[u]) continue; if (result.dist[u] != -1) continue; result.dist[u] = result.dist[v] + 1; result.parent[u] = v; q.push(u); } } return result; } int nextStep(int start, int destination, const BFSResult &bfs) { if (start == destination) return start; int v = destination; while (bfs.parent[v] != start && bfs.parent[v] != -1) { v = bfs.parent[v]; } return v; } void updateStage() { if (stage.phase == BUILD) { bool complete = true; for (const Target &t : stage.targets) { if (!t.gate && !t.built) { complete = false; break; } } if (complete) stage.phase = GATHER; } if (stage.phase == GATHER) { bool allInside = true; for (auto [x, y] : humans) { if (!stage.region.contains(x, y)) { allInside = false; break; } } if (allInside) stage.phase = CLOSE; } if (stage.phase == CLOSE) { bool gateBuilt = false; for (const Target &t : stage.targets) { if (t.gate && t.built) gateBuilt = true; } if (gateBuilt) { bool dirty = false; for (const Pet &p : pets) { if (stage.region.contains(p.x, p.y)) { dirty = true; break; } } if (!dirty) { stage.phase = DONE; } else { Rect container = stage.region; int nextDepth = stage.depth + 1; Rect candidate = findCandidate(container, nextDepth); if (!setupStage(container, candidate, nextDepth)) { stage.container = container; stage.depth = nextDepth; stage.phase = RETRY; } } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; pets.resize(N); for (Pet &p : pets) { cin >> p.x >> p.y >> p.type; --p.x; --p.y; } cin >> M; humans.resize(M); for (auto &[x, y] : humans) { cin >> x >> y; --x; --y; } Rect board{0, L - 1, 0, L - 1}; Rect initial = findCandidate(board, 0); if (!setupStage(board, initial, 0)) return 0; for (int turn = 0; turn < 300; ++turn) { if (stage.phase == RETRY) { Rect candidate = findCandidate(stage.container, stage.depth); if (setupStage(stage.container, candidate, stage.depth)) { // The new stage is ready. } } updateStage(); vector<int> actionType(M, 0); vector<int> actionX(M, -1), actionY(M, -1); vector<int> wallId(M, -1); if (stage.phase == BUILD) { vector<bool> extraBlocked(V, false); for (int i = 0; i < (int)stage.targets.size(); ++i) { const Target &t = stage.targets[i]; if (!t.built && !t.gate) { extraBlocked[id(t.x, t.y)] = true; } } vector<BFSResult> bfs(M); for (int h = 0; h < M; ++h) { bfs[h] = bfsFrom(id(humans[h].first, humans[h].second), extraBlocked); } struct Edge { int cost, human, target, sx, sy; }; vector<Edge> edges; for (int h = 0; h < M; ++h) { bool humanInside = stage.region.contains(humans[h].first, humans[h].second); for (int ti = 0; ti < (int)stage.targets.size(); ++ti) { if (!wallLegal(ti)) continue; const Target &t = stage.targets[ti]; int bestDist = INT_MAX, bestX = -1, bestY = -1; int fallbackDist = INT_MAX, fallbackX = -1, fallbackY = -1; for (int d = 0; d < 4; ++d) { int sx = t.x + dx[d], sy = t.y + dy[d]; if (!insideBoard(sx, sy) || !stage.container.contains(sx, sy)) continue; int dist = bfs[h].dist[id(sx, sy)]; if (dist < 0) continue; bool standInside = stage.region.contains(sx, sy); if (standInside == humanInside) { if (dist < bestDist) { bestDist = dist; bestX = sx; bestY = sy; } } else if (dist < fallbackDist) { fallbackDist = dist; fallbackX = sx; fallbackY = sy; } } if (bestX == -1) { bestDist = fallbackDist; bestX = fallbackX; bestY = fallbackY; } if (bestX != -1) { edges.push_back({bestDist, h, ti, bestX, bestY}); } } } sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b) { if (a.cost != b.cost) return a.cost < b.cost; if (a.target != b.target) return a.target < b.target; return a.human < b.human; }); vector<int> claimedTarget(stage.targets.size(), -1); vector<int> assignedTarget(M, -1); vector<int> assignedStandX(M, -1), assignedStandY(M, -1); for (const Edge &e : edges) { if (assignedTarget[e.human] != -1) continue; if (claimedTarget[e.target] != -1) continue; assignedTarget[e.human] = e.target; assignedStandX[e.human] = e.sx; assignedStandY[e.human] = e.sy; claimedTarget[e.target] = e.human; } int gateIndex = -1; for (int i = 0; i < (int)stage.targets.size(); ++i) { if (stage.targets[i].gate) { gateIndex = i; break; } } for (int h = 0; h < M; ++h) { if (assignedTarget[h] == -1) { bool onFutureWall = false; for (int ti = 0; ti < (int)stage.targets.size(); ++ti) { const Target &t = stage.targets[ti]; if (!t.built && !t.gate && humans[h].first == t.x && humans[h].second == t.y) { onFutureWall = true; break; } } if (onFutureWall && gateIndex != -1) { const Target &g = stage.targets[gateIndex]; int destination = id(g.ix, g.iy); if (bfs[h].dist[destination] > 0) { int to = nextStep(id(humans[h].first, humans[h].second), destination, bfs[h]); actionType[h] = 1; actionX[h] = to / L; actionY[h] = to % L; } } continue; } int ti = assignedTarget[h]; const Target &t = stage.targets[ti]; int current = id(humans[h].first, humans[h].second); int stand = id(assignedStandX[h], assignedStandY[h]); if (current == stand) { actionType[h] = 2; actionX[h] = t.x; actionY[h] = t.y; wallId[h] = ti; } else { int to = nextStep(current, stand, bfs[h]); actionType[h] = 1; actionX[h] = to / L; actionY[h] = to % L; } } } else if (stage.phase == GATHER) { int gateIndex = -1; for (int i = 0; i < (int)stage.targets.size(); ++i) { if (stage.targets[i].gate) { gateIndex = i; break; } } if (gateIndex != -1) { const Target &g = stage.targets[gateIndex]; int destination = id(g.ix, g.iy); vector<bool> extraBlocked(V, false); for (int h = 0; h < M; ++h) { if (stage.region.contains(humans[h].first, humans[h].second)) continue; BFSResult bfs = bfsFrom(id(humans[h].first, humans[h].second), extraBlocked); if (bfs.dist[destination] > 0) { int to = nextStep(id(humans[h].first, humans[h].second), destination, bfs); actionType[h] = 1; actionX[h] = to / L; actionY[h] = to % L; } } } } else if (stage.phase == CLOSE) { int gateIndex = -1; for (int i = 0; i < (int)stage.targets.size(); ++i) { if (stage.targets[i].gate) { gateIndex = i; break; } } if (gateIndex != -1) { const Target &g = stage.targets[gateIndex]; int destination = id(g.ix, g.iy); vector<bool> extraBlocked(V, false); int chosen = -1, bestDist = INT_MAX; vector<BFSResult> bfs(M); for (int h = 0; h < M; ++h) { bfs[h] = bfsFrom(id(humans[h].first, humans[h].second), extraBlocked); int d = bfs[h].dist[destination]; if (d >= 0 && d < bestDist) { bestDist = d; chosen = h; } } if (chosen != -1) { int current = id(humans[chosen].first, humans[chosen].second); if (current == destination) { if (wallLegal(gateIndex)) { actionType[chosen] = 2; actionX[chosen] = g.x; actionY[chosen] = g.y; wallId[chosen] = gateIndex; } else { for (int d = 0; d < 4; ++d) { int nx = humans[chosen].first + dx[d]; int ny = humans[chosen].second + dy[d]; if (insideBoard(nx, ny) && stage.region.contains(nx, ny) && !blocked[id(nx, ny)]) { actionType[chosen] = 1; actionX[chosen] = nx; actionY[chosen] = ny; break; } } } } else if (bestDist > 0) { int to = nextStep(current, destination, bfs[chosen]); actionType[chosen] = 1; actionX[chosen] = to / L; actionY[chosen] = to % L; } } } } vector<bool> wallDestination(V, false); for (int h = 0; h < M; ++h) { if (actionType[h] == 2 && wallId[h] != -1) { if (!wallLegal(wallId[h])) { actionType[h] = 0; actionX[h] = actionY[h] = -1; wallId[h] = -1; } else { wallDestination[id(actionX[h], actionY[h])] = true; } } } for (int h = 0; h < M; ++h) { if (actionType[h] != 1) continue; int x = actionX[h], y = actionY[h]; if (!insideBoard(x, y) || blocked[id(x, y)] || wallDestination[id(x, y)]) { actionType[h] = 0; actionX[h] = actionY[h] = -1; } } string output(M, '.'); for (int h = 0; h < M; ++h) { if (actionType[h] == 1) { int ddx = actionX[h] - humans[h].first; int ddy = actionY[h] - humans[h].second; if (ddx == -1) output[h] = 'U'; else if (ddx == 1) output[h] = 'D'; else if (ddy == -1) output[h] = 'L'; else output[h] = 'R'; } else if (actionType[h] == 2) { int ddx = actionX[h] - humans[h].first; int ddy = actionY[h] - humans[h].second; if (ddx == -1) output[h] = 'u'; else if (ddx == 1) output[h] = 'd'; else if (ddy == -1) output[h] = 'l'; else output[h] = 'r'; } } for (int h = 0; h < M; ++h) { if (actionType[h] == 2 && wallId[h] != -1) { blocked[id(actionX[h], actionY[h])] = true; stage.targets[wallId[h]].built = true; } } for (int h = 0; h < M; ++h) { if (actionType[h] == 1) { humans[h] = {actionX[h], actionY[h]}; } } cout << output << endl; for (int i = 0; i < N; ++i) { string movement; if (!(cin >> movement)) return 0; for (char c : movement) { if (c == 'U') --pets[i].x; else if (c == 'D') ++pets[i].x; else if (c == 'L') --pets[i].y; else if (c == 'R') ++pets[i].y; } } } return 0; }