一个不难的A*。。。比赛的时候没敢写。。晕。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 505
#define maxm 300005
#define eps 1e-10
#define mod 10000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
//head
char g[maxn][maxn];
int vis[maxn][maxn][5];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct node
{
int x, y, t, step;
bool operator < (const node& a) const {
return t > a.t;
}
}tmp, now;
priority_queue<node> q;
int n, si, sj, ti, tj, ans;
void init(void)
{
memset(g, '#', sizeof g);
memset(vis, 0, sizeof vis);
while(!q.empty()) q.pop();
}
void read(void)
{
scanf("%d", &n);
getchar();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
g[i][j] = getchar();
if(g[i][j] == 'M') si = i, sj = j, g[i][j] = '.';
if(g[i][j] == 'T') ti = i, tj = j, g[i][j] = '.';
}
getchar();
}
}
bool check(int x, int y, int t)
{
if(g[x][y] != '.') return true;
t = t % 4;
if(g[x+1][y] == 'N' && t == 0) return true;
if(g[x+1][y] == 'W' && t == 1) return true;
if(g[x+1][y] == 'S' && t == 2) return true;
if(g[x+1][y] == 'E' && t == 3) return true;
if(g[x-1][y] == 'N' && t == 2) return true;
if(g[x-1][y] == 'W' && t == 3) return true;
if(g[x-1][y] == 'S' && t == 0) return true;
if(g[x-1][y] == 'E' && t == 1) return true;
if(g[x][y+1] == 'N' && t == 3) return true;
if(g[x][y+1] == 'W' && t == 0) return true;
if(g[x][y+1] == 'S' && t == 1) return true;
if(g[x][y+1] == 'E' && t == 2) return true;
if(g[x][y-1] == 'N' && t == 1) return true;
if(g[x][y-1] == 'W' && t == 2) return true;
if(g[x][y-1] == 'S' && t == 3) return true;
if(g[x][y-1] == 'E' && t == 0) return true;
return false;
}
void bfs(void)
{
ans = -1;
tmp.x = si, tmp.y = sj, tmp.t = 0, tmp.step = 0;
q.push(tmp);
while(!q.empty()) {
now = q.top();
q.pop();
int x = now.x, y = now.y, t = now.t, step = now.step;
if(vis[x][y][step]) continue;
vis[x][y][step] = 1;
if(x == ti && y == tj) {
ans = t;
return;
}
if(step < 4) {
tmp.x = x, tmp.y = y, tmp.t = t + 1, tmp.step = step + 1;
q.push(tmp);
}
for(int i = 0; i < 4; i++) {
tmp.x = x + dir[i][0];
tmp.y = y + dir[i][1];
tmp.step = 0;
if(g[tmp.x][tmp.y] == '#') continue;
if(check(x, y, t) || check(tmp.x, tmp.y, t)) tmp.t = t + 3;
else tmp.t = t + 1;
q.push(tmp);
}
}
}
int main(void)
{
int _, __;
while(scanf("%d", &_)!=EOF) {
__ = 0;
while(_--) {
init();
read();
bfs();
if(~ans) printf("Case #%d: %d\n", ++__, ans);
else printf("Case #%d: -1\n", ++__);
}
}
return 0;
}