Jasper2024 @ 2024-10-27 21:23:46
#include <iostream>
using namespace std;
int main()
{
int i,t;
cin>>t;
for(i=0;i<t;i++)
{
int n,m,k,x,y,d,x1,y1,sum=1;
cin>>n>>m>>k;
cin>>x>>y>>d;
char a[n][m];
for(int j=1;j<=n;j++)
for(int q=1;q<=m;q++)
cin>>a[j][q];
for(int j=0;j<k;j++)
{
if(d==0)
{
x1=x;
y1=y+1;
}
else if(d==1)
{
x1=x+1;
y1=y;
}
else if(d==2)
{
x1=x;
y1=y-1;
}
else if(d==3)
{
x1=x-1;
y1=y;
}
if(x1<1||x1>n||y1<1||y1>m||a[x1][y1]=='x')
{
d=d+1;
d=d%4;
}
else if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&a[x1][y1]!='x')
{
x=x1;
y=y1;
if(a[x1][y1]=='.')
sum++;
a[x1][y1]=',';
}
}
cout<<sum<<endl;
}
return 0;
}
by wanghuwang @ 2024-10-27 21:27:13
你有一行是
char a[n][m];
数组的长度不能为变量,你尝试改一下。
by shawn0618 @ 2024-10-27 21:27:56
by shawn0618 @ 2024-10-27 21:29:14
@Jasper2024
by wanghuwang @ 2024-10-27 21:29:28
改了50WA记录
你看看代码哪里写错了,应该有问题。
by shawn0618 @ 2024-10-27 21:32:44
重新说一下。
if(a[x1][y1]=='.')
sum++;
a[x1][y1]=',';
这里
by Jasper2024 @ 2024-10-27 21:36:30
@shawn0618 我判断的是如果a[x1][y1]不是x,就执行, ‘,’不是‘x'
by LANDER_TT @ 2024-10-27 21:48:17
#include <bits/stdc++.h>
using namespace std;
bool vist[2000][2000];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,k,x,y,d,sum=0;
cin>>n>>m>>k;
cin>>x>>y>>d;
char a[2000][2000];
memset(a,0,sizeof(a)); //初始化
memset(vist,0,sizeof(vist));
for(int j=1;j<=n;j++){
for(int q=1;q<=m;q++){
cin>>a[j][q];
}
}
for(int j=0;j<k;j++)
{ //原来的是先加再判
if(!vist[x][y]){
sum++; //每个点只记一次数
vist[x][y]=1;
}
if(d==0&&a[x][y+1]=='.')
{
x=x;
y=y+1;
}
else if(d==1&&a[x+1][y]=='.')
{
x=x+1;
y=y;
}
else if(d==2&&a[x][y-1]=='.')
{
x=x;
y=y-1;
}
else if(d==3&&a[x-1][y]=='.')
{
x=x-1;
y=y;
}
else
{
d=(d+1)%4; //只有不能走才转向
}
}
if(!vist[x][y]){
sum++; //最后到达的点需要判断
}
cout<<sum<<endl;
}
return 0;
}
@Jasper2024
by a_silver_star
by LANDER_TT @ 2024-10-27 21:48:51
by gyf xjcw2022