wangmuze @ 2024-11-16 18:59:24
#include<bits/stdc++.h>
using namespace std;
bool f[1010][1010];
char a[1010][1010];
int main()
{
int t;
cin>>t;
while(t--)
{
int cnt=0;
memset(f,0,sizeof(f));
int n,m,k;
cin>>n>>m>>k;
int x,y,d;
cin>>x>>y>>d;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=k;i++)
{
int newx=x;
int newy=y;
if(d==0)
{
newy++;
if(a[x][y]=='.'&&1<=newx&&newx<=n&&1<=newy&&newy<=m)
{
x=newx;
y=newy;
if(f[x][y]==false)
{
cnt++;
f[x][y]=true;
}
}
else
{
d=(d+1)%4;
cnt++;
}
}
else if(d==1)
{
newx++;
if(a[x][y]=='.'&&1<=newx&&newx<=n&&1<=newy&&newy<=m)
{
x=newx;
y=newy;
if(f[x][y]==false)
{
cnt++;
f[x][y]=true;
}
}
else
{
d=(d+1)%4;
cnt++;
}
}
else if(d==2)
{
newy--;
if(a[x][y]=='.'&&1<=newx&&newx<=n&&1<=newy&&newy<=m)
{
x=newx;
y=newy;
if(f[x][y]==false)
{
cnt++;
f[x][y]=true;
}
}
else
{
d=(d+1)%4;
cnt++;
}
}
else if(d==3)
{
newx--;
if(a[x][y]=='.'&&1<=newx&&newx<=n&&1<=newy&&newy<=m)
{
x=newx;
y=newy;
if(f[x][y]==false)
{
cnt++;
f[x][y]=true;
}
}
else
{
d=(d+1)%4;
cnt++;
}
}
}
cout<<cnt<<endl;
}
return 0;
}