#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
struct Node
{
int x,y;
int step;
}node;
queue<Node> q;
char map[202][202];
bool vis[202][202];
int S,N,M;
int startx,starty;
int nxtx[4]={0,0,1,-1};
int nxty[4]={1,-1,0,0};
int bfs()
{
int x,y,step;
node.x=startx;
node.y=starty;
node.step=0;
q.push(node);
while(!q.empty())
{
/*
@ 道路
a 公主
r 骑士
x 守卫
# 墙壁
*/
node=q.front();
q.pop();
x=node.x;y=node.y;step=node.step;
for(int i=0;i<4;i++)
{
int nx=x+nxtx[i];
int ny=y+nxty[i];
int ns=step+1;
if(map[nx][ny]=='#'||vis[nx][ny]) continue;
if(map[nx][ny]=='x') ns++;
if(map[nx][ny]=='a') return ns;
node.x=nx;
node.y=ny;
node.step=ns;
q.push(node);
vis[nx][ny]=1;
}
}
return 0;
}
int main()
{
scanf("%d",&S);
while(S--)
{
memset(map,'#',sizeof(map));
memset(vis,0,sizeof(vis));
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
scanf(" %c",&map[i][j]);
if(map[i][j]=='r')
{
startx=i;
starty=j;
}
}
printf("%d\n",bfs());
}
return 0;
}
0分求助!