#include <iostream>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
struct grid
{
int x,y,step;
};
char a[101][101];
int xstart,ystart,xend,yend;
int n,m;
void path()
{
queue<grid> q;
q.push({xstart,ystart,0});
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
while(!q.empty())
{
grid t=q.front();
q.pop();
if (t.x==xend && t.y==yend)
{
cout<<t.step<<endl;
return;
}
for(int i=0;i<4;i++)
{
int x=(t.x+dir[i][0]+n)%n;
int y=(t.y+dir[i][1]+m)%m;
if (a[x][y]=='0')
{
a[x][y]='1';
q.push({x,y,t.step+1});
}
}
}
cout<<"die"<<endl;
}
int main() //主函数
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
scanf("%s",a[i]);
cin>>xstart>>ystart>>xend>>yend;
path();
}
}
18440 走迷宫
最新推荐文章于 2024-10-09 09:53:23 发布