nowornever0625 @ 2024-10-26 22:31:32
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
char Map[N][N];
bool vis[N][N];
int main() {
//d=0东 1南 2西 3北
// freopen("tmp.in", "r", stdin);
int T;
int n, m, k, x0, y0, d0, ans, x, y, d;
cin >> T;
while (T--) {
memset(vis, 0, sizeof(vis));
cin >> n >> m >> k >> x0 >> y0 >> d0;
for (int i = 1; i <= n; i++) {
string a;
cin >> a;
for (int j = 1; j <= m; j++)
Map[i][j] = a[j - 1];
}
x = x0, y = y0, d = d0;
while (k--) {
// cout << x << y << ' ' << k << endl;
ans = 0;
vis[x][y] = 1;
if (d == 0) {
if (Map[x][y + 1] == '.')
y++, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 1) {
if (Map[x + 1][y] == '.')
x++, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 2) {
if (Map[x][y - 1] == '.')
y--, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
if (d == 3) {
if (Map[x - 1][y] == '.')
x--, vis[x][y] = 1;
else {
d = (d + 1) % 4;
continue;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans += vis[i][j];
cout << ans << endl;
}
return 0;
}
by ShenShi_Yan @ 2024-10-26 22:35:55
Map也要初始化,好兄弟。。我也是这样的mp没初始化,希望他别卡这个。。
by xiaokang_suancai @ 2024-10-26 22:37:55
@RloveY 阿米诺斯我本来写了初始化后来删掉了啊啊啊啊啊啊
by xiaokang_suancai @ 2024-10-26 22:38:38
@RloveY 为什么要写初始化?读入之后原来的地图应该会被覆盖掉吧
by lzy120406 @ 2024-10-26 22:39:11
@nowornever0625 边界情况?
by ShenShi_Yan @ 2024-10-26 22:46:18
@xiaokang_suancai 如果n,m都比上次大就会覆盖掉,比上次小就不好说了。。
by ShenShi_Yan @ 2024-10-26 22:48:32
@xiaokang_suancai 因为他不会自动初始化的,如果m,n比上次小,这次边界之外也会有字符,这样特判就显得有点唐了。。
by xiaokang_suancai @ 2024-10-26 22:51:39
@RloveY 还是有点不懂(糖丸了),如果超出边界了不是应该转向吗
by xiaokang_suancai @ 2024-10-26 22:52:15
@RloveY 能给一下
by ShenShi_Yan @ 2024-10-26 22:55:05
这样也能过就是多加两个特判:if(x+1<=n)和if(y+1<=m),这样就相当于手动设置边界了
#include<bits/stdc++.h>
using namespace std;
char mp[1025][1025];
bool lxl[1025][1025];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,k,x,y,d;
scanf("%d%d%d%d%d%d",&n,&m,&k,&x,&y,&d);
for(register int i=1;i<=n;++i){for(register int j=1;j<=m;++j){cin>>mp[i][j];}}
memset(lxl,0,sizeof(lxl));
lxl[x][y]=1;
int cnt=1;
for(register int i=0;i<k;++i){
if(d==0){
if(y+1<=m&&mp[x][y+1]=='.'){
y++;
if(lxl[x][y]==0){cnt++,lxl[x][y]=1;}
}
else d++;
}
else if(d==1){
if(x+1<=n&&mp[x+1][y]=='.'){
x++;
if(lxl[x][y]==0){cnt++,lxl[x][y]=1;}
}
else d++;
}
else if(d==2){
if(y-1>0&&mp[x][y-1]=='.'){
y--;
if(lxl[x][y]==0){cnt++,lxl[x][y]=1;}
}
else d++;
}
else{
if(x-1>=0&&mp[x-1][y]=='.'){
x--;
if(lxl[x][y]==0){cnt++,lxl[x][y]=1;}
}
else d=0;
}
}
cout<<cnt<<"\n";
}
return 0;
}
by ShenShi_Yan @ 2024-10-26 22:56:04
@xiaokang_suancai 但是你没特判边界。。所以它还会往外找