Ticks (Codeforces 1579C)
Tags: Greedy, implementation

Casimir has a rectangular piece of paper with a checkered field of size $$$n \times m$$$. Initially, all cells of the field are white.Let us denote the cell with coordinates $$$i$$$ vertically and $$$j$$$ horizontally by $$$(i, j)$$$. The upper left cell will be referred to as $$$(1, 1)$$$ and the lower right cell as $$$(n, m)$$$.Casimir draws ticks of different sizes on the field. A tick of size $$$d$$$ ($$$d > 0$$$) with its center in cell $$$(i, j)$$$ is drawn as follows:   First, the center cell $$$(i, j)$$$ is painted black.  Then exactly $$$d$$$ cells on the top-left diagonally to the center and exactly $$$d$$$ cells on the top-right diagonally to the center are also painted black.  That is all the cells with coordinates $$$(i - h, j \pm h)$$$ for all $$$h$$$ between $$$0$$$ and $$$d$$$ are painted. In particular, a tick consists of $$$2d + 1$$$ black cells. An already painted cell will remain black if painted again. Below you can find an example of the $$$4 \times 9$$$ box, with two ticks of sizes $$$2$$$ and $$$3$$$.  You are given a description of a checkered field of size $$$n \times m$$$. Casimir claims that this field came about after he drew some (possibly $$$0$$$) ticks on it. The ticks could be of different sizes, but the size of each tick is at least $$$k$$$ (that is, $$$d \ge k$$$ for all the ticks).Determine whether this field can indeed be obtained by drawing some (possibly none) ticks of sizes $$$d \ge k$$$ or not.

Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number test cases.The following lines contain the descriptions of the test cases. The first line of the test case description contains the integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le k \le n \le 10$$$; $$$1 \le m \le 19$$$) — the field size and the minimum size of the ticks that Casimir drew. The following $$$n$$$ lines describe the field: each line consists of $$$m$$$ characters either being '.' if the corresponding cell is not yet painted or '*' otherwise.

Output: Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if the given field can be obtained by drawing ticks of at least the given size and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).

Note: The first sample test case consists of two asterisks neither of which can be independent ticks since ticks of size $$$0$$$ don't exist.The second sample test case is already described in the statement (check the picture in the statement). This field can be obtained by drawing ticks of sizes $$$2$$$ and $$$3$$$, as shown in the figure.The field in the third sample test case corresponds to three ticks of size $$$1$$$. Their center cells are marked with $$$\color{blue}{\text{blue}}$$$, $$$\color{red}{\text{red}}$$$ and $$$\color{green}{\text{green}}$$$ colors: *.*.*$$$\color{blue}{\textbf{*}}$$$**.$$$\color{green}{\textbf{*}}\color{red}{\textbf{*}}$$$.....The field in the fourth sample test case could have been obtained by drawing two ticks of sizes $$$1$$$ and $$$2$$$. Their vertices are marked below with $$$\color{blue}{\text{blue}}$$$ and $$$\color{red}{\text{red}}$$$ colors respectively: .....*...*.*.*...$$$\color{red}{\textbf{*}}$$$.*...$$$\color{blue}{\textbf{*}}$$$.The field in the fifth sample test case can not be obtained because $$$k = 2$$$, and the last asterisk in the fourth row from the top with coordinates $$$(4, 5)$$$ can only be a part of a tick of size $$$1$$$.The field in the sixth sample test case can not be obtained because the top left asterisk $$$(1, 1)$$$ can't be an independent tick, since the sizes of the ticks must be positive, and cannot be part of a tick with the center cell in the last row, since it is separated from it by a gap (a point, '.') in $$$(2, 2)$$$.In the seventh sample test case, similarly, the field can not be obtained by the described process because the asterisks with coordinates $$$(1, 2)$$$ (second cell in the first row), $$$(3, 1)$$$ and $$$(3, 3)$$$ (leftmost and rightmost cells in the bottom) can not be parts of any ticks.

Sample Inputs:
8
2 3 1
*.*
...
4 9 2
*.*.*...*
.*.*...*.
..*.*.*..
.....*...
4 4 1
*.*.
****
.**.
....
5 5 1
.....
*...*
.*.*.
..*.*
...*.
5 5 2
.....
*...*
.*.*.
..*.*
...*.
4 7 1
*.....*
.....*.
..*.*..
...*...
3 3 1
***
***
***
3 5 1
*...*
.***.
.**..

Sample Outputs:
NO
YES
YES
YES
NO
NO
NO
NO

Solution (Python):
import sys, io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def ticks(n, m, k, matrix):
    stars = {}
    for y in range(n):
        for x in range(m):
            if matrix[y][x] == '*':
                stars[y * m + x] = False
    for x_y in stars:
        x = x_y % m
        y = x_y // m
        stars_ = [x_y]
        k_ = 0
        while True:
            k_ += 1
            if y - k_ < 0:
                break
            if x - k_ < 0:
                break
            if x + k_ >= m:
                break
            if matrix[y - k_][x - k_] == '*' and matrix[y - k_][x + k_] == '*':
                stars_ += [x - k_ + (y - k_) * m]
                stars_ += [x + k_ + (y - k_) * m]
            else:
                break
        k_ -= 1
        if k_ >= k:
            for x_y in stars_:
                stars[x_y] = True
    if all(stars.values()):
        return 'YES'
    return 'NO'
for _ in range(int(input())):
    (n, m, k) = map(int, input().split())
    matrix = [None] * m
    for row in range(n):
        matrix[row] = input().decode('ASCII')
    sys.stdout.write(f'{ticks(n, m, k, matrix)}\n')

Solution (Python):
t = int(input())
for _ in range(t):
    (n, m, k) = map(int, input().split())
    status = [[0] * m for _ in range(n)]
    for i in range(n):
        s = input()
        for j in range(m):
            if s[j] == '*':
                status[i][j] = 1
    for i in range(n - 1, -1, -1):
        for j in range(m):
            if status[i][j] == 0:
                continue
            len = 0
            while j > len and j + len + 1 < m and (i > len):
                if status[i - len - 1][j - len - 1] == 0 or status[i - len - 1][j + len + 1] == 0:
                    break
                len += 1
            if len >= k:
                for d in range(len + 1):
                    status[i - d][j - d] = 2
                    status[i - d][j + d] = 2
    ok = True
    for i in range(n):
        for j in range(m):
            if status[i][j] == 1:
                ok = False
    print('YES' if ok else 'NO')

Solution (Python):
I = input
for _ in [0] * int(I()):
    (n, m, k) = map(int, I().split())
    (*a,) = ''.join((I() for _ in [0] * n))
    n *= m
    f = 0
    while n:
        n -= 1
        l = 0
        j = n
        while l <= min(n % m, m - n % m - 1, n // m) and a[j - l] < '.' > a[j + l]:
            l += 1
            j -= m
        if l > k:
            while l:
                l -= 1
                j += m
                a[j - l] = a[j + l] = '!'
        f |= a[n] == '*'
    print('YNEOS'[f::2])