We need to solve: Given an N x N grid (N up to 100) with values S[i][j] (integers between -2500 and 2500). Starting at (1,1) and ending at (N,N), we can move only right or down. The path includes all visited cells, including start and end? The problem states: "Cyael will be judged at all visited cells with a judge." But note: cells [1][1] and [N][N] have no judges, so S[1][1] = S[N][N] = 0. So visited cells include these, but they contribute 0. Path length K: number of cells visited. Since moves are right/down, the path length from (1,1) to (N,N) includes exactly (2N-1) cells? Actually number of steps: we need to move (N-1) rights and (N-1) downs, total moves = 2(N-1). Number of visited cells = moves+1 = 2N-1. That includes both start and end.
Goal: maximize average of S over visited cells = total sum / (2N-1). Since denominator fixed (2N-1) for any path? Wait: is path forced to be exactly from (1,1) to (N,N) using only right/down moves? Yes. So any such path visits exactly 2N-1 cells: you have to make exactly N-1 right moves and N-1 down moves, order can vary, but total steps = 2(N-1), cells visited = 2N-1. So denominator K = 2N-1 fixed. Therefore maximizing average is equivalent to maximizing total sum of S over visited cells, because denominator is constant.
But careful: The problem states: "She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge." Since start and end have no judges (S=0), they still count as visited cells but contribute 0. So total sum = sum over all visited cells except start/end? Actually both contribute 0 anyway. So we just need to maximize sum of S[i][j] over the path (including start/end but they are 0). Since denominator is constant (2N-1), maximizing average is maximizing total sum. So the problem reduces to classic maximum sum path from top-left to bottom-right with moves right/down. That is DP: dp[i][j] = max sum to reach (i,j) from (1,1). Then total sum at (N,N) is dp[N][N] + S[1][1]? Actually we include start cell. Typically dp[1][1] = S[1][1] (which is 0). Then for each cell, dp[i][j] = S[i][j] + max(dp[i-1][j], dp[i][j-1]). Then answer = dp[N][N] / (2N-1). Then if answer negative, output "Bad Judges"? Actually check constraints: if maximum possible average is negative, output Bad Judges. So we compute max average. If it's negative, output that phrase. Otherwise output the average with high precision.
But is denominator always 2N-1? For any path? Yes because you must go from (1,1) to (N,N) with right/down moves, number of steps is fixed: exactly N-1 rights and N-1 downs, so total visited cells = (N-1)+(N-1)+1 = 2N-1. So constant.
But wait: is it possible to skip some cells? No, you must move along grid, visiting each cell you step onto. So path length is fixed.
Thus solution seems trivial: compute maximum path sum (DP) and divide by (2N-1). But is there any twist? Let's read problem statement again carefully. Possibly there is nuance: "Cyael will be judged at all visited cells with a judge." But note: start and end have no judges, so they are visited but not judged? However the problem says: "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So judges only at other cells. And "Cyael will be judged at all visited cells with a judge." So visited cells that have a judge: i.e., all cells except start and end? Actually she visits start and end, but they have no judges, so she is not judged there. So the average is sum over judged cells divided by number of judged cells? Or sum over all visited cells? The problem statement: "if she passes K judges, each being on cell [i1][j1], ... then she wants to maximize (S[i1][j1] + S[i2][j2] + ... + S[iK][jK]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j]." This implies K is the number of judges she passes (i.e., visited cells that have judges). So not including start and end. So the denominator is not fixed; it's the number of cells with judges on her path. Since start and end have no judges, they are not counted. But note also there might be other cells without judges? The problem says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So every other cell has a judge. So the only cells without judges are (1,1) and (N,N). So along any path, she will visit exactly (2N-1) cells, but two of them (start and end) have no judges, so the number of judges she passes is K = (2N-1) - 2 = 2N-3. However, are there any other cells that might have no judges? The problem says "except the cells [1][1] and [N][N]". So yes, only those two. So K is also constant? Actually number of visited judge cells = total visited cells minus 2. Since total visited cells = 2N-1, K = 2N-3. But wait: is it possible that start or end might be counted as judge if she revisits? No, she doesn't revisit. So indeed she will pass exactly (2N-1) cells, among which (1,1) and (N,N) have no judges, so number of judged cells = 2N-3. But is that always true? Could she not visit (1,1) because she starts there? She does start there, but it's visited. The judges are at cells; she passes a judge only if she visits a cell with a judge. So start cell has no judge, so not counted. Similarly end. So K = number of cells with judges visited. Since all other cells have judges, and her path includes exactly (2N-1) distinct cells, the count of judge cells visited = (2N-1) - 2 = 2N-3, provided that (1,1) and (N,N) are the only ones without judges. So K is constant across paths! Therefore the average denominator is constant again. So the problem reduces to maximizing total sum over judge cells visited, which is total sum over visited cells minus 0 (since start/end are 0 anyway). Because S[1][1]=S[N][N]=0, sum over visited cells = sum over judge cells visited (since they both contribute 0). So the total sum is same regardless of including start/end. So denominator can be either total sum/(2N-1) or judge sum/(2N-3). But these two averages are different numbers, but since we are maximizing, we need to compute exactly which average? The problem says she wants to maximize (sum of S over judges she passes) / K, where K is number of judges she passes. So denominator is K = number of judge cells visited = (2N-1) - 2 = 2N-3. So we need to maximize total sum over visited judge cells divided by (2N-3). Since total sum over visited cells equals sum over judge cells (since start/end zeros), but denominator differs: (2N-3) vs (2N-1). So we must compute average = (max sum of path) / (2N-3). Because the max sum of path includes zeros at start/end. But that sum is same as sum of judge values. So we divide by (2N-3). Is that correct? Let's double-check: Suppose N=2. Then grid:
[1,1]=0, [1,2]=a, [2,1]=b, [2,2]=0.
Paths: from (1,1) to (2,2). Only two paths: right then down: visits (1,1),(1,2),(2,2). Judges at (1,2) only? Actually (2,2) has no judge, so judge cells visited = only (1,2). So K=1, sum = a. Average = a/1 = a. Down then right: visits (1,1),(2,1),(2,2). Judge at (2,1) only. K=1, average = b. So maximum average = max(a,b). In example input 1:
2
0 -4
8 0
So a = -4, b = 8. Max average = 8. Output: 8.000000. That matches: they output 8.000000. If we used denominator 2N-1 = 3, sum of path includes zeros: sum = 8 (if path down then right: 0 + 8 + 0 = 8). Average = 8/3 ≈ 2.666, not 8. So they are using denominator = number of judges visited = 1. So denominator is not constant? Wait, for N=2, total visited cells = 2N-1 = 3. Number of judge cells = 3 - 2 = 1. So indeed K=1 constant. So average = total sum / 1 = total sum (since zeros don't affect sum). So both formulas give the same total sum. But denominator difference matters only when we compute average value to output: we must divide by K = number of judges visited = 2N-3? For N=2, 2N-3 = 1, yes. For N>2, K = 2N-3 constant. So the denominator is constant across paths? Let's compute for N=3: total visited cells = 2*3-1=5. Judge cells visited = 5 - 2 = 3? Actually start(1,1) and end(3,3) are no judges, so judges visited = 3. But is that always? In a 3x3 grid, any path from (1,1) to (3,3) with right/down moves will have exactly 5 cells. Those include start and end, leaving 3 other cells, all of which have judges. So K = 3 constant. So indeed for any N, K = 2N-3 is constant? Check N=1: trivial? But N≥2 as per constraints? Actually constraints: 2 ≤ N ≤ 100? It says "202 ≤ N ≤ 100" seems weird: "202 ≤ N ≤ 100" maybe a typo. Actually reading: "1 ≤ T ≤ 202 ≤ N ≤ 100-2500 ≤ S[i][j] ≤ 2500" That is garbled. Usually they mean: 1 ≤ T ≤ 20, 2 ≤ N ≤ 100, -2500 ≤ S[i][j] ≤ 2500. So N≥2. So for N≥2, K = 2N-3 constant? Let's verify: number of steps: you need to move (N-1) right and (N-1) down, total moves = 2(N-1). Number of visited cells = 2N-1. Subtract 2 for start and end (no judges) gives 2N-3. But is it possible that start or end are counted as judges? No, they are explicitly no judges. So yes, any path visits exactly 2N-1 cells, of which exactly two are non-judge, so judge cells visited = 2N-3. However, is it possible that the path includes some other cell that also does not have a judge? The problem says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So all other cells have judges. So indeed only (1,1) and (N,N) lack judges. So K = 2N-3 is constant.
Thus maximizing average = maximizing sum of S over visited judge cells / (2N-3). Since denominator constant, we need to maximize total sum over visited judge cells. But note total sum over visited judge cells = total sum over visited cells because S[1][1]=S[N][N]=0. So we need to compute maximum path sum from (1,1) to (N,N) with moves right/down, and the sum includes all cells visited (including zeros at start and end). Then the average = that max sum / (2N-3). So DP solves it.
But careful: For N=2, 2N-3 = 1, so dividing by 1 yields the sum. That matches example. For N>2, denominator > 0.
Now the problem also says: "if the maximum possible average points Cyael can obtain is negative, output "Bad Judges"." So after computing max average, if it's < 0, output Bad Judges; else output the average with absolute error ≤ 1e-6.
Now confirm with second example:
2
0 -45
-3 0
N=2, possible paths: right then down: sum = -45, average = -45. Down then right: sum = -3, average = -3. Maximum average = -3 (since -3 > -45). But -3 is negative. So output "Bad Judges". Indeed they output "Bad Judges". So that matches.
Thus solution: For each test case, compute max path sum via DP, then compute average = max_sum / (2.0*N - 3.0). If average < 0 (or maybe maximum average negative, including zero? If average == 0, it's not negative, so output 0.000000? But check: if maximum average is 0, then it's not negative, so output 0.000000. That seems plausible.
But wait: Could average be 0 exactly? Possibly if all possible sums are 0 or negative, but max sum = 0. Then average = 0, not negative, so output 0. That should be fine.
Now complexity: T up to 20, N up to 100, DP O(N^2) per test case, trivial.
But is there any nuance about path length being exactly 2N-1 cells? Let's double-check: Path must start at (1,1) and end at (N,N) moving only right or down. Number of steps = (N-1) + (N-1) = 2N-2, visited cells = steps+1 = 2N-1. That's correct. So judge cells visited = 2N-1 minus start and end = 2N-3. Good.
Thus simple DP solves.
But wait: The problem statement says "Cyael will be judged at all visited cells with a judge." That could imply that if she visits a cell with judge, that judge gives her points. But does she get points from judges even if she passes them? That's the sum. So yes.
Now, is there any possibility that the path length varies? Could she choose not to move right/down sometimes? She must keep moving until she reaches (N,N). She cannot skip moves; she must move either right or down at each step. So the path is a simple monotonic path. So length fixed.
Thus problem is trivial: Maximum path sum in grid.
But before finalizing, I want to read the constraints: "1 ≤ T ≤ 202 ≤ N ≤ 100-2500 ≤ S[i][j] ≤ 2500" Might be mis-typed, but typical problems: T up to 20, N up to 100, S in [-2500,2500].
But wait: There's also "Your code will be judged against several input files." Might be there is a twist: Maybe the denominator is not constant because she might not visit all judge cells? But we established it's constant. However, think about N=1? Not considered.
But what about the possibility that the judges at start and end are not present, but the problem states "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So yes.
I want to be absolutely sure: Could there be a path that visits more than 2N-1 cells? No, because monotonic path without revisits.
Thus DP is correct.
Now the tricky part: The average we need is sum over judges visited / number of judges visited. Number of judges visited is constant = (2N-3). So average = total sum / (2N-3). However, note that for N=2, (2N-3) = 1, which yields division by 1, as expected. For N=3, it's 3. So DP sum divided by 3.
But what about N=2? total sum includes zeros? Actually total sum of visited cells = S[1][1] + S[1][2] + S[2][2] if path right then down; that sum = 0 + a + 0 = a. Same for down then right. So total sum equals judge sum. So dividing by 1 gives a.
Now check: For N=2, if both a and b are negative, max sum is the least negative. That average is negative, output Bad Judges.
Now for N=100, denominator = 197. So average might be a rational.
Now DP: Since values can be negative, we must handle negative properly. But DP works.
Implementation details: We can create a 2D dp array of size N+1 x N+1 (1-indexed). Initialize dp[1][1] = 0. Then for i from 1 to N, for j from 1 to N, if i==1 and j==1: continue, else dp[i][j] = S[i][j] + max( (i>1? dp[i-1][j] : -inf), (j>1? dp[i][j-1] : -inf) ). Since S[1][1]=0, S[N][N]=0.
But careful: dp values may be negative, but we need to compute max sum. The max from top/left might be negative if both ways yield negative sums. That's okay.
Edge cases: When i=1, only left neighbor; when j=1, only top neighbor.
So O(N^2) per test.
Then max_sum = dp[N][N].
Then average = max_sum / (2.0 * N - 3.0). But what if N=2, denominator = 1, okay.
But check N=2, 2N-3 = 1. Yes.
Now is there any scenario where the maximum average could be computed differently? For instance, if the path length varies due to possibly including more judges? But as argued, it's fixed.
But wait: Could she possibly not be judged at start and end? She is not, but that's irrelevant.
Thus solution is straightforward.
But before finalizing, read the problem again: "Cyael will be judged at all visited cells with a judge." There's a subtlety: If she visits a cell with a judge, she gets that judge's points. So sum is over those. But could she possibly skip a judge? Only by not visiting that cell. Since each cell she visits has a judge except start/end.
Now is there any possibility that the path length is not exactly 2N-3 judges? Let's enumerate for N=3: Total cells visited = 5. Among them, start (1,1) and end (3,3) are no judges, so judges visited = 3. That is fixed. But consider if there is a path that goes through (1,2), (2,2), (2,3) etc. It always visits exactly 3 interior cells. So yes.
Now is it possible that N=2 yields 1 judge visited, as we saw.
Thus denominator constant.
Thus the problem reduces to classic maximum sum path.
But wait, there's a twist: The problem may be from some contest and maybe it's not that trivial. The presence of "average" and "Bad Judges" might indicate that the path length is not fixed because she could maybe take longer path by moving right/down but also possibly staying in place? No, moves are only right or down. So length is fixed.
Let's double-check constraints: "N × N square matrix", "moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N)." So no staying.
Thus DP.
But wait: Could there be multiple test cases? Yes.
Now is there any possibility that the maximum average might be infinite? No.
So I'll implement DP.
But the solution is too trivial. However, the problem might be more subtle: Because the average is sum over judges divided by number of judges. The number of judges visited might vary because she might choose to not visit some judges? But path must go from start to end, so the number of cells visited (including start/end) is fixed as 2N-1. However, among those, there are exactly (2N-1) cells, but start and end are fixed; the rest are exactly (2N-3) cells, but those could be any of the interior cells. But all interior cells have judges. So number of judge cells visited is always (2N-3). Yes.
But consider if N=2, the interior cells? The cells other than start and end are (1,2) and (2,1) but a path only visits one of them. So number of judge cells visited = 1. That's still constant across paths? Both paths visit exactly one judge cell. So yes.
So denominator constant.
Thus trivial.
But maybe there's a nuance: The problem says "Cyael will be judged at all visited cells with a judge." If there is no judge at a cell, she's not judged there. So the average is sum of points from judges she passed divided by number of such judges. So denominator = number of visited cells that contain a judge. But if the number of visited cells with judges varies? In our reasoning, it's constant because only start and end lack judges. However, are there any other cells lacking judges? Possibly some cells might have no judges? The problem says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So that means every other cell has exactly one judge. So no missing judges. So K = number of visited cells minus 2, but number of visited cells is constant. So K constant.
Thus DP.
Now is there any catch about "negative average" output "Bad Judges"? The problem might want that if the maximum average is less than 0, output Bad Judges. If it's zero or positive, output average.
But check: If max average is exactly 0, output 0.000000. Is that considered negative? No.
Now precision: output with absolute error ≤ 1e-6. So we can output using double and printf("%.6f") or similar. But note that the answer might be rational; we need to compute as double.
But be careful: When N is large, denominator up to 197, sum up to (2N-3)*2500? Actually max sum could be up to (2N-3)*2500 if all judge cells give max 2500. But DP sum can be as high as (2N-3)2500. That fits in integer (max about 1972500 = 492500). So integer is fine.
Now consider negative: min sum = (2N-3)*(-2500) = -492500. So integer range safe.
Thus we can compute dp as integer, then convert to double for division.
Now corner case: N=2 yields denominator 1. So average is integer; we output with decimals anyway.
Now "Bad Judges" printed exactly.
Now, but there's a nuance: The problem might be tricky: The average might be maximized not by maximizing sum, because the number of judges visited is not constant? But we argued it is. However, is it possible that she can choose to not be judged at some cells? For instance, if she passes a cell with a judge, she is judged; but maybe she can avoid being judged by not passing the cell? But to avoid a judge, she would have to avoid that cell, but she must go through a set of cells; the path determines which judges she passes. But the count of judges passed is exactly the number of visited cells minus 2. And that count might depend on path? Wait, the number of visited cells is constant, but the number of visited cells with judges is always visited cells minus 2 because start and end have no judges, but all other visited cells have judges. So if all other visited cells have judges, then count = visited cells - 2, which is constant because visited cells is constant (2N-1). So indeed count constant.
But is it possible that some visited cells (besides start/end) also have no judge? The problem explicitly says only those two. So no.
Thus solved.
However, I'm suspicious that the problem might be about maximizing average, not total sum, and the path length could vary if she can choose to move not only right/down but also possibly "stay"? But no.
Maybe the trick is that the start and end are not judged, but they are still counted as visited cells in the denominator? The problem statement says: "if she passes K judges, each being on cell ... then she wants to maximize (sum)/K". So K is number of judges passed, not number of visited cells. So start/end not included.
Thus denominator = K = number of judges passed. That is constant.
Thus DP for max sum.
But wait: Are there other cells without judges? The problem says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." So all other cells have a judge. So K = number of visited cells - 2. So constant.
But check: In the path, could she visit a cell with a judge multiple times? No, monotonic path doesn't revisit.
Thus DP.
Thus solution straightforward.
Now before coding, consider if there might be an alternative interpretation: The matrix is N x N, but "corridors" might be rows? Actually they say "j-th cell on corridor i, as [i][j]". So corridor i is like row i? And moving "directly in front" meaning increase j (along corridor) or move to next corridor (increase i). So it's essentially grid.
Now final answer: For each test case, compute max sum path from (1,1) to (N,N) via DP, compute average = max_sum / (2*N - 3) as double. If average < 0, output "Bad Judges". Else output average with required precision.
But wait: Example 1: N=2, max sum = 8, average = 8/1 = 8. Output 8.000000, which is not negative. Example 2: max sum = max(-45, -3) = -3, average = -3, negative => Bad Judges.
Thus matches.
Now check N=3 with some values: Suppose grid:
0 5 0
2 3 0
0 0 0? Actually need N=3, S[3][3]=0. Let's test: S[1][1]=0, S[1][2]=5, S[1][3]= -? Actually could be anything but S[3][3]=0. Let's compute max sum path: typical dp. Denominator = 2*3-3=3. So average = sum/3.
Now, if all values negative, max sum could be negative; then average negative => Bad Judges.
Now, what about zero? If max sum = 0, average = 0, not negative => output 0.000000.
Now ensure double division: (double)max_sum / (2*N - 3).
Now, is there any scenario where denominator 2*N-3 could be zero? For N=2, it's 1, not zero. For N=1? Not allowed. So fine.
Now implement.
But before finalizing, consider if there is any possibility that the path length is not fixed because the moves are not forced to be exactly N-1 each? Actually from (1,1) to (N,N) you must increment row N-1 times and column N-1 times, so yes.
Thus done.
But wait: The problem statement includes "Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0." So they give 0 anyway.
Thus DP.
But this seems too trivial for a contest problem? Possibly there is a twist: The average might be maximized by a path that doesn't necessarily maximize total sum because the denominator K might vary if you can choose to not include some judges? However, if all cells besides start/end have judges, then any path will include exactly (2N-2) moves, but the number of judges visited is (2N-1) - 2 = 2N-3? Actually number of visited cells = 2N-1. Start and end are no judge, so judges visited = 2N-3. However, is it possible that some visited cells don't have judges because maybe the grid has cells without judges? The problem statement says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." That implies exactly those two have no judge. So all others have. So indeed.
Now what about the possibility that the path could go through (1,1) and (N,N) which are no judges, so they don't count. So the number of judges visited is exactly the number of steps? Actually steps = number of moves, each move lands on a new cell. Starting at (1,1), each move goes to a new cell. So the number of moves = (2N-2). The number of judges visited equals the number of moves? Because start is not a judge, but after first move you land on a cell that has a judge (unless that cell is (N,N) but that would require N=1, not). So for N>2, you make 2N-2 moves, but you land on 2N-2 cells? Actually you start at (1,1), then after first move you are at new cell, after second move another, ... after final move you land at (N,N). So you visit total cells = 1 (start) + number of moves = 2N-1 cells. The cells visited after each move (excluding start) are 2N-2 cells. Among these, the final cell (N,N) has no judge, so judges visited among those landed cells = (2N-2) - 1 = 2N-3. So judges visited = number of moves minus 1? But anyway constant.
But wait: Is there any possibility that the start cell might have a judge? No.
Thus denominator constant.
Thus maximum average is simply max total sum divided by constant.
Therefore DP solves.
Now is there any possibility of using floating point to avoid division? No.
Now I'll implement.
But maybe the problem expects more: It could be that the path length is not fixed because she can choose to not move sometimes? But description says "moving either to the cell directly in front ... She will keep doing this until she reaches the end point". It implies she must move at each step until end; no staying.
Thus fine.
Given the simplicity, maybe there is trickiness: The grid values can be negative, and maximizing average might sometimes favor paths with fewer judges if possible, but since number of judges is fixed, it's just total sum.
But what if we consider that the average could be maximized by a path that includes fewer judges but yields higher average? Suppose there is a path that visits less than 2N-3 judges? Is that possible? The only way to visit fewer judges is to skip some judges, but you cannot skip cells; you must traverse a contiguous path. However, could you possibly pass through a cell that has no judge? Only start and end have no judges. Could you design a path that visits only those two? That would require going from (1,1) to (N,N) without visiting any other cell, which is impossible if N>1 because you need to move. So no.
Thus K constant.
Therefore solution stands.
Now write code.
Pseudo:
read T
for each test:
read N
read grid S (N x N) as integers
dp = array of size N+1 x N+1, maybe 0-initialized, but we need to handle negative numbers: For cells not reachable? All reachable. But we must initialize dp[1][1] = S[1][1] = 0.
For i from 1 to N:
For j from 1 to N:
if i==1 and j==1: continue
best = -inf
if i>1: best = max(best, dp[i-1][j])
if j>1: best = max(best, dp[i][j-1])
dp[i][j] = best + S[i][j]
max_sum = dp[N][N]
K = 2*N - 3
avg = max_sum / (double) K
if avg < 0:
print("Bad Judges")
else:
print with 6 decimal places, perhaps using printf("%.6f\n", avg)
But careful: The problem says "The answer will be considered correct if it has an absolute error no more than 10^-6." So we can output with higher precision like %.9f. But 6 decimal places is likely enough. But to be safe, use %.10f or cout with setprecision.
Now double check for N=2, K=1, avg = max_sum. For example 1, max_sum = 8, avg = 8.0, not negative.
Now what about the case where max_sum is negative but avg maybe negative? That's Bad Judges.
Now also consider if max_sum is negative but avg is exactly 0? Not possible because denominator positive; if max_sum negative, avg negative.
Now what about max_sum positive but avg negative? Not possible.
Now what about integer division? Use double.
Now implement.
But wait: Are we sure dp[1][1] should be 0? Yes, since S[1][1]=0.
Now we need to handle dp for cells on first row (i=1) and first column (j=1) properly: For (1,j) with j>1: only left neighbor. For (i,1) with i>1: only top neighbor.
Now best = -inf. Since values can be as low as -2500*(2N-3) which for N=100 is around -492500. So we can use -1e9 as -inf.
Now we must consider that S[i][j] can be 0 for many cells; but that's fine.
Now test with given examples.
Now is there any hidden nuance: The problem might be about maximizing average, not sum, but denominator is number of judges visited. If all judges visited is constant, it's sum. So indeed.
But think: For N=100, the number of judges visited is 197 constant? Wait, 2N-3 = 197. So if we maximize sum, we maximize average.
Now is there any possibility that there exists a path that visits fewer than 2N-3 judges because maybe some cells on the path may have no judge if S[i][j] = 0? But S[i][j]=0 doesn't mean no judge; it means judge awards 0 points. The presence of a judge is independent of points. So all cells except start/end have a judge. So even if a judge awards 0, it still counts as a judge. So K includes that cell.
Thus denominator constant.
Now perhaps the problem originally had variable number of judges because some cells may have no judges? But statement says only those two.
Thus DP.
Now confirm with some random small N to sanity-check.
Case N=3:
Grid:
0 10 -5
-2 0 6
4 3 0
We'll compute max sum path.
dp:
(1,1)=0
(1,2)= from (1,1): 0+10=10
(1,3)= from (1,2): 10 + (-5)=5
(2,1)= from (1,1): 0+(-2)=-2
(2,2)= max( (1,2)=10, (2,1)=-2 ) => 10 + 0? Wait S[2][2]=0 => 10
(2,3)= max( (1,3)=5, (2,2)=10 ) => 10 + 6 = 16
(3,1)= from (2,1): -2+4=2
(3,2)= max( (2,2)=10, (3,1)=2 ) => 10+3=13? S[3][2]=3 => 13
(3,3)= max( (2,3)=16, (3,2)=13 ) => 16 + 0 = 16
So max sum = 16. K = 3. avg = 16/3 ≈ 5.33333. Positive.
Now any path that yields sum > 0? yes.
Now if all numbers negative, sum negative, avg negative => Bad Judges.
Now what about scenario where maximum sum is positive but average could be negative? Not possible.
Thus solution.
Now implementation details.
Edge Cases:
Now time complexity O(T*N^2) = 20 * 10000 = 200k, trivial.
Now one more check: The problem statement might have a twist: "Cyael will be judged at all visited cells with a judge." Could it be that she gets judged only if she stops at a cell? But she is moving continuously, perhaps she only gets judged when she steps into a cell? That's typical.
Now, is it possible that she could avoid being judged at a cell by moving through it quickly? No.
Thus DP.
But what about the phrase "she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N])"? So she starts at start cell, then moves.
Now maybe the trick is that she cannot step on cells that have negative judges because she wants to maximize average? But DP handles.
Now I'm concerned about reading input: N lines each with N integers. S[1][1] and S[N][N] are given as 0 but we should still read them. They are guaranteed to be 0.
Now we can store whole grid.
Now DP approach is fine.
Now confirm that we are not missing something: Could there be multiple test cases with T up to 202? The constraint says "1 ≤ T ≤ 202"? Actually it says "1 ≤ T ≤ 202 ≤ N ≤ 100-2500". I think it's a formatting issue. Possibly T up to 20. Many problems have T up to 20. But we can handle T up to 202 as well, it's fine.
Now in code we can just read T as int, loop.
Now final code: In C++.
But careful: The DP must handle negative numbers, and we need to ensure we use large negative sentinel for unreachable cells. Since all cells are reachable, we can simply compute dp in loops with initialization for row 0 and column 0 as -inf.
Simplify: Use a 2D vector of ints dp(N+1, vector(N+1, INT_MIN)). Set dp[0][1] = 0 or dp[1][0] = 0? Alternative: We can initialize dp[1][1] = 0 and for i=1..N, j=1..N, if i==1 && j==1 continue, else dp[i][j] = S[i][j] + max( dp[i-1][j], dp[i][j-1] ). But for i=1, dp[0][j] is not defined; we can treat dp[0][j] as -inf, but then max will ignore. Simpler: Use two loops: first row: dp[1][j] = dp[1][j-1] + S[1][j]; first column: dp[i][1] = dp[i-1][1] + S[i][1]; then inner cells: dp[i][j] = S[i][j] + max(dp[i-1][j], dp[i][j-1]).
But careful: This approach works only if we assume the path must go through these edges. Indeed, for first row, only way is from left; for first column, only from top. So we can compute accordingly.
But since S[1][1]=0, dp[1][1]=0. Then for j=2..N: dp[1][j] = dp[1][j-1] + S[1][j]. For i=2..N: dp[i][1] = dp[i-1][1] + S[i][1]. Then for i=2..N, j=2..N: dp[i][j] = S[i][j] + max(dp[i-1][j], dp[i][j-1]).
This works and avoids negative infinity.
But note: if some S values are negative, dp might become negative. That's fine.
Now dp[N][N] gives max sum.
Now compute avg.
But we must ensure that dp[N][N] includes S[N][N] which is 0, so it's fine.
Now write code.
Test with examples:
Example 1:
N=2, S:
0 -4
8 0
dp[1][1]=0
dp[1][2] = dp[1][1] + (-4) = -4
dp[2][1] = dp[1][1] + 8 = 8
dp[2][2] = S[2][2] (0) + max(dp[1][2]=-4, dp[2][1]=8) = 8
max_sum=8, avg=8/1=8, positive -> output 8.000000.
Example 2:
0 -45
-3 0
dp[1][2] = -45, dp[2][1] = -3, dp[2][2]= max(-45,-3)+0 = -3, avg = -3/1 = -3 -> Bad Judges.
Matches.
Now test with N=3 example above.
Now ensure output format: If positive or zero, output the number with required precision. Probably we can output with 6 decimal places. But to be safe, output with 10 decimal places.
Now also note that average could be negative but we output "Bad Judges". So only when average < 0.
Now check borderline: avg = 0 exactly. Should output 0.000000. That's not negative, so output number.
Now what about if max_sum = 0 and denominator >0, avg = 0. Not negative.
Now final code.
Potential pitfalls: The denominator 2*N-3 could be zero? N=2 yields 1, N=1 not allowed. So safe.
Now also check if DP may overflow? dp values up to about 500k, well within int.
Now time to implement.
But wait, I recall that sometimes problems like this ask to maximize average and the denominator is not constant, leading to binary search on average to find if there exists a path with average >= x, then convert to checking if sum(S - x) >= 0 over path with weight (S - x) and maybe variable length? But here denominator constant so it's unnecessary. However, I'm slightly uneasy: The problem statement mentions "average" prominently, and it's unusual for them to make it a simple DP if denominator is constant. Could it be that the number of judges visited is not constant because she could also move to the next corridor directly in front? That is the moves described: moving to cell directly in front on same corridor (r, c+1) or to the cell in the next corridor directly in front (r+1, c). That's exactly right/down. So still.
Wait: Could it be that she can also move backwards? No.
Thus I'm convinced.
But maybe there is a nuance: The "corridors" might be like rows, and "j-th cell on corridor i" might indicate that each corridor is like a line of cells. But the moves: "moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N)." So yes, only right and down.
Now perhaps the twist: The start and end cells have no judges, so they don't contribute to sum or count. But the path length in terms of judges is constant, but the number of judges visited might be less than 2N-3 if the path passes through a cell without a judge? But we've established only those two. So it's constant.
Thus DP.
Given the constraints, DP is appropriate.
Now one more check: The problem says "Cyael will be judged at all visited cells with a judge." It doesn't say that she gets the points only if she stops there; she gets the points when she visits. So ok.
Thus solution.
Now I'll produce final answer code in C++.
But I'll also consider if there is any alternative interpretation: maybe she can choose to not be judged at a cell by not stopping? But she always stops at each cell? She moves through cells, but presumably she steps into each cell. She "passes" judges.
Thus no.
Now think about the possibility that the denominator is not constant because the number of judges visited is not necessarily all visited cells except start/end, because maybe some visited cells might be the same as start/end? No.
Thus DP.
Now code.
Implementation steps:
- Include iostream, vector, algorithm, cstdio, etc.
- Use std::fixed and std::setprecision.
Pseudo-code:
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while(T--) {
int N;
cin >> N;
vector<vector> S(N+1, vector(N+1));
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
cin >> S[i][j];
vector<vector> dp(N+1, vector(N+1, 0));
dp[1][1] = S[1][1]; // = 0
for (int j = 2; j <= N; j++)
dp[1][j] = dp[1][j-1] + S[1][j];
for (int i = 2; i <= N; i++)
dp[i][1] = dp[i-1][1] + S[i][1];
for (int i = 2; i <= N; i++) {
for (int j = 2; j <= N; j++) {
dp[i][j] = S[i][j] + max(dp[i-1][j], dp[i][j-1]);
}
}
int max_sum = dp[N][N];
double avg = (double)max_sum / (2 * N - 3);
if (avg < 0) {
cout << "Bad Judges\n";
} else {
cout << fixed << setprecision(6) << avg << "\n";
}
}
return 0;
}
But careful: For N=2, 2*N-3 = 1, fine.
Test with example.
Now consider when max_sum may be negative but denominator positive, avg negative, output Bad Judges.
Now check if we need to use long long? max_sum can be up to (2N-3)2500, for N=100 => 1972500=492500, fits in int (which is at least 2^31-1). But if N=100 and values up to 2500, sum max = 197*2500 = 492,500, which fits. However, negative min = -492,500. So int is fine. But just to be safe, we could use long long, but not necessary.
Now confirm that dp uses int.
Now finalize.
But I recall that some problems use binary search on average to handle variable path length, but here it's constant, so it's okay.
Wait, re-read problem: "Cyael will be judged at all visited cells with a judge." It says "with a judge". Could it be that there are judges only at some cells, and at other cells there are none? The problem says "There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N]." That seems clear: exactly one judge per cell except those two. So all other cells have a judge.
Thus constant.
Now perhaps the nuance: The "judges" are people, and there is exactly one judge per cell (except start/end). So each judge can award points. So yes.
Thus final solution.
But before finalizing, think about edge case N=2: 2N-3 = 1, average = sum, which matches example.
Now we have to output "Bad Judges" (with capital B and J and space? The example shows "Bad Judges". So exact string.
Now also note that if max average is negative, we output that phrase, not the number.
Now check: In example 2, output is "Bad Judges" (no trailing spaces). So we output exactly.
Now we should handle when average is -0.0? In floating point, if max_sum = 0, avg = 0.0, not negative. So fine.
Now final code.
But wait, could the path avoid some judges by taking a route that goes through start/end only? For N>2, you cannot go directly from (1,1) to (N,N) without intermediate cells. So you must go through at least one other cell. Actually for N=3, you must go through 3 judge cells? Let's see: path must consist of 2 steps right and 2 steps down? Actually for N=3, you need 2 rights and 2 downs: total moves=4, cells visited=5. Subtract start/end => 3 judges. Yes.
Thus K = 2N-3.
Now, is there any possibility to have path that visits more than 2N-3 judges? Could she visit a cell twice? No. Could she visit extra cells by moving back? Not allowed. So maximum judges visited = total cells visited - 2 = 2N-1 - 2 = 2N-3. Also the minimum judges visited = same, because any path has exactly that many non-start/end cells. So K is fixed. Therefore average denominator fixed.
Thus DP solves.
Now let's think about the problem statement more: "Cyael will be judged at all visited cells with a judge." If a cell has a judge, she gets points. That's sum. But what about if a cell has a judge but she doesn't stop? She still visits. So it's fine.
Now I'm convinced.
I'll write final solution accordingly.
But wait, there is a detail: The input constraints: "202 ≤ N ≤ 100-2500" is weird. Possibly it's "1 ≤ T ≤ 20, 2 ≤ N ≤ 100, -2500 ≤ S[i][j] ≤ 2500". So it's safe.
Now deliver code.
Potential pitfalls: Using int for dp but need to handle negative values when initializing first row/col. In our method, we compute dp[1][j] sequentially, which is fine even if values negative. But if all values are extremely negative, dp may become very negative, but that's fine.
Now check correctness of dp formula: For first row, only left moves possible, so dp[1][j] = dp[1][j-1] + S[1][j]. This yields the sum along that row path, which is the only possible path for those cells. But if there are alternative paths to those cells? For first row, the only way is from left, since you cannot come from above (no row 0). So correct. Similarly for first column.
Thus dp[N][N] is the maximum sum.
Now we must be careful: The DP as described yields the maximum sum for a path that ends at each cell. That's standard.
Now test with a small grid where negative values might cause the DP to choose a less negative path: e.g., N=3:
0 -5 -6
-1 -2 -3
-4 -5 0
dp[1][2] = -5, dp[1][3] = -11
dp[2][1] = -1, dp[2][2] = max(-5, -1) + (-2) = -1 + -2 = -3, dp[2][3] = max(-11, -3) + (-3) = -3 -3 = -6
dp[3][1] = dp[2][1] + (-4) = -1 -4 = -5
dp[3][2] = max(dp[2][2] = -3, dp[3][1] = -5) + (-5) = -3 -5 = -8
dp[3][3] = max(dp[2][3] = -6, dp[3][2] = -8) + 0 = -6
So max_sum = -6. K=3, avg = -2, negative => Bad Judges. That seems plausible.
Now if there is a mix, DP chooses best.
Now confirm that we should output average with high precision. The required absolute error ≤ 1e-6, so we can output with e.g., 10 decimal places.
Now final code.
But before finalizing, I recall that some problems like this are known as "Maximum average path in a grid with fixed path length", and the trick is to use binary search on average because denominator fixed? Actually if denominator fixed, it's max sum. So binary search not needed.
Thus code is ready.
Now let me consider if the problem could be interpreted differently: The path may not need to include all cells, but she can choose to not be judged by not stopping? But she passes through, so she is judged. Alternatively, maybe judges are only at cells where she stops? But she stops at each cell? In a grid traversal, she moves step by step, so she stops at each cell? The description: "She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge." This implies as soon as she visits a cell, the judge there evaluates her. So she gets points.
Thus fine.
Now confirm the start and end: They have no judges, so she is not judged there. So they are not counted. So sum is over visited judge cells, but we include S[1][1] and S[N][N] as 0 anyway, so total sum = sum over judge cells visited. So DP sum is sum over judge cells visited + 0 + 0 = same as sum over judge cells.
Thus denominator is K = number of judge cells visited = total visited cells - 2 = 2N-3. So average = dp[N][N] / (2N-3). This matches example for N=2.
Now note: For N=2, dp[N][N] = max(S[1][2], S[2][1])? Actually dp[2][2] includes max of left and up plus 0, so yes.
Now final answer.
But before moving to final, I'll consider if there is any scenario where dp[N][N] might not equal the sum over judge cells because of start/end? But they are 0, so no.
Now I'm going to write solution.
Now to be thorough, maybe we should consider the possibility of integer division causing rounding issues. avg is double, so fine.
Now, I will produce final answer with explanation.
But wait: The problem statement says "Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance." That's just flavor.
Thus done.
Now I'll generate final ChatGPT answer with solution explanation and code.
But the user said "Help her determine the best path she has to follow in order to maximize her average points." They might want the average value, not the path.
Thus code as above.
Now the final code will be in C++.
Now possible improvements: Use long long to avoid overflow if N up to 1000? But N up to 100, fine.
Now final answer. The problem reduces to finding the maximum sum path from (1,1) to (N,N) moving only right or down.
Since the start and end cells contain no judges (S=0) and every other cell has a judge, the number of judges visited along any such path is constant: (K = (2N-1) - 2 = 2N-3).
Thus maximizing the average (\frac{\text{sum of judge scores}}{K}) is equivalent to maximizing the total sum of scores along the path.
We compute the maximum path sum using simple dynamic programming:
- (dp[1][1] = 0).
- For the first row: (dp[1][j] = dp[1][j-1] + S[1][j]) for (j>1).
- For the first column: (dp[i][1] = dp[i-1][1] + S[i][1]) for (i>1).
- For interior cells: (dp[i][j] = S[i][j] + \max(dp[i-1][j], dp[i][j-1])).
The answer is (dp[N][N] / (2N-3)). If this value is negative we output "Bad Judges", otherwise we output it with sufficient precision.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<vector<int>> S(N+1, vector<int>(N+1));
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
cin >> S[i][j];
vector<vector<int>> dp(N+1, vector<int>(N+1, 0));
dp[1][1] = S[1][1]; // = 0
for (int j = 2; j <= N; ++j)
dp[1][j] = dp[1][j-1] + S[1][j];
for (int i = 2; i <= N; ++i)
dp[i][1] = dp[i-1][1] + S[i][1];
for (int i = 2; i <= N; ++i)
for (int j = 2; j <= N; ++j)
dp[i][j] = S[i][j] + max(dp[i-1][j], dp[i][j-1]);
long long maxSum = dp[N][N];
double avg = (double)maxSum / (2 * N - 3);
if (avg < 0) {
cout << "Bad Judges\n";
} else {
cout << fixed << setprecision(6) << avg << '\n';
}
}
return 0;
}