全WA,求助~~qwq

P11228 [CSP-J 2024] 地图探险

xjc2011 @ 2025-01-04 19:06:24

错误代码

#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,x,y,d;
char a[1010][1010];
int main(){
    cin>>T;
    for(int i=1;i<=T;i++){
        int ans=0;
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        memset(a,' ',sizeof(a));
        for(int j=1;j<=n;j++){
            string s;
            cin>>s;
            for(int l=0;l<s.length();l++)
                a[j][l]=s[l];
        }
        for(int j=1;j<=k;j++){
            if(d==0){
                if(a[x][y+1]=='.') a[x][y+1]='o',y++,ans++;continue;
                if(a[x][y+1]=='o') y++,ans++;continue;
            }
            if(d==1){
                if(a[x+1][y]=='.') a[x+1][y]='o',x++,ans++;continue;
                if(a[x+1][y]=='o') x++,ans++;continue;
            }
            if(d==2){
                if(a[x][y-1]=='.') a[x][y-1]='o',y--,ans++;continue;
                if(a[x][y-1]=='o') y--,ans++;continue;
            }
            if(d==3){
                if(a[x-1][y]=='.') a[x-1][y]='o',x--,ans++;continue;
                if(a[x-1][y]=='o') x--,ans++;continue;
            }
            d=(d+1)%4;
        }
        cout<<ans<<'\n';
    }
    return 0;
}

by wangshengchen @ 2025-01-04 19:08:22

@xjc2011

#include<iostream>
using namespace std;
const int N=1e3+10;
int t,n,m,k,x,y,d,ans,d2[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//d是方向 
string s;
int main(){
    ios::sync_with_stdio(0); 
    cin.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>m>>k>>x>>y>>d;
        char map[N][N]={};
        bool vis[N][N]={};
        for(int i=1;i<=n;i++){
            cin>>s;//用字符串输入快 
            int len=s.size();
            for(int j=0;j<len;j++) map[i][j+1]=s[j];//存到字符数组里 
        }
        ans=1;//起点也有一步
        vis[x][y]=1;//判断走过的(可能有重复的) 
        while(k--){
            int nx=d2[d][0]+x,ny=d2[d][1]+y;//现在的x,y坐标 
            if(nx>0&&nx<n+1&&ny>0&&ny<m+1&&map[nx][ny]!='x'){//判断可不可以走 
                x=nx;
                y=ny;
                if(!vis[nx][ny]){//加个数
                    vis[nx][ny]=1;
                    ans++; 
                }
            }
            else d=(d+1)%4;//转向
        }
        cout<<ans<<"\n";
    }
    return 0;
}

求关


by WA_csp_noip @ 2025-01-04 19:19:09

#include <bits/stdc++.h>
using namespace std;

struct info {
    int d;
    int x, y;
} p;
int T, n, m, k, ans;
char s[1002][1002];
bool b[1001][1001];

int main() {
    scanf("%d", &T);
    for (int i = 1; i <= T; i++) {
        memset(b, 0, sizeof(b));
        scanf("%d%d%d", &n, &m, &k);
        scanf("%d%d%d", &p.x, &p.y, &p.d);
        for (int j = 1; j <= n; j++)
            scanf("%s", s[j] + 1);
        ans = 1;
        b[p.x][p.y] = 1;
        for (; k--; ) {
            int nx = 0, ny = 0;
            if (p.d == 0)
                nx = p.x, ny = p.y + 1;
            if (p.d == 1)
                nx = p.x + 1, ny = p.y;
            if (p.d == 2)
                nx = p.x, ny = p.y - 1;
            if (p.d == 3)
                nx = p.x - 1, ny = p.y;
            if (nx < 1 || nx > n || ny < 1 || ny > m || s[nx][ny] != '.') {
                p.d = (p.d + 1) % 4;
                continue;
            }
            p.x = nx, p.y = ny;
            if (!b[p.x][p.y]) {
                ++ans;
                b[p.x][p.y] = 1;
            }
        }
        printf("%d\n", ans);
    }
}

|