#include<bits/stdc++.h>
using namespace std;
char a[100][100];
int book[100][100]={0},b[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int c,d;
int xq,yq,xz,yz;
struct s{
int x;
int y;
int s;
}qe[100000];
int main()
{
int n;
cin>>n;
while(n--)
{
memset(book, 0, sizeof(book));
char s='S',e='E';
cin>>c>>d;
for(int i=0;i<c;i++)
for(int j=0;j<d;j++)
cin>>a[i][j];
for(int i=0;i<c;i++)
for(int j=0;j<d;j++)
{
if(s==a[i][j]) {
xq=i;yq=j;
}
if(e==a[i][j]){
xz=i;yz=j;//找到起始点的坐标
}
}
book[xq][yq]=1;
int tx=0,ty=0,k,h=1,tt=1;
qe[tt].x=xq;
qe[tt].y=yq;
qe[tt].s=0;
tt++;
int flag=0;//用来标记是否到达终点
while(h<tt)
{
for(k=0;k<=3;k++)//枚举四个方向;
{
tx=qe[h].x+b[k][0];
ty=qe[h].y+b[k][1];
if((a[tx][ty]=='-'||a[tx][ty]=='E')&& book[tx][ty]==0&& tx>=0&&tx<c&&ty>=0&&ty<d)
//判断是否越界,是否在路上
//满足条件出列
{
book[tx][ty]=1;//标记这个点已经走过
qe[tt].y=ty;
qe[tt].x=tx;
qe[tt].s=qe[h].s+1;
tt++;
}
if(tx==xz&&ty==yz)
{
flag=1;
break;
}
}
if(flag==1)
break;
h++;
}
if(flag==1)
cout<<qe[tt-1].s<<endl;
else cout<<-1<<endl;
}
return 0;
}
0.0分
0 人评分
C语言训练-求函数值 (C语言代码)浏览:938 |
A+B for Input-Output Practice (VI) (C++代码)浏览:418 |
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:572 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:756 |
WU-整除问题 (C++代码)浏览:612 |
IP判断 (C语言代码)浏览:763 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:591 |
C二级辅导-温度转换 (C语言代码)浏览:732 |
判定字符位置 (C语言代码)浏览:799 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:617 |