Problem:
Problem H: Queen's Case A small country called Maltius was governed by a queen. The queen was known as an oppressive ruler.
People in the country suffered from heavy taxes and forced labor. So some young people decided to form
a revolutionary army and fight against the queen. Now, they besieged the palace and have just rushed into
the entrance. Your task is to write a program to determine whether the queen can escape or will be caught by the army. Here is detailed description.  The palace can be considered as grid squares.  The queen and the army move alternately. The queen moves first.  At each of their turns, they either move to an adjacent cell or stay at the same cell.  Each of them must follow the optimal strategy.  If the queen and the army are at the same cell, the queen will be caught by the army immediately.  If the queen is at any of exit cells alone after the army’s turn, the queen can escape from the army.  There may be cases in which the queen cannot escape but won’t be caught by the army forever,
      under their optimal strategies.  Input The input consists of multiple datasets. Each dataset describes a map of the palace. The first line of the input contains two integers W (1 ≤ W ≤ 30) and H (1 ≤ H ≤ 30), which indicate the width and height of the palace. The following H lines, each of which contains W characters, denote the map of the palace. " Q " indicates the queen, " A " the army," E " an exit," # " a wall and " . " a floor. The map contains exactly one " Q ", exactly one " A " and at least one " E ". You can assume both the queen and the army can reach all the exits. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, output " Queen can escape. ", " Army can catch Queen. " or " Queen can not escape and Army can not catch Queen. " in a line. Sample Input 2 2
QE
EA
3 1
QAE
3 1
AQE
5 5
..E..
.###.
A###Q
.###.
..E..
5 1
A.E.Q
5 5
A....
####.
..E..
.####
....Q
0 0 Output for the Sample Input Queen can not escape and Army can not catch Queen.
Army can catch Queen.
Queen can escape.
Queen can not escape and Army can not catch Queen.
Army can catch Queen.
Army can catch Queen. Hint On the first sample input, the queen can move to exit cells, but either way the queen will be caught at the
next army’s turn. So the optimal strategy for the queen is staying at the same cell. Then the army can
move to exit cells as well, but again either way the army will miss the queen from the other exit. So the
optimal strategy for the army is also staying at the same cell. Thus the queen cannot escape but won’t be
caught.

Solution (Python):
from collections import deque, defaultdict
import sys
readline = sys.stdin.readline
write = sys.stdout.write
dd = ((-1, 0), (0, -1), (1, 0), (0, 1), (0, 0))
def solve():
    W, H = map(int, readline().split())
    if W == H == 0:
        return False
    S = [readline().strip() for i in range(H)]
    px = py = qx = qy = 0
    rs = []
    pp = []
    MP = [[0]*W for i in range(H)]
    for i in range(H):
        for j, c in enumerate(S[i]):
            if c == "Q":
                px = j; py = i
            elif c == "A":
                qx = j; qy = i
            elif c == "E":
                MP[i][j] = 1
                rs.append((j, i))
            elif c == "
                MP[i][j] = 2
            if c != "
                pp.append((j, i))
    mp = {}
    L = 0
    for ax, ay in pp:
        for bx, by in pp:
            mp[ax, ay, bx, by, 0] = L
            L += 1
            mp[ax, ay, bx, by, 1] = L
            L += 1

    que1 = deque()
    ss = [-1]*L
    cc = [0]*L

    que = deque([(px, py, qx, qy, 0)])
    used = [0]*L
    used[mp[px, py, qx, qy, 0]] = 1
    RG = [[] for i in range(L)]
    deg = [0]*L
    while que:
        ax, ay, bx, by, t = key = que.popleft()
        k0 = mp[key]
        if ax == bx and ay == by:
            que1.append(k0)
            ss[k0] = 1
            continue
        if t == 0 and MP[ay][ax] == 1:
            que1.append(k0)
            ss[k0] = 0
            continue
        if t == 1:
            for dx, dy in dd:
                nx = bx + dx; ny = by + dy
                if not 0 <= nx < W or not 0 <= ny < H or MP[ny][nx] == 2:
                    continue
                k1 = mp[ax, ay, nx, ny, t^1]
                deg[k0] += 1
                RG[k1].append(k0)
                if not used[k1]:
                    used[k1] = 1
                    que.append((ax, ay, nx, ny, t^1))
        else:
            for dx, dy in dd:
                nx = ax + dx; ny = ay + dy
                if not 0 <= nx < W or not 0 <= ny < H or MP[ny][nx] == 2:
                    continue
                k1 = mp[nx, ny, bx, by, t^1]
                deg[k0] += 1
                RG[k1].append(k0)
                if not used[k1]:
                    used[k1] = 1
                    que.append((nx, ny, bx, by, t^1))

    while que1:
        v = que1.popleft()
        s = ss[v]; t = v % 2
        if t == 0:
            for w in RG[v]:
                if ss[w] != -1:
                    continue
                if s == 1:
                    que1.append(w)
                    ss[w] = 1
                else:
                    deg[w] -= 1
                    if deg[w] == 0:
                        que1.append(w)
                        ss[w] = 0
        else:
            for w in RG[v]:
                if ss[w] != -1:
                    continue
                if s == 0:
                    que1.append(w)
                    ss[w] = 0
                else:
                    deg[w] -= 1
                    if deg[w] == 0:
                        que1.append(w)
                        ss[w] = 1
    k0 = mp[px, py, qx, qy, 0]
    if ss[k0] == -1:
        write("Queen can not escape and Army can not catch Queen.\n")
    elif ss[k0] == 0:
        write("Queen can escape.\n")
    else:
        write("Army can catch Queen.\n")
    return True
while solve():
    ...