http://paste.ubuntu.com/14663693/#include
<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
char mp[55][55];
int dis[4][2] = { {-1,0},{1,0},{0,1},{0,-1}};
int n,m;
struct I
{
int x,y;
}s,e;
struct P
{
int w;
int num;
}res[55][55];
bool Isleagal( I a )
{
if( a.x < 1 || a.x > n || a.y < 1 || a.y > m || mp[a.x][a.y] == '#')
return false;
return true;
}
void bfs()
{
for( int i = 1; i <= n; i++ )
for( int j = 1; j <= m; j++ )
{
res[i][j].w = 99999;
res[i][j].num = 0;
}
queue<I> q;
I now,next;
q.push( s );
res[s.x][s.y].w = 0;
res[s.x][s.y].num = 1;
while( !q.empty() )
{
now = q.front();
q.pop();
if( now.x == e.x && now.y == e.y )
continue;
for( int i = 0; i < 4; i++ )
{
next.x = now.x + dis[i][0];
next.y = now.y + dis[i][1];
if( Isleagal(next) )
{
if( res[now.x][now.y].w + 1 < res[next.x][next.y].w)
{
res[next.x][next.y].w = res[now.x][now.y].w + 1;
res[next.x][next.y].num = (res[now.x][now.y].num) % 10086;
q.push(next);
}
else if( res[now.x][now.y].w + 1 == res[next.x][next.y].w )
{
res[next.x][next.y].num = (res[now.x][now.y].num + res[next.x][next.y].num)%10086;
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while( t-- )
{
scanf("%d %d",&n,&m);
for( int i = 1; i <= n; i++ )
{
getchar();
for( int j = 1; j <= m; j++ )
{
scanf("%c",&mp[i][j]);
if( mp[i][j] == 'S')
{
s.x = i;
s.y = j;
}
if( mp[i][j] == 'N')
{
e.x = i;
e.y = j;
}
}
}
bfs();
printf("%d\n",res[e.x][e.y].num);
}
return 0;
}
/**************************************************************
Problem: 1080
User: 105032014065
Language: C++
Result: Accepted
Time:0 ms
Memory:1640 kb
****************************************************************/