FengYuXinMing @ 2024-10-27 08:11:44
大概用的就是普通的模拟,nm数组记录森林,z代表走过了,然后xx,yy是xy值,一直在变d就是方向,求大佬调一下
#include<bits/stdc++.h>
using namespace std;
const int Maxn = 1e3+5;
int T,n,m,k;
int xx,yy,d;
char nm[Maxn][Maxn];
int main()
{
cin >> T;
for (int i = 1;i <= T;i++){
int sum = 1;
cin >> n >> m >> k;
cin >> xx >> yy >> d;
nm[xx][yy] = 'z';
for (int j = 1;j <= n;j++){
for (int k = 1;k <= m;k++){
cin >> nm[j][k];
}
}
for (int l = 1;l <= k;l++){
if (d == 0){
if (yy+1 <= m && nm[xx][yy+1] != 'x'){
if (nm[xx][yy+1] != 'z'){
sum++;
nm[xx][yy+1] = 'z';
}
yy+=1;
}
else d = (d+1) % 4;
}
else if (d == 1){
if (xx+1 <= n && nm[xx+1][yy] != 'x'){
if (nm[xx+1][yy] != 'z'){
sum++;
nm[xx+1][yy] = 'z';
}
xx+=1;
}
else d = (d+1) % 4;
}
else if (d == 2){
if (yy-1 > 0 && nm[xx][yy-1] != 'x'){
if (nm[xx][yy-1] != 'z'){
sum++;
nm[xx][yy-1] = 'z';
}
yy-=1;
}
else d = (d+1) % 4;
}
else if (d == 3){
if(xx-1 > 0 && nm[xx-1][yy] != 'x'){
if (nm[xx-1][yy] != 'z'){
sum++;
nm[xx-1][yy] = 'z';
}
xx-=1;
}
else d = (d+1) % 4;
}
}
cout << sum << endl;
}
return 0;
}
注:文件读入和写入我删了,赛时写了()
by ARIS2_0 @ 2024-10-27 08:28:10
@FengYuXinMing
nm[xx][yy] = 'z';
和
for (int j = 1;j <= n;j++){
for (int k = 1;k <= m;k++){
cin >> nm[j][k];
}
}
顺序调换一下
by ARIS2_0 @ 2024-10-27 08:29:26
@FengYuXinMing 你那样写的话,nm[xx][yy]='z';
就相当于没有(因为被输入数据覆盖了)
by FengYuXinMing @ 2024-10-27 08:34:56
@ARIS2_0 哦哦,现在只能希望自己赛时把这句话写在输入后面了。因为我当时记起来还特意写了一下。
by ARIS2_0 @ 2024-10-27 08:35:37
@FengYuXinMing 祝你好运