Almost Rectangle (Codeforces 1512B)
Tags: Implement, implementation

There is a square field of size $$$n \times n$$$ in which two cells are marked. These cells can be in the same row or column.You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.For example, if $$$n=4$$$ and a rectangular field looks like this (there are asterisks in the marked cells):$$$$$$ \begin{matrix} . & . & * & . \\ . & . & . & . \\ * & . & . & . \\ . & . & . & . \\ \end{matrix} $$$$$$Then you can mark two more cells as follows$$$$$$ \begin{matrix} * & . & * & . \\ . & . & . & . \\ * & . & * & . \\ . & . & . & . \\ \end{matrix} $$$$$$If there are several possible solutions, then print any of them.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 400$$$). Then $$$t$$$ test cases follow.The first row of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 400$$$) — the number of rows and columns in the table.The following $$$n$$$ lines each contain $$$n$$$ characters '.' or '*' denoting empty and marked cells, respectively.It is guaranteed that the sums of $$$n$$$ for all test cases do not exceed $$$400$$$.It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column.It is guaranteed that the solution exists.

Output: For each test case, output $$$n$$$ rows of $$$n$$$ characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.


Sample Inputs:
6
4
..*.
....
*...
....
2
*.
.*
2
.*
.*
3
*.*
...
...
5
.....
..*..
.....
.*...
.....
4
....
....
*...
*...

Sample Outputs:
*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..

Solution (Python):
def test():
    n = int(input())
    a = []
    f = None
    found = 0
    c = 0
    while c < n:
        s = input()
        if found != 2:
            if '*' in s:
                if found == 0:
                    tmp = s.count('*')
                    if tmp == 1:
                        found = 1
                        f = [s.index('*'), c]
                    else:
                        found = 2
                        if c == n - 1:
                            a[-1] = s
                        else:
                            a.append(s)
                else:
                    if f[0] == s.index('*'):
                        f[0] = [0, 1][f[0] == 0]
                    tmp = [*s]
                    tmp[f[0]] = '*'
                    s = ''.join(tmp)
                    a[f[1]] = s
        a.append(s)
        c += 1
    for x in range(n):
        print(a[x])
t = int(input())
for _ in range(t):
    test()

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l = []
    flag = False
    lst = set()
    list_i = []
    for _ in range(n):
        s = input()
        lst.add(s.find('*'))
        lst.add(s.rfind('*'))
        if s.count('*') == 2:
            list_i.append(s)
            flag = True
        l.append(s)
    if -1 in lst:
        lst.remove(-1)
    lst = list(lst)
    if len(lst) == 1:
        if lst[0] == 0:
            lst.append(1)
        else:
            lst.append(0)
    m = 2
    if flag:
        if l[0].count('*') > 0:
            m = 0
        elif l[-1].count('*') > 0:
            m = 1
        if m == 0:
            print(l[0])
            print(l[0])
            for i in l[2:]:
                print(i)
        elif m == 1:
            for i in l[0:-2]:
                print(i)
            print(l[-1])
            print(l[-1])
        else:
            print(list_i[0])
            for i in l[1:]:
                print(i)
    else:
        for i in l:
            if i.count('*') > 0:
                for j in range(len(i)):
                    if j in lst:
                        print('*', end='')
                    else:
                        print('.', end='')
                print()
            else:
                print(i)

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    pos_r = []
    pos_c = []
    rows = []
    for i in range(n):
        rows.append(list(input()))
        for (j, c) in enumerate(rows[-1]):
            if c == '*':
                pos_r.append(i)
                pos_c.append(j)
    if pos_r[0] == pos_r[1]:
        r = (pos_r[0] + 1) % n
        for c in pos_c:
            rows[r][c] = '*'
    elif pos_c[0] == pos_c[1]:
        c = (pos_c[0] + 1) % n
        for r in pos_r:
            rows[r][c] = '*'
    else:
        for (r, c) in zip(pos_r[::-1], pos_c):
            rows[r][c] = '*'
    rows = '\n'.join((''.join(row) for row in rows))
    print(rows)