Problem:


A Light Road

There is an evil creature in a square on N-by-M grid (2 \leq N, M \leq 100),
and you want to kill it using a laser generator located in a different square.
Since the location and direction of the laser generator are fixed,
you may need to use several mirrors to reflect laser beams.
There are some obstacles on the grid and you have a limited  number of mirrors.
Please find out whether it is possible to kill the creature,
and if possible, find the minimum number of mirrors.

There are two types of single-sided mirrors;
type P mirrors can be placed at the angle of 45 or 225 degrees from east-west direction,
and type Q mirrors can be placed with at the angle of 135 or 315 degrees.
For example, four mirrors are located properly, laser go through like the following. 

Note that mirrors are single-sided,
and thus back side (the side with cross in the above picture) is not reflective.
You have A type P mirrors, and also A type Q mirrors (0 \leq A \leq 10).
Although you cannot put mirrors onto the squares with the creature or the laser generator,
laser beam can pass through the square.
Evil creature is killed if the laser reaches the square  it is  in.

Input

Each test case consists of several lines.

The first line contains three integers, N, M, and A.
Each of the following N lines contains M characters,
and represents the grid information.
'#', '.', 'S', 'G' indicates obstacle, empty square,
the location of the laser generator, and the location of the evil creature, respectively.
The first line shows the information in northernmost squares
and the last line shows the information in southernmost squares.
 You can assume that there is exactly one laser generator and exactly one creature,
and the laser generator emits laser beam always toward the south.

Output

Output the minimum number of mirrors used if you can kill creature, or -1 otherwise.

Sample Input 1

3 3 2
S#.
...
.#G

Output for the Sample Input 1

2

Sample Input 2

3 3 1
S#.
...
.#G

Output for the Sample Input 2

-1

Sample Input 3

3 3 1
S#G
...
.#.

Output for the Sample Input 3

2

Sample Input 4

4 3 2
S..
...
..#
.#G

Output for the Sample Input 4

-1



Sample input:
3 3 2
S#.
...
.#G


Sample output:
2

Submission status: Accepted

Solution code:
from collections import deque
N, M, A = map(int, input().split())
MP = [[0]*M for i in range(N)]
sx = sy = gx = gy = -1
for i in range(N):
    for j, c in enumerate(input()):
        if c == '#':
            MP[i][j] = 1
        elif c == 'S':
            sx = j; sy = i
        elif c == 'G':
            gx = j; gy = i
U = [[set() for i in range(M)] for j in range(N)]
DD = ((-1, 0), (0, -1), (1, 0), (0, 1))
que = deque([(sx, sy, 0, 0, 3)])
for i in range(A+1):
    for j in range(A+1):
        U[sy][sx].add((i, j, 1))
        U[sy][sx].add((i, j, 3))
pp = ((sx, sy), (gx, gy))
while que:
    x, y, a, b, d = que.popleft()

    if (x, y) not in pp:
        d0 = (d-1) % 4
        dx, dy = DD[d0]
        nx = x + dx; ny = y + dy
        if 0 <= nx < M and 0 <= ny < N and MP[ny][nx] == 0:
            if d % 2 == 0:
                if b < A and (a, b+1, d0) not in U[ny][nx]:
                    U[ny][nx].add((a, b+1, d0))
                    que.append((nx, ny, a, b+1, d0))
            else:
                if a < A and (a+1, b, d0) not in U[ny][nx]:
                    U[ny][nx].add((a+1, b, d0))
                    que.append((nx, ny, a+1, b, d0))

        d1 = (d+1) % 4
        dx, dy = DD[d1]
        nx = x + dx; ny = y + dy
        if 0 <= nx < M and 0 <= ny < N and MP[ny][nx] == 0:
            if d % 2 == 0:
                if a < A and (a+1, b, d1) not in U[ny][nx]:
                    U[ny][nx].add((a+1, b, d1))
                    que.append((nx, ny, a+1, b, d1))
            else:
                if b < A and (a, b+1, d1) not in U[ny][nx]:
                    U[ny][nx].add((a, b+1, d1))
                    que.append((nx, ny, a, b+1, d1))

    dx, dy = DD[d]
    nx = x + dx; ny = y + dy
    if 0 <= nx < M and 0 <= ny < N and MP[ny][nx] == 0:
        if (a, b, d) not in U[ny][nx]:
            U[ny][nx].add((a, b, d))
            que.append((nx, ny, a, b, d))

if U[gy][gx]:
    print(min(a+b for a, b, d in U[gy][gx]))
else:
    print(-1)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(n,m,l):
        a = [[c for c in S()] for _ in range(n)]
        s = (-1,-1)
        g = (-1,-1)
        kh = set()
        for i in range(n):
            for j in range(m):
                c = a[i][j]
                if c == 'S':
                    s = (i,j)
                    a[i][j] = '.'
                if c == 'G':
                    g = (i,j)
                    a[i][j] = '.'
                if a[i][j] == '.':
                    kh.add((i,j))

        if (s[0]+1, s[1]) not in kh:
            return -1

        def search():
            d = collections.defaultdict(lambda: (inf,0,0))
            ss = ((s[0]+1, s[1]), 2)
            d[ss] = (0,0,0)
            q = []
            heapq.heappush(q, ((0,0,0), ss))
            v = collections.defaultdict(bool)
            while len(q):
                k, u = heapq.heappop(q)
                if v[u]:
                    continue
                v[u] = True

                if u[0] == g:
                    return k[0]

                if u == (s, 0) or (k[0]>0 and u == (s,2)):
                    continue

                for di in range(4):
                    if abs(di - u[1]) == 2:
                        continue
                    if (u[0][0]+dd[di][0], u[0][1]+dd[di][1]) not in kh:
                        continue
                    uv = ((u[0][0]+dd[di][0], u[0][1]+dd[di][1]), di)
                    if v[uv]:
                        continue
                    if di == u[1]:
                        if d[uv] > k:
                            d[uv] = k
                            heapq.heappush(q, (k, uv))
                        continue

                    if u[0] == s:
                        continue

                    ti = (0,0)
                    if u[1] % 2 == 1:
                        if u[1] - 1 == di:
                            ti = (1,0)
                        else:
                            ti = (0,1)
                    else:
                        if u[1] - 1 == di:
                            ti = (0,1)
                        else:
                            ti = (1,0)

                    ud = (k[0]+1,k[1]+ti[0],k[2]+ti[1])
                    if ud[1] > l or ud[2] > l or d[uv] <= ud:
                        continue
                    d[uv] = ud
                    heapq.heappush(q, (ud, uv))

            return -1

        return search()

    while 1:
        n,m,l = LI()
        if n == 0:
            break
        rr.append(f(n,m,l))
        break

    return '\n'.join(map(str, rr))


print(main())