taohuanyu @ 2024-11-23 19:15:22
#include<bits/stdc++.h>
using namespace std;
void f()
{
int n,m,k,x,y,d,sum=0;
char a[1000][1000];
cin>>n>>m>>k>>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++)
{
if(d==0)
{
if(a[x][y+1]!='X' and x<n and x>1 and y+1<m and y+1>1) y=y+1;
else
{
d++;
d=d%4;
}
}
else if(d==2)
{
if(a[x][y-1]!='X' and x<n and x>1 and y-1<m and y-1>1) y=y-1;
else
{
d++;
d=d%4;
}
}
else if(d==1)
{
if(a[x+1][y]!='X' and x+1<n and x+1>1 and y<m and y>1) x=x+1;
else
{
d++;
d=d%4;
}
}
else if(d==3)
{
if(a[x-1][y]!='X' and x+1<n and x+1>1 and y<m and y>1) x=x-1;
else
{
d++;
d=d%4;
}
}
//if(a[x][y]!='#') sum++;
a[x][y]='#';
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='#') sum++;
cout<<sum<<endl;
return ;
}
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
f();
}
by LittleE1 @ 2024-11-23 20:19:06
@[taohuanyu] AC代码
#include <bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
char ch[1005][1005];
const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
void solve() {
int n,m,k,x0,y0,d0;
memset(vis,0,sizeof(vis));
cin >> n >> m >> k;
cin >> x0 >> y0 >> d0;
for (int i=1;i<=n;i++) {
char s[1005];
cin >> s;
for (int j=1;j<=m;j++)
ch[i][j]=s[j-1];
}
vis[x0][y0]=true;
for (int i=1;i<=k;i++) {
int x1=x0+dx[d0],y1=y0+dy[d0];
if (1<=x1 && x1<=n && 1<=y1 && y1<=m && ch[x1][y1]=='.') {
x0=x1;
y0=y1;
}
else
d0=(d0+1)%4;
vis[x0][y0]=true;
}
int ans=0;
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++)
ans+=vis[i][j];
}
cout << ans << endl;
}
int main() {
int T;
cin >> T;
while (T--) {
solve();
}
}
by niuniudundun @ 2024-12-04 21:15:00
@LittleE1这是题解代码吧?