求救!!!样例过不了!!

P11228 [CSP-J 2024] 地图探险

zhouchenrui @ 2024-11-02 11:13:15

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
char a[N][N];
int main(){
    bool vis[N][N];
    int t;
    cin>>t;
    while(t--){
        int n,m,k;
        int x,y,d;
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        int cnt=0;
        while(k>0){
            if(d==0){
                if(a[x][y+1]=='.'){
                    if(vis[x][y+1]==true){
                        cnt++;
                        vis[x][y+1]==false;
                        k--;
                    }else{
                        k--;
                    }
                }else{
                    d=1;
                }
            }
            if(d==1){
                if(a[x+1][y]=='.'){
                    if(vis[x+1][y]==true){
                        cnt++;
                        vis[x+1][y]==false;
                        k--;
                    }
                    else{
                        k--;
                    }
                }else{
                    d=2;
                }
            }
            if(d==2){
                if(a[x][y-1]=='.'){
                    if(vis[x][y-1]==true){
                        cnt++;
                        vis[x][y-1]==false;
                    }
                k--;
                }else{
                    d=3;
                }
            }
            if(d==3){
                if(a[x-1][y]=='.'){
                    if(vis[x-1][y]==true){
                        cnt++;
                        vis[x-1][y]==false;
                        k--;
                    }
                    else{
                        k--; 
                    } 
                }else{
                    d=0;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

by zhezhelin618 @ 2024-11-02 11:24:36

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+100;
int tx[]={0,1,0,-1};
int ty[]={1,0,-1,0};
int T,n,m,k,x,y,d,step;
string s;
bool vis[N][N];
char ma[N][N];
int main(){
//  freopen("explore.in","r",stdin);
//  freopen("explore.out","w",stdout);
//  freopen没用别注意代码能过
    cin>>T;
    for(int t=1;t<=T;t++){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        step=1;
        memset(vis,0,sizeof(vis));
        vis[x][y]=1;
        for(int i=1;i<=n;i++){
            cin>>s;
            for(int j=1;j<=m;j++){
                ma[i][j]=s[j-1];
            }
        }
        for(int i=1;i<=k;i++){
            int nx=x+tx[d];
            int ny=y+ty[d];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&ma[nx][ny]!='x'){
                if(!vis[nx][ny]){
                    step++;
                }
                x=nx;
                y=ny;
                vis[nx][ny]=1;
            }
            else{
                d=(d+1)%4;
            }
        }
        cout<<step<<endl;
    }
}

输入有错 应改成这样

for(int i=1;i<=n;i++){
   string s;
   cin>>s;
    for(int j=1;j<=m;j++){
        a[i][j]=s[j-1];
    }
}

by zhouchenrui @ 2024-11-02 12:00:09

谢谢大佬 但是我还是不对

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
char a[N][N];
int main(){
    bool vis[N][N];
    int t;
    cin>>t;
    while(t--){
        int n,m,k;
        int x,y,d;
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(int i=1;i<=n;i++){
            string s;
            cin>>s;
            for(int j=1;j<=m;j++){
                a[i][j]=s[j-1];
            }
        }
        int cnt=0;
        while(k>0){
            if(d==0){
                if(a[x][y+1]=='.'){
                    if(vis[x][y+1]==true){
                        cnt++;
                        vis[x][y+1]==false;
                        k--;
                    }else{
                        k--;
                    }
                }else{
                    d=1;
                }
            }
            if(d==1){
                if(a[x+1][y]=='.'){
                    if(vis[x+1][y]==true){
                        cnt++;
                        vis[x+1][y]==false;
                        k--;
                    }
                    else{
                        k--;
                    }
                }else{
                    d=2;
                }
            }
            if(d==2){
                if(a[x][y-1]=='.'){
                    if(vis[x][y-1]==true){
                        cnt++;
                        vis[x][y-1]==false;
                    }
                k--;
                }else{
                    d=3;
                }
            }
            if(d==3){
                if(a[x-1][y]=='.'){
                    if(vis[x-1][y]==true){
                        cnt++;
                        vis[x-1][y]==false;
                        k--;
                    }
                    else{
                        k--; 
                    } 
                }else{
                    d=0;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

|