Problem:


Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , .  or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2



Sample input:
3 3 3
#.#
#S.
###


Sample output:
1

Submission status: Wrong Answer

Solution code:
import math
H,W,K=map(int,input().split())

A=[]
S=[0,0]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
    A.append(a)

#print(A)
#print(S)
import sys
input=sys.stdin.readline
B=[[1000 for j in range(W)]for i in range(H)]

B[S[0]][S[1]]=0
ans=min(S[0],S[1],W-1-S[1],H-1-S[0])
#print(ans)
Q=[]
for i in range(4):
    if S[0]>0 and A[S[0]-1][S[1]]==".":
        Q.append([B[S[0]][S[1]]+1,S[0]-1,S[1]])
        B[S[0]-1][S[1]]=B[S[0]][S[1]]+1
        if ans>min(S[0]-1,S[1],H-1-(S[0]-1),W-1-S[1]):
            ans=min(S[0]-1,S[1],H-1-(S[0]-1),W-1-S[1])
    if S[1]>0 and A[S[0]][S[1]-1]==".":
        Q.append([B[S[0]][S[1]]+1,S[0],S[1]-1])
        B[S[0]][S[1]-1]=B[S[0]][S[1]]+1
        if ans>min(S[0],S[1]-1,H-1-S[0],W-1-(S[1]-1)):
            ans=min(S[0],S[1]-1,H-1-S[0],W-1-(S[1]-1))
    if S[0]<H-1 and A[S[0]+1][S[1]]==".":
        Q.append([B[S[0]][S[1]]+1,S[0]+1,S[1]])
        B[S[0]+1][S[1]]=B[S[0]][S[1]]+1
        if ans>min(S[0]+1,S[1],H-1-(S[0]+1),W-1-S[1]):
            ans=min(S[0]+1,S[1],H-1-(S[0]+1),W-1-S[1])
    if S[1]<W-1 and A[S[0]][S[1]+1]==".":
        Q.append([B[S[0]][S[1]]+1,S[0],S[1]+1])
        B[S[0]][S[1]+1]=B[S[0]][S[1]]+1
        if ans>min(S[0],S[1]+1,H-1-S[0],W-1-(S[1]+1)):
            ans=min(S[0],S[1]+1,H-1-S[0],W-1-(S[1]+1))

for i in range(10**6):
    if i==len(Q):
        break
    else:
        for j in range(4):
            if Q[i][1]>0 and B[Q[i][1]-1][Q[i][2]]==1000 and B[Q[i][1]][Q[i][2]]<K and A[Q[i][1]-1][Q[i][2]]==".":
                Q.append([Q[i][0]+1,Q[i][1]-1,Q[i][2]])
                B[Q[i][1]-1][Q[i][2]]=B[Q[i][1]][Q[i][2]]+1
                if ans>min(Q[i][1]-1,Q[i][2],H-1-(Q[i][1]-1),W-1-(Q[i][2])):
                    ans=min(Q[i][1]-1,Q[i][2],H-1-(Q[i][1]-1),W-1-(Q[i][2]))
            if Q[i][2]>0 and B[Q[i][1]][Q[i][2]-1]==1000 and B[Q[i][1]][Q[i][2]]<K and B[Q[i][1]][Q[i][2]-1]==".":
                Q.append([Q[i][0]+1,Q[i][1],Q[i][2]-1])
                B[Q[i][1]][Q[i][2]-1]=B[Q[i][1]][Q[i][2]]+1
                if ans>min(Q[i][1],Q[i][2]-1,H-1-(Q[i][1]),W-1-(Q[i][2]-1)):
                    ans=min(Q[i][1],Q[i][2]-1,H-1-(Q[i][1]),W-1-(Q[i][2]-1))
            if Q[i][1]<H-1 and B[Q[i][1]+1][Q[i][2]]==1000 and B[Q[i][1]][Q[i][2]]<K and B[Q[i][1]+1][Q[i][2]]==".":
                Q.append([Q[i][0]+1,Q[i][1]+1,Q[i][2]])
                B[Q[i][1]+1][Q[i][2]]=B[Q[i][1]][Q[i][2]]+1
                if ans>min(Q[i][1]+1,Q[i][2],H-1-(Q[i][1]+1),W-1-(Q[i][2])):
                    ans=min(Q[i][1]+1,Q[i][2],H-1-(Q[i][1]+1),W-1-(Q[i][2]))
            if Q[i][2]<W-1 and B[Q[i][1]][Q[i][2]+1]==1000 and B[Q[i][1]][Q[i][2]]<K and B[Q[i][1]][Q[i][2]+1]==".":
                Q.append([Q[i][0]+1,Q[i][1],Q[i][2]+1])
                B[Q[i][1]][Q[i][2]+1]=B[Q[i][1]][Q[i][2]]+1
                if ans>min(Q[i][1],Q[i][2]+1,H-1-(Q[i][1]),W-1-(Q[i][2]+1)):
                    ans=min(Q[i][1],Q[i][2]+1,H-1-(Q[i][1]),W-1-(Q[i][2]+1))
#print(B)
#print(ans)
print(math.ceil(ans/K)+1)

Submission status: Wrong Answer

Solution code:
H, W, K = map(int, input().split())
items = []

start = []
diff = float("inf")

for i in range(H):
    items.append(list(input()))
    for j in range(W):
        s = items[i][j]
        if s == "S":
            start = [i, j]
            diff = min(diff, min(i, H - 1 - i, min(j, W - 1 - j)))
            p = [i, j]

for i in range(H):
    for j in range(W):
        if abs(i - start[0]) + abs(j - start[1]) <= K and items[i][j] != "#":
            diff = min(diff, min(i, H - 1 - i, min(j, W - 1 - j)))

ans = 1
if diff == 0:
    print(ans)
else:
    print(ans + (K - 1 + diff) // K)

Submission status: Wrong Answer

Solution code:
H, W, K = map(int, input().split())
items = []

start = []
diff = float("inf")
p = []
prev = [[False] * W for _ in range(H)]
for i in range(H):
    items.append(list(input()))
    for j in range(W):
        s = items[i][j]
        if s == "S":
            start = [i, j]
            diff = min(diff, min(i, H - 1 - i, min(j, W - 1 - j)))
            p = [i, j]
            prev[i][j] = True

from collections import deque

que = deque([])

dx = [-1, 0, 0, 1]
dy = [0, 1, -1, 0]

for i in range(4):
    x = dx[i]
    y = dy[i]
    if 0 <= start[1] + y <= H - 1 and 0 <= start[0] + x <= W - 1:
        tmp = items[start[1] + y][start[0] + x]
        if tmp == ".":
            que.append((start[1] + y, start[0] + x))
k = K
while len(que) != 0 and k != 0:
    que2 = deque([])
    while len(que) != 0:
        q = que.popleft()
        prev[q[0]][q[1]] = True
        if min(diff, min(q[0], H - 1 - q[0], min(q[1], W - 1 - q[1]))) < diff:
            diff = min(diff, min(q[0], H - 1 - q[0], min(q[1], W - 1 - q[1])))
            p = (q[0], q[1])

        for i in range(4):
            x = dx[i]
            y = dy[i]
            if 0 <= q[0] + y <= H - 1 and 0 <= q[1] + x <= W - 1:
                if prev[q[0] + y][q[1] + x]:
                    continue
                tmp = items[q[0] + y][q[1] + x]
                if tmp == ".":
                    que2.append((q[0] + y, q[1] + x))
    k -= 1
    que = que2


ans = 1
if diff == 0:
    print(ans)
else:
    print(ans + (K - 1 + diff) // K)

Submission status: Wrong Answer

Solution code:
import heapq
import sys
import math
input=sys.stdin.readline
H,W,K=map(int,input().split())

s=[0,0]
A=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            s=[i,j]
    A.append(a)
#print(A)
#print(s)
B=[[10**6 for j in range(W)]for i in range(H)]
#print(B)
B[s[0]][s[1]]=0

import math
MIN=10**6

Q=[]
for i in range(4):
    if s[0]+1>=H:
        print(1)
        exit()
    elif A[s[0]+1][s[1]]==".":
        Q.append([1,s[0]+1,s[1]])
    if s[0]-1<0:
        print(1)
        exit()
    elif A[s[0]-1][s[1]]==".":
        Q.append([1,s[0]-1,s[1]])
    if s[1]+1>=W:
        print(1)
        exit()
    elif A[s[0]][s[1]+1]==".":
        Q.append([1,s[0],s[1]+1])
    if s[1]-1<0:
        print(1)
        exit()
    elif A[s[0]][s[1]-1]==".":
        Q.append([1,s[0],s[1]-1])


heapq.heapify(Q)
for i in range(10**6):
    if len(Q)==0:
        break
    else:
        if B[Q[0][1]][Q[0][2]]==10**6:
            B[Q[0][1]][Q[0][2]]=Q[0][0]
            q=Q[0]
            U=math.ceil(q[1]/K)
            L=math.ceil(q[2]/K)
            D=math.ceil((H-1-q[1])/K)
            R=math.ceil((W-1-q[2])/K)
            if min(U,L,D,R)+1<MIN:
                MIN=min(U,L,D,R)+1
                
            heapq.heappop(Q)
            for j in range(4):
                if q[1]+1<=H-1 and K>=q[0]:
                    if A[q[1]+1][q[2]]==".":
                        heapq.heappush(Q,[q[0]+1,q[1]+1,q[2]])
                if q[1]-1>=0 and K>=q[0]:
                    if A[q[1]-1][q[2]]==".":
                        heapq.heappush(Q,[q[0]+1,q[1]-1,q[2]])
                if q[2]+1<=W-1 and K>=q[0]:
                    if A[q[1]][q[2]+1]==".":
                        heapq.heappush(Q,[q[0]+1,q[1],q[2]+1])
                if q[2]-1>=0 and K>=q[0]:
                    if A[q[1]][q[2]-1]==".":
                        heapq.heappush(Q,[q[0]+1,q[1],q[2]-1])
    heapq.heappop(Q)
#print(A)
#print(B)


print(MIN)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import heapq as hq
H,W,K = map(int,input().split())
A = []
X,Y=0,1
start = None
for h in range(H):
    tmp = list(input())
    A.append(tmp)
    if "S" in tmp:
        start = (tmp.index("S"), h)

def minCost(x,y):
    return min(start[X], W-1-start[X], start[Y], H-1-start[Y]) // K
search = []
searched = [[False for w in range(W)] for h in range(H)]
search.append((start[X],start[Y], 0))
vector = [(1,0),(-1,0),(0,1),(0,-1)]
max_x,min_x,max_y,min_y = 0,W-1,0,H-1
while len(search) > 0:
    x,y,cnt = search.pop()
    if x == 0 or x == W-1 or y == 0 or y == H-1:
        print(1)
        exit()
    if searched[y][x]:
        continue
    searched[y][x] = True
    max_x = max(max_x, x)
    min_x = min(min_x, x)
    max_y = max(max_y, y)
    min_y = min(min_y, y)
    for v in vector:
        newX,newY = x+v[X], y+v[Y]
        if not searched[newY][newX] and cnt < K and A[newY][newX] != "#":
            search.append((newX,newY,cnt+1))

dist = min(min_x, min_y, W-1-max_x, H-1-max_y)
print(1 + (dist+K-1)//K)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import heapq as hq
H,W,K = map(int,input().split())
A = []
X,Y=0,1
start = None
for h in range(H):
    tmp = list(input())
    A.append(tmp)
    if "S" in tmp:
        start = (tmp.index("S"), h)

def minCost(x,y):
    return min(start[X], W-1-start[X], start[Y], H-1-start[Y]) // K
search = []
searched = [[False for w in range(W)] for h in range(H)]
hq.heapify(search)
hq.heappush(search, (minCost(start[X],start[Y]),0,0,0,start))
vector = [(1,0),(-1,0),(0,1),(0,-1)]
while len(search) > 0:
    _, cost, wall, walk, pos = hq.heappop(search)
    x,y = pos[X], pos[Y]
    if searched[y][x]:
        continue
    if x == 0 or x == W-1 or y == 0 or y == H-1:
        print(cost)
        break
    searched[y][x] = True
    for v in vector:
        new_x,new_y = x+v[X], y+v[Y]
        if not searched[new_y][new_x]:
            new_cost, new_wall, new_walk = cost, wall, walk
            if A[new_y][new_x] == "#":
                new_wall += 1
                if new_wall > 0:
                    new_cost += 1
                    new_wall = -K+1
            new_walk += 1
            if new_walk > 0:
                new_cost += 1
                new_walk = -K+1
            hq.heappush(search, (new_cost + minCost(new_x, new_y), new_cost, new_wall, new_walk, (new_x, new_y)))

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    H, W, K = map(int, input().split())
    A = [input() for _ in range(H)]
    s = None
    for i in range(H):
        for j in range(W):
            if A[i][j] == "S":
                s = (i, j)
    q = deque()
    q.append((s[0], s[1], K))
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    visited = set()
    dst = float("inf")
    while(len(q)):
        i, j, m = q.popleft()
        dst = min([i, j, H-1-i, W-1-j])
        if m == 0:
            continue
        for d in directions:
            ni = i + d[0]
            nj = j + d[1]
            if 0 <= ni < H and 0 <= nj < W:
                if A[ni][nj] == "." and (ni, nj) not in visited:
                    q.append((ni, nj, m-1))
                    visited.add((ni, nj))
    ans = 1 - (-dst//K)
    print(ans)

        


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
h, w, k = [int(item) for item in input().split()]
field = ["O" * (w+2)]
for i in range(h):
    line = "O" + input().rstrip() + "O"
    field.append(line)
    if "S" in line:
        start = [i+1, line.index("S"), 0]
field.append("O" * (w+2))

stack = [start]
visited = [[0] * (w+2) for _ in range(h+2)]
visited[start[0]][start[1]] = 1
closest = 10**3 
while stack:
    x, y, d = stack.pop()
    closest = min(closest, min(x-1, y-1, h-x, w-y))
    if closest == 0:
        break
    d += 1
    for dx, dy in [[1,0], [0,1], [-1,0], [0,-1]]:
        if field[x+dx][y+dy] == "." and visited[x+dx][y+dy] == 0 and d <= k:
            stack.append([x+dx, y+dy, d])
            visited[x+dx][y+dy] = 1
print(1 + int(math.ceil(closest / k)))

Submission status: Wrong Answer

Solution code:
import math
h, w, k = [int(item) for item in input().split()]
field = ["O" * (w+2)]
for i in range(h):
    line = "O" + input().rstrip() + "O"
    field.append(line)
    if "S" in line:
        start = [i+1, line.index("S")]
field.append("O" * (w+2))

stack = [start]
visited = [[0] * (w+2) for _ in range(h+2)]
visited[start[0]][start[1]] = 1
closest = 10**3 
while stack:
    x, y = stack.pop()
    closest = min(closest, min(x-1, y-1, h-x, w-y))
    for dx, dy in [[1,0], [0,1], [-1,0], [0,-1]]:
        if field[x+dx][y+dy] == "." and visited[x+dx][y+dy] == 0:
            stack.append([x+dx, y+dy])
            visited[x+dx][y+dy] = 1
print(1 + int(math.ceil(closest / k)))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [input() for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        A[i] = A[i][:j] + '.' + A[i][j + 1:]
        break
visited = [[False] * W for _ in range(H)]
ans = min([i, H - 1 - i, j, W - 1 - j])
Q = collections.deque([[i, j, K]])
while Q:
    h, w, res = Q.popleft()
    if not visited[h][w] and A[i][j] == '.':
        visited[h][w] = True
        ans = min([ans, h, H - 1 - h, w, W - 1 - w])
        if res:
            res -= 1
            if h:
                Q += [[h - 1, w, res]]
            if h < H - 1:
                Q += [[h + 1, w, res]]
            if w:
                Q += [[h, w - 1, res]]
            if w < W - 1:
                Q += [[h, w + 1, res]]
print(1 + math.ceil(ans / K))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [input() for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    if A[h][w] == 'S':
        break
visited = [[False] * W for _ in range(H)]
ans = min([h, H - 1 - h, w, W - 1 - w])
res = K
Q = collections.deque([[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                       [h, w + 1, res]])
while Q:
    h, w, res = Q.popleft()
    if 0 <= h < H - 1 and 0 <= w < W - 1 and A[h][
            w] == '.' and not visited[h][w]:
        visited[h][w] = True
        ans = min([ans, h, H - 1 - h, w, W - 1 - w])
        if res:
            res -= 1
            Q += [[h - 1, w, res]]
            Q += [[h + 1, w, res]]
            Q += [[h, w - 1, res]]
            Q += [[h, w + 1, res]]
print(1 + math.ceil(ans / K))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [input() for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    if A[h][w] == 'S':
        break
visited = [[False] * W for _ in range(H)]
ans = min([h, H - 1 - h, w, W - 1 - w])
res = K
Q = collections.deque([[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                       [h, w + 1, res]])
while Q:
    h, w, res = Q.popleft()
    if 0 <= h < H - 1 and 0 <= w < W - 1 and A[h][
            w] == '.' and not visited[h][w]:
        visited[h][w] = True
        ans = min([ans, h, H - 1 - h, w, W - 1 - w])
        if res:
            res -= 1
            Q += [[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                  [h, w + 1, res]]
print(1 + math.ceil(ans / K))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [input() for _ in range(H)]
for h in range(H):
    if 'S' in A[h]:
        w = A[h].index('S')
        break
visited = [[False] * W for _ in range(H)]
ans = min([h, H - 1 - h, w, W - 1 - w])
res = K
Q = collections.deque([[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                       [h, w + 1, res]])
while Q:
    h, w, res = Q.popleft()
    if 0 <= h < H and 0 <= w < W and A[h][w] == '.' and not visited[h][w]:
        visited[h][w] = True
        ans = min([ans, h, H - 1 - h, w, W - 1 - w])
        if res:
            res -= 1
            Q += [[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                  [h, w + 1, res]]
print(1 + math.ceil(ans / K))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [input() for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if 'S' in A[i]:
        A[i].index('S')
visited = [[False] * W for _ in range(H)]
ans = min([i, H - 1 - i, j, W - 1 - j])
Q = collections.deque([[i, j, K]])
while Q:
    h, w, res = Q.popleft()
    if 0 <= h < H and 0 <= w < W and A[i][j] == '.' and not visited[i][j]:
        visited[h][w] = True
        ans = min([ans, h, H - 1 - h, w, W - 1 - w])
        if res:
            res -= 1
            Q += [[h - 1, w, res], [h + 1, w, res], [h, w - 1, res],
                  [h, w + 1, res]]
print(1 + math.ceil(ans / K))

Submission status: Wrong Answer

Solution code:
import itertools, math, collections, sys
input = sys.stdin.readline
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
Q = collections.deque([[i, j, 0]])
aH = aW = []
while Q:
    h, w, dist = Q.popleft()
    A[h][w] = '#'
    aH += [h]
    aW += [w]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                Q += [[nh, nw, dist + 1]]
print(1 + min([
    math.ceil(_ / K)
    for _ in (min(aH), H - 1 - max(aH), min(aW), W - 1 - max(aW))
]))

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [[2 if _ == 'S' else 1 if _ == '.' else 0 for _ in input()]
     for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == '2':
        break
stack = [[i, j, 0]]
sH = set()
sW = set()
while stack:
    h, w, dist = stack.pop()
    A[h][w] = 0
    sH.add(h)
    sW.add(w)
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw]:
                stack += [[nh, nw, dist + 1]]
print(1 + min([
    math.ceil(_ / K)
    for _ in (min(sH), H - 1 - max(sH), min(sW), W - 1 - max(sW))
]))

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
sH = set()
sW = set()
while stack:
    h, w, dist = stack.pop()
    A[h][w] = '#'
    sH.add(h)
    sW.add(w)
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
print(1 + min([
    math.ceil(_ / K)
    for _ in (min(sH), H - 1 - max(sH), min(sW), W - 1 - max(sW))
]))

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
aH = aW = []
while stack:
    h, w, dist = stack.pop()
    A[h][w] = '#'
    aH += [h]
    aW += [w]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
print(1 + min([
    math.ceil(_ / K)
    for _ in (min(aH), H - 1 - max(aH), min(aW), W - 1 - max(aW))
]))

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
cand = []
while stack:
    h, w, dist = stack.pop()
    A[h][w] = '#'
    cand += [[h, w]]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
ans = float('inf')
for hw in cand:
    h, w = hw
    ans = min([
        ans,
        math.ceil(h / K),
        math.ceil((H - 1 - h) / K),
        math.ceil(w / K),
        math.ceil((W - 1 - w) / K)
    ])
print(ans + 1)

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
cand = []
while stack:
    h, w, dist = stack.pop()
    print(h, w, dist)
    A[h][w] = '#'
    cand += [[h, w]]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
ans = float('inf')
for hw in cand:
    h, w = hw
    ans = min([
        ans,
        math.ceil(h / K),
        math.ceil((H - 1 - h) / K),
        math.ceil(w / K),
        math.ceil((W - 1 - w) / K)
    ])
print(ans + 1)

Submission status: Wrong Answer

Solution code:
import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
cand = []
while stack:
    h, w, dist = stack.pop()
    A[h][w] = '#'
    cand += [[h, w]]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
ans = float('inf')
for hw in cand:
    h, w = hw
    ans = min([
        ans,
        math.ceil(h / K),
        math.ceil((H - h) / K),
        math.ceil(w / K),
        math.ceil(W - w / K)
    ])
print(ans + 1)

Submission status: Wrong Answer

Solution code:
H,W,K=map(int,input().split())
B=[]
S=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
            a[j]="."
    B.append(a)

A=[[10**5 for i in range(W)]for j in range(H)]

A[S[0]][S[1]]=0  
#print(A)
X=[1,-1,0,0]
Y=[0,0,1,-1]
Q=[[S[0],S[1]]]
for a in range(10**5):
    if len(Q)==0:
        break
    else:
        if (Q[0][0]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]-1][Q[0][1]] and B[Q[0][0]-1][Q[0][1]]==".":
                A[Q[0][0]-1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]-1,Q[0][1]])
        if (Q[0][0]+1)<=(H-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]+1][Q[0][1]] and B[Q[0][0]+1][Q[0][1]]==".":
                A[Q[0][0]+1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]+1,Q[0][1]])
        if (Q[0][1]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]-1] and B[Q[0][0]][Q[0][1]-1]==".":
                A[Q[0][0]][Q[0][1]-1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]-1])
        if (Q[0][1]+1)<=(W-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]+1] and B[Q[0][0]][Q[0][1]+1]==".":
                A[Q[0][0]][Q[0][1]+1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]+1])
    del Q[0]
                
#print(B)
#print(A)
#print(P)
ANS=10**5
for i in range(H):
    for j in range(W):
        if A[i][j]<=K:
            if ANS>min(i,j,(H-1-i),(W-1-j)):
                ANS=min(i,j,(H-1-i),(W-1-j))
                #print(ANS)
ans=ANS
#print(ANS)

if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)

Submission status: Wrong Answer

Solution code:
H,W,K=map(int,input().split())
B=[]
S=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
            a[j]="."
    B.append(a)

A=[[10**5 for i in range(W)]for j in range(H)]

A[S[0]][S[1]]=0  
#print(A)
X=[1,-1,0,0]
Y=[0,0,1,-1]
Q=[[S[0],S[1]]]
for a in range(10**5):
    if len(Q)==0:
        break
    else:
        if (Q[0][0]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]-1][Q[0][1]] and B[Q[0][0]-1][Q[0][1]]==".":
                A[Q[0][0]-1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]-1,Q[0][1]])
        if (Q[0][0]+1)<=(H-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]+1][Q[0][1]] and B[Q[0][0]+1][Q[0][1]]==".":
                A[Q[0][0]+1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]+1,Q[0][1]])
        if (Q[0][1]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]-1] and B[Q[0][0]][Q[0][1]-1]==".":
                A[Q[0][0]][Q[0][1]-1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]-1])
        if (Q[0][1]+1)<=(W-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]-1] and B[Q[0][0]][Q[0][1]+1]==".":
                A[Q[0][0]][Q[0][1]+1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]+1])
    del Q[0]
                
#print(B)
#print(A)
#print(P)
ANS=10**5
for i in range(H):
    for j in range(W):
        if A[i][j]<=K:
            if ANS>min(i,j,(H-1-i),(W-1-j)):
                ANS=min(i,j,(H-1-i),(W-1-j))
ans=ANS
#print(ANS)

if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)

Submission status: Wrong Answer

Solution code:
import copy
H,W,K=map(int,input().split())
B=[]
S=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
            a[j]="."
    B.append(a)
#print(S)    

#print(B)
for i in range(H):
    for j in range(W):
        if (abs(S[0]-i)+abs(S[1]-j))<=K and B[i][j]==".":
            B[i][j]=1
        else:
            B[i][j]=2
#print(B)
P=[i for i in range(H*W)]
size=[1 for i in range(H*W)]

def find(x):
    if x==P[x]:
        return x
    else:
        return find(P[x])

def union(y,z):
    Y=find(y)
    Z=find(z)
    if size[Y]>=size[Z]:
        P[Z]=P[Y]
        size[Y]+=size[Z]
    else:
        P[Y]=P[Z]
        size[Z]+=size[Y]
        
for i in range(H):
    for j in range(W):
        if B[i][j]==1:
            if i-1>=0:
                if B[i-1][j]==1:
                    if find(i*W+j)!=find((i-1)*W+j):
                        union(i*W+j,(i-1)*W+j)
            if i+1<=(H-1):
                if B[i+1][j]==1:
                    if find(i*W+j)!=find((i+1)*W+j):
                        union(i*W+j,(i+1)*W+j)
            if j-1>=0:
                if B[i][j-1]==1:
                    if find(i*W+j)!=find((i)*W+(j-1)):
                        union(i*W+j,(i)*W+(j-1))
            if j+1<=(W-1):
                if B[i][j+1]==1:
                    if find(i*W+j)!=find((i)*W+(j+1)):
                        union(i*W+j,(i)*W+(j+1))
            
p=P[S[0]*W+S[1]]
#print(p)
#print(P)
ANS=[10**5]
for i in range(H):
    for j in range(W):
        if P[i*W+j]==p:
            ANS.append(min(i,j,(H-1-i),(W-1-j)))
ans=min(ANS)
#print(ANS)
if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)

Submission status: Wrong Answer

Solution code:
import copy
H,W,K=map(int,input().split())
B=[]
S=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
            a[j]="."
    B.append(a)
#print(S)    

#print(B)
for i in range(H):
    for j in range(W):
        if (abs(S[0]-i)+abs(S[1]-j))<=K and B[i][j]==".":
            B[i][j]=1
        else:
            B[i][j]=2
#print(B)
P=[i for i in range(H*W)]
size=[1 for i in range(H*W)]

def find(x):
    if x==P[x]:
        return x
    else:
        return find(P[x])

def union(y,z):
    Y=find(y)
    Z=find(z)
    if size[Y]>=size[Z]:
        P[Z]=P[Y]
        size[Y]+=size[Z]
    else:
        P[Y]=P[Z]
        size[Z]+=size[Y]
        
for i in range(H):
    for j in range(W):
        if B[i][j]==1:
            if i-1>=0:
                if B[i-1][j]==1:
                    if find(i*W+j)!=find((i-1)*W+j):
                        union(i*W+j,(i-1)*W-j)
            if i+1<=(H-1):
                if B[i+1][j]==1:
                    if find(i*W+j)!=find((i+1)*W+j):
                        union(i*W+j,(i+1)*W+j)
            if j-1>=0:
                if B[i][j-1]==1:
                    if find(i*W+j)!=find((i)*W+(j-1)):
                        union(i*W+j,(i)*W+(j-1))
            if j+1<=(W-1):
                if B[i][j+1]==1:
                    if find(i*W+j)!=find((i)*W+(j+1)):
                        union(i*W+j,(i)*W+(j+1))
            
p=P[S[0]*W+S[1]]
#print(p)
#print(P)
ANS=[10**5]
for i in range(H):
    for j in range(W):
        if P[i*W+j]==p:
            ANS.append(min(i,j,(H-1-i),(W-1-j)))
ans=min(ANS)
#print(ans)
if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)