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:
h, w, kk = [int(i) for i in input().split()]
mi = [[10 ** 18] * w for _ in range(h)]

a = []
q = []
for l in range(h):
    s = input()
    b = []
    for j, k in enumerate(s):
        b.append(k != "#")
        if k == "S":
            q.append([l, j, 0])
            mi[l][j] = 0
    a.append(b)

k = 0
while True:
    if k == len(q):
        break
    i, j, c = q[k]
    k += 1
    if c < kk:
        e = j
        for d in range(i - 1, 1 + 2, 2):
            if 0 <= d < h and 0 <= e < w and a[d][e]:
                    if mi[d][e] != kk:
                        mi[d][e] = kk
                        q.append([d, e, c + 1])
                        
                        if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                            print(1)
                            exit()
        d = i
        for e in range(j - 1, j + 2, 2):
            if 0 <= d < h and 0 <= e < w and a[d][e]:
                
                if mi[d][e] !=kk:
                    mi[d][e] = kk
                    q.append([d, e, c + 1])
                    
                    if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                        print(1)
                        exit()
    else:
        break
for i in q:
    i[-1] = kk
k = 0
while True:
    i, j, c = q[k]
    k+=1
    e = j
    for d in range(i-1, 1+2, 2):
        if 0 <= d < h and 0 <= e < w:
            if mi[d][e] > c + 1:
                mi[d][e] = c + 1
                q.append([d, e, c + 1])
                
                if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                    print(c//kk+1)
                    exit()
    d = i
    for e in range(j-1, j+2, 2):
        if 0 <= d < h and 0 <= e < w:
            if mi[d][e] > c + 1:
                mi[d][e] = c + 1
                q.append([d, e, c + 1])
                
                if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                    print(c//kk+1)
                    exit()

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
b=[list("#"*(w+2))]+[list("#"+input()+"#")for _ in range(h)]+[list("#"*(w+2))]
for i in range(h+2):
  for j in range(w+2):
    if b[i][j]=="S":
      Q=[(i,j)]
      ans=0--min(i-1,h-i,j-1,w-j)//k
co=0
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while Q and co!=k:
  co+=1
  P=[]
  for i,j in Q:
    ans=min(ans,0--min(i-1,h-i,j-1,w-j)//k)
    for x,y in zip(dx,dy):
      if b[i+x][j+y]==".":
        b[i+x][j+y]=0
        P.append((i+x,j+y))
        if i+x in [1,h] or j+y in [1,w]:print(1);exit()
  Q=P
print(ans+1)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
b=[list("#"*(w+2))]+[list("#"+input()+"#")for _ in range(h)]+[list("#"*(w+2))]
for i in range(h+2):
  for j in range(w+2):
    if b[i][j]=="S":
      Q=[(i,j)]
      ans=0--min(i-1,h-i,j-1,w-j)//k
co=0
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while Q and co!=k:
  co+=1
  P=[]
  for i,j in Q:
    ans=min(ans,0--0--min(i-1,h-i,j-1,w-j)//k)
    for x,y in zip(dx,dy):
      if b[i+x][j+y]==".":
        b[i+x][j+y]=0
        P.append((i+x,j+y))
        if i+x in [1,h] or j+y in [1,w]:print(1);exit()
  Q=P
print(ans+1)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
b=[list("#"*(w+2))]+[list("#"+input()+"#")for _ in range(h)]+[list("#"*(w+2))]
for i in range(h+2):
  for j in range(w+2):
    if b[i][j]=="S":Q=[(i,j)]
co=0
ans=10**10
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while Q and co!=k:
  co+=1
  P=[]
  for i,j in Q:
    for x,y in zip(dx,dy):
      if b[i+x][j+y]==".":
        b[i+x][j+y]=0
        P.append((i,j))
        m=min(i+x-1,h-i-x,j+y-1,w-j-y)
        ans=min(ans,0--m//k)
      if i+x in [1,h] or j+y in [1,w]:print(1);exit()
  Q=P
print(ans+1)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
b=[list("#"*(w+2))]+[list("#"+input()+"#")for _ in range(h)]+[list("#"*(w+2))]
for i in range(h+2):
  for j in range(w+2):
    if b[i][j]=="S":Q=[(i,j)]
co=0
ans=10**10
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while Q and co!=k:
  co+=1
  P=[]
  for i,j in Q:
    for x,y in zip(dx,dy):
      if b[i+x][j+y]==".":
        b[i+x][j+y]=0
        P.append((i,j))
        m=min(i+x-1,h-i-x,j+y-1,w-j-y)
        ans=min(ans,0--m//k)
      if i+x in [1,h] or j+y in [1,w]:print(1);exit()
  Q=P
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,k=map(int,input().split())
b=[list("#"*(w+2))]+[list("#"+input()+"#")for _ in range(h)]+[list("#"*(w+2))]
for i in range(h+2):
  for j in range(w+2):
    if b[i][j]=="S":Q=[(i,j)]
co=0
ans=10**10
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while Q and co!=k:
  co+=1
  P=[]
  for i,j in Q:
    for x,y in zip(dx,dy):
      if i+x in [1,h] or j+y in [1,w]:print(1);exit()
      if b[i+x][j+y]==".":
        b[i+x][j+y]=0
        P.append((i,j))
        m=min(i+x-1,h-i-x,j+y-1,w-j-y)
        ans=min(ans,0--m//k)
  Q=P
print(ans+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math

#入力
h,w,k=map(int,input().split())
a=[input() for i in range(h)]

#Sの位置の捜索
for i in range(h):
    for j in range(w):
        if a[i][j]=='S':
            sy,sx=i,j

#各種パラメータの初期設定
d=deque([[sy,sx]])
dist=[]
inf=-1
m=1000
for i in range(h):
    dist.append([inf]*w)
dist[sy][sx]=0

#一回目の魔法で移動できる範囲を幅優先探索
while len(d)!=0:
    b=d.popleft()
    p,q=b[0],b[1]
    for i in [[1,0],[0,-1],[-1,0],[0,1]]:
        by,bx=b[0]+i[0],b[1]+i[1]
        if bx>=0 and bx<w and by>=0 and by<h and a[by][bx]!='#' and dist[by][bx]==-1 and dist[p][q]<k:
            #訪問先が配列内かつ壁でなく未訪問、訪問元の距離がk以下のとき訪問
            d.append([by,bx])
            dist[by][bx]=dist[p][q]+1

#一回目で移動できる各地点からの魔法の使用回数を計算
for i in range(h):
    for j in range(w):
        if dist[i][j]!=-1:
            m=min(m,math.ceil(i/k),math.ceil(w-i-1/k),math.ceil(j/k),math.ceil(w-j-1/k))

print(m+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
L = []
min_dist = min(sy, h - sy - 1, sx, w - sx - 1)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            L += [(ny, nx)]
            min_dist = min(min_dist, min(ny, h - 1 - ny, nx, w - 1 - nx))
            if A[ny][nx] < k:
                q += [(ny, nx)]

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
min_dist = min(sy, h - sy - 1, sx, w - 1, sx)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            min_dist = min(min_dist, min(ny, h - 1 - ny, nx, w - 1 - nx))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(-(- min_dist // k) + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
min_dist = min(sy, h - sy - 1, sx, w - 1, sx)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            min_dist = min(min_dist, min(ny, h - 1 - ny, nx, w - 1 - nx))
            if A[ny][nx] <= k:
                q += [(ny, nx)]

print(-(- min_dist // k) + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
min_dist = min(sy, h - sy - 1, sx, w - 1, sx)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            min_dist = min(min_dist, min(y, h - 1 - y, x, w - 1 - x))
            if A[ny][nx] <= k:
                q += [(ny, nx)]

print(-(- min_dist // k) + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
min_dist = min(sy, h - sy - 1, sx, w - 1, sx)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            min_dist = min(min_dist, min(y, h - 1 - y, x, w - 1 - x))
            if A[ny][nx] == k:
                continue
            q += [(ny, nx)]

print(-(- min_dist // k) + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
q = deque()
for a in range(h):
    if 'S' in A[a]:
        sy = a; sx = A[a].index('S')
        q += [(sy, sx)]

A[sy][sx] = 0
min_dist = INF
while q:
    y, x = q.popleft()
    min_dist = min(min_dist, x, y, h - 1 - y, w - 1 - x)
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            if A[ny][nx] == k:
                continue
            q += [(ny, nx)]

print(1-(-min_dist // k))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = min(-(-sy//k), -(-h-sy-1//k), -(-sx//k), -(-w-sx-1//k))
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, -((-y)//k), -((-(h-y-1))//k), -((-y)//k), -((-(w-x-1))//k))
            if A[ny][nx] == k:
                continue
            q += [(ny, nx)]

print(ans + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = min(-(-sy//k), -(-h-sy-1//k), -(-sx//k), -(-w-sx-1//k))
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, -((-y)//k), -((-(h-y-1))//k), -((-y)//k), -((-(w-x-1))//k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = min((sy + k - 1) // k, (h - 1 + sy + k - 1) // k,
      (sx + k - 1) // k, (w - 1 - sx + k - 1) // k)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, -((-y)//k), -((-(h-y-1))//k), -((-y)//k), -((-(w-x-1))//k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = 1 + min((sy + k - 1) // k, (h - 1 + sy + k - 1) // k,
              (sx + k - 1) // k, (w - 1 - sx + k - 1) // k)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, 1 + min((ny - 1 + k - 1) // k, (h - 1 - ny + k - 1) // k,
                               (nx - 1 + k - 1) // k, (w - 1 - nx + k - 1) // k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = INF
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, 1 + min((ny - 1 + k - 1) // k, (h - 1 - ny + k - 1) // k,
                               (nx - 1 + k - 1) // k, (w - 1 - nx + k - 1) // k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = 1 + min((sy + k - 1) // k, (h - 1 + sy + k) // k,
              (sx + k - 1) // k, (w - 1 - sx + k - 1) // k)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, 1 + min((ny - 1 + k - 1) // k, (h - 1 - ny + k - 1) // k,
                               (nx - 1 + k - 1) // k, (w - 1 - nx + k - 1) // k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


h, w, k = LI()
A = SRL(h)
flg = 0
for a in range(h):
    for b in range(w):
        if A[a][b] == 'S':
            sy, sx = a, b
            A[sy][sx] = 0
            flg = 1
            break
    if flg:
        break

q = deque([(sy, sx)])
ans = 1 + min((sy + k - 1) // k, (h - sy + k) // k,
              (sx + k - 1) // k, (w - sx + k) // k)
while q:
    y, x = q.popleft()
    for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx, ny = x + i, y + j
        if 0 <= nx < w and 0 <= ny < h and A[ny][nx] == '.':
            A[ny][nx] = A[y][x] + 1
            ans = min(ans, 1 + min((ny - 1 + k - 1) // k, (h - 1 - ny + k - 1) // k,
                               (nx - 1 + k - 1) // k, (w - 1 - nx + k - 1) // k))
            if A[ny][nx] < k:
                q += [(ny, nx)]

print(ans)