题面
来源:USACO 官网。模型收到的是同一份题面的文字。
**Note: The time limit for this problem is 4s, twice the default.**
Bessie is using her nifty telescope to take photos of all the stars in the night sky. Her telescope can capture an $N \times N$ ($1 \leq N \leq 1000$) photo of the stars where each pixel is either a star or empty sky. Each star will be represented by exactly one pixel, and no two distinct stars share the same pixel.
Overnight, something strange happens to the stars in the sky. Every star either disappears or moves $A$ pixels to the right, and $B$ pixels downwards ($0 \leq A,B \leq N$). If a star disappears or moves beyond the photo boundary, it no longer appears in the second photo.
Bessie took photos before and after the shifting positions, but after experimenting in Mootoshop, she accidentally superimposed one photo onto the other. Now, she can see white pixels where both photos were empty, gray pixels where stars existed in exactly one photo, and black pixels where there was a star in both photos. Bessie also remembers that no new stars moved into the frame of the second photo, so her first photo contains all of the stars in the night sky.
Given the final photo, determine the minimum possible number of stars in the sky before the shifting incident for $T$ ($1 \leq T \leq 1000$) independent test cases. If no arrangement of stars can produce the given final photo, output $-1$.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains $T$ and $T$ test cases will follow.The first line of each test case will contain $N$ $A$ $B$.
Then follow $N$ lines each representing one row of the superimposed photo. The $i$th row from the top is represented by a string $c_{i,1}c_{i,2}\dots c_{i,N}$, where each $c_{i,j} \in \{W,G,B\}$, representing the colors white, gray, and black respectively.
It is guaranteed that the sum of $N^2$ over all test cases will not exceed $10^7$.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output the minimum number of stars that existed before the shifting, or $-1$ if impossible.SAMPLE INPUT:
1 3 0 0 WWB BBB GGG
SAMPLE OUTPUT:
7In this example, there is no shifting. The first photo is as follows: (. for sky, * for star)
..* *** ***
The second photo, where the stars on the bottom row disappeared, is as follows:
..* *** ...
This is the only way to produce the superimposed photo, so the minimum possible number of initial stars is $7$.
SAMPLE INPUT:
3 5 1 2 GWGWW WGWWW WBWGW WWWWW WWGWW 3 1 1 WWW WBW WWW 3 1 0 GGB GGW WWW
SAMPLE OUTPUT:
4 -1 4
In the first case, there were at least $4$ stars at the start. If we let $(r,c)$ denote the intersection of the $r$th row from the top and $c$th column from the left, one possibility is that they were initially at $(1,1), (3,2), (2,2),$ and $(1,3)$. All the stars shifted, except for the one at $(2,2)$ which disappeared.
In the second case, there is no arrangement of stars in the initial photo that can produce the middle black pixel given the shift.
In the third case, there were at least $4$ stars at the start. One possibility is that they were initially at $(1,1), (1,2), (1,3),$ and $(2,1)$. In the second photo, the star originally at $(1,1)$ disappeared and the star originally at $(1,3)$ moved off frame. The other two stars shifted to the right by 1.
SCORING:
- Input 3: $A=B=0$
- Inputs 4-7: $A=1, B=0, N\le 10$
- Inputs 8-9: $A=1, B=0$
- Inputs 10-12: No additional constraints.
Problem credits: Suhas Nagar
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 12.0k平均输出 token(含思考) ↓
- 28.1输出速度 tokens/s ↑
- 427 秒延迟 ↓
- 0.87×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.71Pylint 得分 ↑
- 26圈复杂度 ↓
- 59.4可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 21.8k平均输出 token(含思考) ↓
- 204.4输出速度 tokens/s ↑
- 107 秒延迟 ↓
- 0.85×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 14Pylint 问题 ↓
- 8.27Pylint 得分 ↑
- 29圈复杂度 ↓
- 57.9可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 32.2k平均输出 token(含思考) ↓
- 126.3输出速度 tokens/s ↑
- 258 秒延迟 ↓
- 0.65×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.82Pylint 得分 ↑
- 26圈复杂度 ↓
- 61.5可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): data = sys.stdin.buffer.read().split() if not data: return t = int(data[0]) pos = 1 answers = [] for _ in range(t): n = int(data[pos]) a = int(data[pos + 1]) b = int(data[pos + 2]) pos += 3 grid = data[pos:pos + n] pos += n # With zero shift, every non-white pixel needs one initial star. if a == 0 and b == 0: answers.append(str(sum(n - row.count(b'W') for row in grid))) continue # Every star moves out of the frame, so the second photo is empty. if a >= n or b >= n: gray = 0 impossible = False for row in grid: gray += row.count(b'G') if b'B' in row: impossible = True answers.append(str(-1 if impossible else gray)) continue inf = n * n + 1 total = 0 impossible = False # Each chain follows repeated applications of the shift (b, a). # dp0/dp1 are the minimum costs when the last pixel has no initial # star / has an initial star, respectively. for r in range(n): for c in range(n): # Process each chain only from its first pixel. if r >= b and c >= a: continue dp0, dp1 = 0, inf i, j = r, c while i < n and j < n: color = grid[i][j] if color == 71: # G if dp0 < dp1: dp0, dp1 = dp1, dp0 + 1 else: dp0, dp1 = dp1, dp1 + 1 elif color == 66: # B # A black pixel needs an initial star here and an # initial star at its predecessor that shifts here. if dp1 >= inf: impossible = True break dp0 = inf dp1 += 1 else: # W if dp1 < dp0: dp0 = dp1 dp1 = inf i += b j += a if impossible: break total += dp0 if dp0 < dp1 else dp1 if impossible: break answers.append(str(-1 if impossible else total)) sys.stdout.write('\n'.join(answers)) if __name__ == '__main__': main()
import sys def solve() -> None: data = sys.stdin.buffer.read().split() it = iter(data) T = int(next(it)) out = [] W = ord('W') Bchar = ord('B') INF = 10**15 for _ in range(T): N = int(next(it)) A = int(next(it)) B = int(next(it)) grid = [next(it) for _ in range(N)] # Special case: no movement. if A == 0 and B == 0: ans = 0 for row in grid: for ch in row: if ch != W: ans += 1 out.append(str(ans)) continue ans = 0 possible = True # Each cell belongs to a chain following the shift vector (B, A). # Process every chain starting from a cell with no valid predecessor. for r in range(N): if not possible: break # A cell (r, c) is a chain start iff r < B or c < A. if r < B: c_limit = N else: c_limit = A for c in range(c_limit): cr = r cc = c ch = grid[cr][cc] # Initialize DP for the first cell of this chain. if ch == W: # p = 0, q = 0 d0 = 0 d1 = INF elif ch == Bchar: # q = 1 is impossible without a predecessor. possible = False break else: # 'G' # Must be p = 1, q = 0, costing one initial star. d0 = INF d1 = 1 while True: nr = cr + B nc = cc + A # End of this chain. if nr >= N or nc >= N: best = d0 if d0 < d1 else d1 if best >= INF: possible = False else: ans += best break cr = nr cc = nc ch = grid[cr][cc] if ch == W: # Current cell: p = 0, q = 0. best_prev = d0 if d0 < d1 else d1 nd0 = best_prev nd1 = INF elif ch == Bchar: # Current cell: p = 1, q = 1. # Requires previous cell to have p = 1. nd0 = INF if d1 < INF: nd1 = d1 + 1 else: nd1 = INF else: # 'G' # Option 1: p = 1, q = 0, cost 1. best_prev = d0 if d0 < d1 else d1 if best_prev < INF: nd1 = best_prev + 1 else: nd1 = INF # Option 2: p = 0, q = 1, cost 0. # Requires previous cell to have p = 1. nd0 = d1 d0 = nd0 d1 = nd1 if d0 >= INF and d1 >= INF: possible = False break if not possible: break if possible: out.append(str(ans)) else: out.append("-1") sys.stdout.write("\n".join(out)) if __name__ == "__main__": solve()
import sys def minimum_stars(n, a, b, rows): # With no displacement, a star either remains visible in both photos # (black) or disappears (gray). White pixels contain no initial star. if a == 0 and b == 0: return sum(len(row) - row.count(b"W") for row in rows) # Every moved star leaves the photograph. if a >= n or b >= n: answer = 0 for row in rows: if b"B" in row: return -1 answer += row.count(b"G") return answer picture = b"".join(rows) # A is horizontal, B is vertical. dr, dc = b, a step = dr * n + dc inf = n * n + 1 WHITE, GRAY, BLACK = 87, 71, 66 # A cell has an in-frame successor iff both coordinates are below these. last_row = n - dr last_col = n - dc answer = 0 # A chain starts at a cell whose predecessor is outside the picture. # Such cells satisfy r < dr or c < dc. for r in range(n): start_columns = n if r < dr else dc row_base = r * n for c in range(start_columns): pos = row_base + c color = picture[pos] # The first cell of a chain has no incoming star. if color == BLACK: return -1 # An isolated chain consists of only this cell. if r >= last_row or c >= last_col: if color == GRAY: answer += 1 continue # d0: minimum cost when the current cell's star does not move. # d1: minimum cost when the current cell's star moves. if color == WHITE: d0, d1 = 0, inf else: # GRAY d0 = d1 = 1 nr = r + dr nc = c + dc pos += step while nr < n and nc < n: color = picture[pos] if color == WHITE: # The predecessor must not move, and this cell is empty. d1 = inf elif color == GRAY: # If the predecessor did not move, this cell initially # contains a star, which may move or disappear. # If the predecessor moved, this cell is initially empty. candidate = d0 + 1 if candidate < d1: d0 = candidate else: d0 = d1 d1 = candidate else: # BLACK # The predecessor must move, and this cell initially # contains a star, which may move or disappear. d1 += 1 d0 = d1 nr += dr nc += dc pos += step best = d0 if d0 < d1 else d1 if best >= inf: return -1 answer += best return answer def main(): read = sys.stdin.buffer.readline t = int(read()) answers = [] for _ in range(t): n, a, b = map(int, read().split()) rows = [read().strip() for _ in range(n)] answers.append(str(minimum_stars(n, a, b, rows))) sys.stdout.write("\n".join(answers) + "\n") if __name__ == "__main__": main()