Code: Select all
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <string.h>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define nln puts("") ///printnewline
#define getInt(a) scanf("%d",&a)
#define Max(a,b,c) max(a,max(b,c)) ///3 ta theke max
#define Min(a,b,c) min(a,min(b,c)) ///3 ta theke min
#define FOR1(i,n) for(i=1;i<=n;i++)
#define FOR0(i,n) for(i=0;i<n;i++) ///looping
#define FORR(i,n) for(i=n-1;i>=0;i--)
#define ALL(p) p.begin(),p.end()
#define SET(p) memset(p,-1,sizeof(p))
#define CLR(p) memset(p,0,sizeof(p)) ///memset
#define MEM(p,v) memset(p,v,sizeof(p))
#define READ(f) freopen(f, "r", stdin) /// file
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size()
#define PB(x) push_back(x) ///STL defines
#define MP(x,y) make_pair(x,y)
#define ff first
#define ss second
#define LI long int
#define LLI long long int
#define f64 long double
#define PI acos(-1.0) ///PI er value
#define check(n, pos) (n & (1<<(pos))) ///AND
#define biton(n, pos) (n | (1<<(pos))) ///OR }-bit opr.
#define bitoff(n, pos) (n & ~(1<<(pos))) ///OFF
#define dbg(x) cout <<"Line= "<<__LINE__ <<" -> "<< #x " = " << (x) << endl;
void CI(int &_x)
{
scanf("%d",&_x);
}
void CI(int &_x,int &_y)
{
CI(_x);
CI(_y);
}
void CI(int &_x,int &_y,int &_z)
{
CI(_x);
CI(_y,_z);
}
void CI(int &_a,int &_b,int &_c,int &_d)
{
CI(_a,_b);
CI(_c,_d);
}
template<typename T> void getarray(T a[],int b,int e)
{
for(int i=b; i<e+b; i++) cin>>a[i];
}
template<typename T> void printarray(T a[],int b,int e)
{
for(int i=b; i<e-1+b; i++) cout<<a[i]<<" ";
cout<<a[e-1+b]<<endl;
}
template <typename T> T BigMod (T b,T p,T m)
{
if (p == 0) return 1;
if (p%2 == 0)
{
T s = BigMod(b,p/2,m);
return ((s%m)*(s%m))%m;
}
return ((b%m)*(BigMod(b,p-1,m)%m))%m;
}
template <typename T> T ModInv (T b,T m)
{
return BigMod(b,m-2,m);
}
const double EPS=1e-9; ///constatnts
const int INF=0x7f7f7f7f;
int dr8[8]= {1,-1,0,0,1,-1,-1,1}; ///8 direction move
int dc8[8]= {0,0,-1,1,1,1,-1,-1};
int dr4[4]= {0,0,1,-1}; ///4 direction move
int dc4[4]= {-1,1,0,0}; ///or adjacent dir.
int kn8r[8]= {1,2,2,1,-1,-2,-2,-1}; ///knight moves
int kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
struct point
{
int x;
int y;
int jump;
point(int _x=0,int _y=0,int _j=0) : x(_x),y(_y),jump(_j) {};
} ;
char board[301][301];
int dis[301][301][4];
int R,C;
bool isValid(point p)
{
if(p.x>=1&&p.y>=1&&p.x<=C&&p.y<=R)
return 1;
return 0;
}
int ans;
void bfs(point source)
{
queue<point> Q;
Q.push(source);
dis[source.y][source.x][source.jump]=0;
while(!Q.empty())
{
point currp=Q.front();
Q.pop();
//cout<<currp.y<<" "<<currp.x<<" "<<currp.jump<<" "<<dis[currp.y][currp.x][currp.jump]<<endl;
// if(board[currp.y][currp.x]=='E')
// {
// ans= dis[currp.y][currp.x][currp.jump];
// return;
// }
for(int i=0; i<4; i++)
{
point nextp=point(currp.x+dr4[i]*currp.jump, currp.y+dc4[i]*currp.jump, currp.jump%3+1);
if(isValid(nextp)==1)
{
int &kola=dis[nextp.y][nextp.x][nextp.jump];
if(kola==INF)
{
bool obst=0;
for(int mv=1; mv<=nextp.jump; mv++)
{
if(board[currp.y+dc4[i]*mv][currp.x+dr4[i]*mv]=='#')
obst=1;
}
if(obst)
continue;
kola=dis[currp.y][currp.x][currp.jump]+1;
Q.push(nextp);
}
}
}
}
}
/**UVa 928**/
int main()
{
//READ("input.txt");
// WRITE("input.txt");
int t,i,j;
CI(t);
point start,endp;
while(t--)
{
CI(R,C);
for(int r=1; r<=R; r++)
{
getchar();
for(int c=1; c<=C; c++)
{
board[r][c]=getchar();
if(board[r][c]=='S')
start = point(c,r,1);
if(board[r][c]=='E')
endp = point(c,r,0);
dis[r][c][0]=INF;
dis[r][c][1]=INF;
dis[r][c][2]=INF;
dis[r][c][3]=INF;
}
}
ans=INF;
bfs(start);
ans=min(ans,dis[endp.y][endp.x][0]);
ans=min(ans,dis[endp.y][endp.x][1]);
ans=min(ans,dis[endp.y][endp.x][2]);
ans=min(ans,dis[endp.y][endp.x][3]);
if(ans==INF)
cout<<"NO"<<endl;
else
cout<<ans<<endl;
}
return 0;
}