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 in C++ (judge verdict: Accepted)

#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

const int MAX = 40;
int W,H,cq,ca;
char field[MAX][MAX];
int memo[MAX*MAX][MAX*MAX][2]; // dp[Queen][Army][phase( 0 : Queen, 1 : Army )] := state
int dx[] = {0,0,1,0,-1};
int dy[] = {0,1,0,-1,0};

bool isValid(int x,int y) { return 0 <= x && x < W && 0 <= y && y < H; }

#define REPEAT 0
#define ESCAPE 1
#define CATCH -1

void compute(){

  vector<int> E;

  rep(i,H) rep(j,W) {
    if( field[i][j] == 'Q' ) field[i][j] = '.', cq = j + i * W;
    if( field[i][j] == 'A' ) field[i][j] = '.', ca  = j + i * W;
    if( field[i][j] == 'E' ) E.push_back(j+i*W);
  }

  rep(i,H*W) rep(j,H*W) rep(k,2) memo[i][j][k] = REPEAT;
  
  rep(i,(int)E.size()) rep(j,H*W) if( E[i] != j ) memo[E[i]][j][0] = ESCAPE;

  rep(i,H*W) rep(k,2) if( field[i/W][i%W] != '#' ) memo[i][i][k] = CATCH;

  bool update = true;
  while( update ){
    update = false;
    rep(queen,H*W){
      rep(army,H*W){
	rep(phase,2){
	  if( memo[queen][army][phase] != REPEAT ) continue;
	  if( phase ) { // Army
	    bool escape = true;
	    bool cat = false;
	    REP(i,0,5){
	      int nx = army % W + dx[i], ny = army / W + dy[i];
	      if( !isValid(nx,ny) ) continue;
	      if( field[ny][nx] == '#' ) continue;
	      if( memo[queen][nx+ny*W][!phase] == CATCH  ) { cat = true; }
	      if( memo[queen][nx+ny*W][!phase] != ESCAPE ) { escape = false; }
	    }
	    if( cat    ) memo[queen][army][phase] = CATCH , update = true;
	    if( escape ) memo[queen][army][phase] = ESCAPE, update = true;
	  } else { // Queen
	    bool escape = false;
	    bool cat = true;
	    REP(i,0,5) {
	      int nx = queen % W + dx[i], ny = queen / W + dy[i];
	      if( !isValid(nx,ny) ) continue;
	      if( field[ny][nx] == '#' ) continue;
	      if( memo[nx+ny*W][army][!phase] == ESCAPE ) { escape = true; }
	      if( memo[nx+ny*W][army][!phase] != CATCH  ) { cat = false; }
	    }
	    if( escape ) memo[queen][army][phase] = ESCAPE, update = true;
	    if( cat    ) memo[queen][army][phase] = CATCH , update = true;
	  }
	}
      }
    }
    /*
    cout << endl;
    cout << "------ " << endl;
    rep(i,H*W) rep(j,H*W) {
      cout << endl;
      cout << memo[i][j][0] << " " << memo[i][j][1] << endl;
      rep(y,H) {
	rep(x,W) {
	  if( x + y * W == i && i == j ) cout << "X";
	  else if( x + y * W == i ) cout << "Q";
	  else if( x + y * W == j ) cout << "A";
	  else cout << field[y][x];
	}
	cout << endl;
      }
    }
    */
  }
  if( memo[cq][ca][0] == REPEAT ) puts("Queen can not escape and Army can not catch Queen.");
  else if( memo[cq][ca][0] == ESCAPE ) puts("Queen can escape.");
  else puts("Army can catch Queen.");
}

int main(){
  while( cin >> W >> H, W|H ){
    rep(i,H) rep(j,W) cin >> field[i][j];
    compute();
  }
  return 0;
}