Problem:
Problem H: Slippy Floors

The princess of the Fancy Kingdom has been loved by many people for her lovely face. However the
witch of the Snow World has been jealous of the princess being loved. For her jealousy, the witch has
shut the princess into the Ice Tower built by the witch’s extreme magical power.

As the Ice Tower is made of cubic ice blocks that stick hardly, the tower can be regarded to be composed
of levels each of which is represented by a 2D grid map. The only way for the princess to escape from the
tower is to reach downward stairs on every level. However, many magical traps set by the witch obstruct
her moving ways. In addition, because of being made of ice, the floor is so slippy that movement of the
princess is very restricted. To be precise, the princess can only press on one adjacent wall to move against
the wall as the reaction. She is only allowed to move in the vertical or horizontal direction, not in the
diagonal direction. Moreover, she is forced to keep moving without changing the direction until she is
prevented from further moving by the wall, or she reaches a stairway or a magical trap. She must avoid
the traps by any means because her being caught by any of these traps implies her immediate death by
its magic. These restriction on her movement makes her escape from the tower impossible, so she would
be frozen to death without any help.

You are a servant of the witch of the Snow World, but unlike the witch you love the princess as many other
people do. So you decided to help her with her escape. However, if you would go help her directly in the
Ice Tower, the witch would notice your act to cause your immediate death along with her. Considering
this matter and your ability, all you can do for her is to generate snowmans at arbitrary places (grid
cells) neither occupied by the traps nor specified as the cell where the princess is initially placed. These
snowmans are equivalent to walls, thus the princess may utilize them for her escape. On the other hand,
your magical power is limited, so you should generate the least number of snowmans needed for her
escape.

You are required to write a program that solves placement of snowmans for given a map on each level,
such that the number of generated snowmans are minimized.

Input

The first line of the input contains a single integer that represents the number of levels in the Ice Tower.
Each level is given by a line containing two integers NY (3 ≤ NY ≤ 30) and NX (3 ≤ NX ≤ 30) that
indicate the vertical and horizontal sizes of the grid map respectively, and following NY lines that specify
the grid map. Each of the NY lines contain NX characters, where ‘A’ represents the initial place of the
princess, ‘>’ the downward stairs, ‘.’ a place outside the tower, ‘_’ an ordinary floor, ‘#’ a wall, and ‘^’ a
magical trap. Every map has exactly one ‘A’ and one ‘>’.

You may assume that there is no way of the princess moving to places outside the tower or beyond the
maps, and every case can be solved with not more than ten snowmans. The judge also guarantees that
this problem can be solved without extraordinary optimizations.

Output

For each level, print on a line the least number of snowmans required to be generated for letting the
princess get to the downward stairs.

Sample Input

3
10 30
......#############...........
....##_____________#..........
...#________________####......
..#________####_________#.....
.#________#....##________#....
#_________#......#________#...
#__________###...#_____^^_#...
.#__________A_#...#____^^_#...
..#___________#...#_>__###....
...###########.....####.......
7 17
......#..........
.....#_##........
.#...#_A_#.......
#^####___#.......
#_______#........
#>_____#.........
########.........
6 5
#####
#_A_#
#___#
#_>_#
#___#
#####

Output for the Sample Input

2
1
0

Solution in C++ (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
using namespace std;

const int MAXH = 31;
const int MAXW = 31;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

int H, W;
char G[MAXH][MAXW];

int ans;

void show(int x, int y) {
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      if(i == y && j == x) cout << '*';
      else cout << G[i][j];
    }
    cout << endl;
  }
}

void dfs(int x, int y, int c) {
  if(c >= ans) return;
  if(G[y][x] == '$') return;

  G[y][x] = '$';
  for(int i = 0; i < 4; ++i) {
    const int rx = x + dx[(i+2)%4];
    const int ry = y + dy[(i+2)%4];
    bool wall = false;
    if(G[ry][rx] == '^') continue;
    if(G[ry][rx] == '$') continue;
    if(G[ry][rx] == '_') {
      wall = true;
    }
    if(wall) G[ry][rx] = '#';
    int nc = c + wall;
    int nx = x;
    int ny = y;
    while(1) {
      int px = nx;
      int py = ny;
      nx += dx[i];
      ny += dy[i];
      if(G[py][px] == '>') {
	ans = min(ans, nc);
	break;
      }
      if(G[py][px] == '^') break;
      if(G[py][px] == '$') continue;
      if(G[py][px] == '#') break;

      if(G[ny][nx] == '^') break;
      if(G[ny][nx] == '$') continue;
      if(G[ny][nx] == '#') {
	dfs(px,py,nc);
	break;
      }
      if(G[ny][nx] == '_') {
	G[ny][nx] = '#';
	dfs(px,py,nc+1);
	G[ny][nx] = '_';
      }
    }
    if(wall) G[ry][rx] = '_';
  }
  G[y][x] = '_';
}


int main() {
  int Tc;
  cin >> Tc;
  for(int tc = 0; tc < Tc; ++tc) {
    cin >> H >> W;
    int x, y;
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
	cin >> G[i][j];
	if(G[i][j] == 'A') {
	  G[i][j] = '_';
	  x = j;
	  y = i;
	}
      }
    }
    ans = 11;
    dfs(x, y, 0);
    cout << ans << endl;
  }
  return 0;
}