We are given t test cases. For each test case, we have an n x n grid. Exactly two cells contain ''. We need to output a grid with four '' forming a rectangle with sides parallel to axes. That is, we need to add two more '' such that all four '' are at the corners of some axis-aligned rectangle. The given two '*' may be in same row or same column, but they can be any two cells.
We need to find any rectangle (with sides parallel to axes) that has the two given cells as two of its corners. Then we mark the remaining two corners. If the given two cells are already opposite corners of a rectangle? Wait: For a rectangle with sides parallel to axes, the four corners are (x1,y1), (x1,y2), (x2,y1), (x2,y2). So any two cells that share either the same row or same column? Actually if two cells are given, they could be either on same row (then they share x), same column (share y), or neither. However, if they are not in same row and not in same column, they can be opposite corners of a rectangle (if we consider the rectangle defined by their rows and columns). But they could also be adjacent corners? In an axis-aligned rectangle, adjacent corners share either same row or same column. Opposite corners do not share row nor column. So given two cells, there are three possibilities:
-
They are in same row: (r, c1) and (r, c2). Then to form a rectangle, we need to pick another row r' and then the corners would be (r, c1), (r, c2), (r', c1), (r', c2). So we need to pick any r' != r such that the two new cells are not already * (could be anything, we can mark them). Since solution guaranteed exists, we can just pick any r' (e.g., 1 if r!=1 else 2) and mark those. It's always possible because n>=2, and we can always find another row. But what about the condition that the two given cells can be in same row. So solution exists; we can just pick any other row.
-
They are in same column: similarly pick any other column.
-
They are neither same row nor same column: then the two cells are (r1,c1) and (r2,c2). They can serve as opposite corners of a rectangle, with the other corners being (r1,c2) and (r2,c1). That gives four cells. That rectangle's sides are parallel to axes. So we can simply mark those two cells. However, we must ensure that those cells are not already marked? Actually they could be already marked? But there are exactly two '' initially. So (r1,c2) and (r2,c1) are not '' initially (since only two cells are '*'). So marking them is safe. That gives a rectangle. So the solution is straightforward: if they are not in same row and not same column, we add the missing corners. If they are in same row or same column, we need to pick another row/column.
But there is a nuance: The problem says "You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes." That means after marking, the set of four '*' are exactly the corners of some axis-aligned rectangle. It does not require that the two given cells be opposite corners; they could be adjacent. In the same row case, given two cells adjacent in that row, we can complete a rectangle by adding two cells in some other row at those columns. So any other row works.
However, what if the given two cells are already in the same row and also the columns are such that picking a row r' would cause the rectangle to have corners that are the same as given? That is not possible because we are adding two new cells; the given two cells remain. There is no conflict.
But there is a tricky case: what if n=2 and the two cells are in same row? For n=2, rows are 1 and 2. If they are in same row, say row1, then columns 1 and 2. Then the only other row is row2. The rectangle's corners would be (1,1), (1,2), (2,1), (2,2). Marking (2,1) and (2,2) gives four '*' covering whole grid. That's fine. Similarly if they are in same column. So works.
But what about the case when they are in same row but there is no other row? n>=2 so there is always another row.
Thus solution: For each test case:
- Find positions of the two '*': (x1,y1), (x2,y2).
- If x1 == x2: pick another row x = any row != x1 (e.g., if x1 != 1 then pick 1 else 2). Then set (x,y1) and (x,y2) as '*'.
- Else if y1 == y2: pick another column y = any column != y1 (e.g., if y1 != 1 then pick 1 else 2). Then set (x1,y) and (x2,y) as '*'.
- Else (different rows and columns): set (x1,y2) and (x2,y1) as '*'.
Then output grid with original two '' and these two new ones. Since grid initially only has two '', we need to output the final grid with four '*'. Ensure we don't duplicate marking the same cell if it coincidentally is one of the original? In the third case, (x1,y2) and (x2,y1) are distinct from the given ones because given are (x1,y1) and (x2,y2) and if (x1,y2) equals one of them, that would imply y2=y1 or x1=x2? Actually if (x1,y2) equals (x1,y1) then y2=y1, but that case is same column, handled separately. Similarly, (x1,y2) = (x2,y2) would imply x1=x2, handled. So safe.
But wait: what about scenario where they are same row but we pick another row that might coincide with existing row? That's fine: we pick a different row.
Thus simple.
However, the problem statement says "It is guaranteed that the solution exists." So the above method always yields a valid solution. But let's double-check: Could there be an edge case where the two cells are in same row but the only other row is fully occupied? But there is no occupancy constraint; we are adding '*' to empty cells. So any empty cells can be marked. So always works.
Therefore solution is straightforward.
We need to handle multiple test cases, total sum n ≤ 400. So we can read each grid as strings.
Implementation steps:
- Read t.
- For each test case:
- Read n.
- Initialize grid = list of strings (list of list of characters for mutability).
- Also find positions of '*': store in list positions.
- Determine (x1,y1) and (x2,y2). (Indices 0-based or 1-based? Use 0-based for internal.)
- If same row: pick another row r = 0 if x1 != 0 else 1 (ensure within n). But if n=2 and x1=0 then r=1 works. If n>2, we can pick any row not equal to x1; we can pick 0 if x1 !=0 else 1. That works. But need to ensure r < n. Since n >=2, r=0 or 1 is valid. But if x1=0 and n=2, r=1 is fine. If x1=1 and n=2, r=0 is fine.
- Mark grid at (r, y1) and (r, y2) as '*'.
- If same column: pick another col c = 0 if y1 !=0 else 1. Mark (x1,c) and (x2,c) as '*'.
- Else: mark (x1,y2) and (x2,y1) as '*'.
- Print the grid as strings.
But careful: In same row case, after picking another row, we need to mark those cells. But the original grid already has '*' at (x1,y1) and (x1,y2). So we add two more. So final grid will have four.
Check if any of the new cells coincidentally already contain '*'? In same row case, the new cells are at (r, y1) and (r, y2). Could these be the same as the original ones? Only if r = x1, but we are picking r != x1, so no. Could they be the same as each other? That would require y1 = y2 but that would be same column and row -> they'd be the same cell, but that cannot happen because there are two distinct cells. If they are same row and same column, they'd be the same cell. But they are two distinct cells, so y1 != y2. So fine.
In same column case, we pick c != y1, so new cells distinct.
In different rows and columns case, the new cells are distinct and not same as given.
Thus fine.
Example:
Input:
6
4
..*.
....
*...
....
We have two stars at (0,2) and (2,0). Different rows/cols, so mark (0,0) and (2,2). So output:
..
....
..
.... which matches sample output? Sample output for first test case is:
..
....
..
....
Yes.
Second test case:
2
.
.
Positions: (0,0) and (1,1). Different rows and cols -> mark (0,1) and (1,0). So grid becomes:
(0,0):, (0,1): => ""
(1,0):, (1,1): => ""
So output:
**
**
Matches.
Third:
2
.*
.*
Positions: (0,1) and (1,1) same column. Pick another column: column index 0 (since y1=1, pick 0). Mark (0,0) and (1,0). So output:
**
**
Matches sample: they show:
**
**
Yes.
Fourth:
3
.
...
...
Positions: (0,0) and (0,2) same row. Pick another row: row 1 (since row0 used, pick 1). Mark (1,0) and (1,2). So grid becomes:
Row0: .
Row1: . (since originally row1 ... -> becomes * . *)
Row2: ...
But sample output for fourth test case is:
.
.
...
Yes.
Fifth:
5
.....
....
.....
....
.....
Positions: (1,2) and (3,1). Different rows/cols -> mark (1,1) and (3,2). So grid becomes:
Row1: .... -> now .**..? Actually original row1: "...." indices 0..4: 0:.,1:.,2:,3:.,4:. After marking (1,1) becomes '', so row1 becomes "..."? Wait (1,1) is column1 (0-index), so that's second character. So row1 becomes . * * . . => "...". And row3 originally "...." -> (3,2) becomes '' => row3 becomes "..."? Actually original row3: "...." positions: col0='.', col1='', col2='.', col3='.', col4='.'. Mark (3,2) => col2 becomes '', so row3 becomes "..."? That would be . * . * . but we need to check: col2 becomes '', so . * * . .? Wait careful: Row3: indices:0 .,1 ,2 .,3 .,4 . -> after marking index2 -> . * * . .? Actually index2 becomes '', so string: index0 '.', index1 '', index2 '', index3 '.', index4 '.' => "...". But sample output for fifth test case is:
.....
...
.....
...
.....
So row1 becomes "...", row3 becomes "...". That matches our marking if we set (1,1) and (3,2)?? Let's check: row1: we marked (1,1) and original (1,2) -> gives stars at col1 and col2: "..." yes. Row3: we have original (3,1) and marked (3,2): stars at col1 and col2: ".**.." yes. So output matches.
Sixth:
4
....
....
*...
*...
Positions: (2,0) and (3,0) same column. Pick another column: if col0, pick col1 (since col0 used). Mark (2,1) and (3,1). So rows become: row2: * * . . -> "..", row3: "..". Sample output:
....
....
**..
**..
Matches.
So solution works.
Edge case: if same row but there is only one other row, that's fine.
But we need to be careful: When picking another row, ensure it's within range and not equal to x1. Our simple logic: choose r = 0 if x1 != 0 else 1. But if n = 2 and x1 = 1, then r=0 works. If n>2, also works. But what if n=1? n >=2, so fine.
But there is a scenario: if n=2 and x1=0, r=1 works. So all good.
Similarly for column: choose c = 0 if y1 != 0 else 1.
Thus algorithm is simple.
Time complexity: O(total n^2) to read and output, but within constraints.
But note: The sum of n across test cases ≤ 400, so we can even do O(n^2) per case.
Now we need to implement.
We can read each test case grid as list of strings, but to modify, we need mutable structure. We can convert each string to list of chars, then modify, then join.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().splitlines()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
grid = []
stars = []
for i in range(n):
line = next(it).strip()
grid.append(list(line))
for j, ch in enumerate(line):
if ch == '':
stars.append((i, j))
(x1, y1), (x2, y2) = stars
if x1 == x2:
# same row
r = 0 if x1 != 0 else 1
grid[r][y1] = ''
grid[r][y2] = ''
elif y1 == y2:
# same column
c = 0 if y1 != 0 else 1
grid[x1][c] = ''
grid[x2][c] = ''
else:
# different rows and columns
grid[x1][y2] = ''
grid[x2][y1] = '*'
# add each row as string to output
for row in grid:
out_lines.append(''.join(row))
sys.stdout.write('\n'.join(out_lines))
But careful: For same row case, we need to ensure the new row r is within n. Since n >=2, and r is either 0 or 1, it's always < n. But what if n=2 and x1=0, r=1 works; x1=1, r=0 works. So fine.
But one more nuance: In same row case, what if the two given cells are in row 0, and we pick r=1. That's fine. But what if n=3 and row 1 is chosen, that's fine. So no issue.
However, what if the two given cells are in row 0 and row 0 is the only row? But n>=2, so there is at least one other row.
Thus solution is correct.
But is there any situation where our choice would produce a rectangle that doesn't have sides parallel to axes? No, because we construct explicitly.
Now, note: In same row case, we are adding two cells at same row r. But do we guarantee that these new cells are not already marked? Since only two '' exist and they are in row x1, and r != x1, they are different rows, so they are not marked. However, if the grid originally had other characters? Only '.' and '', and exactly two '*'. So new cells are '.'.
Thus fine.
Now, one more check: In same row case, what if the two given cells are in the same row and also the same column? That's impossible because two distinct cells cannot share both row and column.
Thus solution works.
We can also think about alternative solutions: Use any row/col, but we must ensure that the chosen row/col exists. So our simple pick is fine.
Now, we should also ensure that the sum of n is ≤ 400, so reading all lines is fine.
Now, test with the provided sample.
We'll run mentally:
Test case 1: n=4, stars (0,2) and (2,0) diff -> mark (0,0) and (2,2) => row0: . . * . becomes * . * .? Wait row0 originally "..." => indices:0 .,1 .,2 ,3 . . Mark (0,0) => * at index0 => " . * ."? Actually index0='', index1='.', index2='', index3='.' => ".." yes. Row2 originally "..." => index0='', index1='.', index2='.', index3='.' -> after mark (2,2) => index2='' => "* . * ." => ".."? Actually becomes ".."? That would be ".." but original had "..." => after mark: index0 '', index1 '.', index2 '', index3 '.' => ".." yes. So output rows: "..", "....", ".*.", "....". matches.
All good.
Thus solution accepted.
Now, consider potential pitfalls: The input may have spaces? No, lines contain only '.' and '*'.
We need to be careful about reading from sys.stdin.read().splitlines() because there may be empty lines? Typically not.
Alternatively, we can use sys.stdin.readline.
But the approach is fine.
Now, some might think about cases where the two stars are already forming a rectangle? Actually they are only two, we need to add two. So always we will add two more.
Now, I'll produce final answer with explanation.
But the user only wants "the answer", which likely means the code? The instruction says: "You are an AI... return the final answer." It expects the solution output? Actually they probably want the code solution. In this environment, we usually output the Python code.
Thus we'll output the code with explanation? Typically we respond with the solution description and the code.
Given the conversation history, they said: "Write a solution in python." So we need to provide Python code.
Thus final answer: Provide Python code implementing solve() as described.
But note: The problem may have multiple test cases, and we need to print each test case's grid. Use fast I/O.
Also, we might need to handle cases where the grid is read with trailing spaces. Use strip.
I'll include a if name == "main": solve().
Thus final answer: the Python code.
Let's produce final solution. ```python
import sys
def solve() -> None:
data = sys.stdin.read().splitlines()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
grid = []
stars = []
for i in range(n):
row = list(next(it).strip())
grid.append(row)
for j, ch in enumerate(row):
if ch == '*':
stars.append((i, j))
(r1, c1), (r2, c2) = stars
if r1 == r2: # same row
nr = 0 if r1 != 0 else 1 # any other row
grid[nr][c1] = '*'
grid[nr][c2] = '*'
elif c1 == c2: # same column
nc = 0 if c1 != 0 else 1 # any other column
grid[r1][nc] = '*'
grid[r2][nc] = '*'
else: # opposite corners
grid[r1][c2] = '*'
grid[r2][c1] = '*'
for row in grid:
out.append(''.join(row))
sys.stdout.write('\n'.join(out))
if name == "main":
solve()