菜鸡


私信TA

用户名:ph1134370710

访问量:25774

签 名:

任花自飘零水自流

等  级
排  名 91
经  验 8537
参赛次数 8
文章发表 54
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

#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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区