注意边界:
/*
参考题解:
https://www.acwing.com/solution/content/26973/
https://www.acwing.com/solution/content/21775/
观看视频:
https://www.acwing.com/video/132/
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb push_back
#define fi first
#define se second
#define mem(a,x) memset(a,x,sizeof(a));
#define db double
#define fir(i,a,n) for(int i=a;i<=n;i++)
#define debug(x) cout<<#x<<" "<<x<<endl;
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
//要注意边界范围======================
const int N=510;
int n,m;
char g[N][N];
int ans[N][N];
deque<pii>dq;
int dx[4]={-1,-1,1,1};
int dy[4]={-1,1,-1,1};//格点的四个方向
int ix[4]={-1,-1,0,0};
int iy[4]={-1,0,-1,0};
string idxy="\\//\\";
void bfs()
{
dq.clear();
mem(ans,0x3f);
dq.push_back({0,0});
ans[0][0]=0;
while(dq.size())
{
auto t=dq.front();
dq.pop_front();
for(int i=0;i<4;i++)
{
int x=t.fi+dx[i];
int y=t.se+dy[i];
if(x>=0&&x<=n&&y>=0&&y<=m)
{
int w=0;//先假设权值为0
int xx=t.fi+ix[i];
int yy=t.se+iy[i];
if(g[xx][yy]!=idxy[i]) w=1;
if(ans[x][y]>ans[t.fi][t.se]+w)
{
ans[x][y]=ans[t.fi][t.se]+w;
if(w) dq.push_back({x,y});
else dq.push_front({x,y});
}
}
}
}
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>g[i];
bfs();
if(ans[n][m]==0x3f3f3f3f) cout<<"NO SOLUTION";
else cout<<ans[n][m];
cout<<endl;
}
return 0;
}