qiujiu

P11228 [CSP-J 2024] 地图探险

shuyihang @ 2024-11-02 14:52:04

#include<bits/stdc++.h>
using namespace std;
int n,m,f;
int x,y,d;
char a[1005][1005];
int dx[10]={0,1,0,-1},dy[10]={1,0,-1,0};
int main() {
    int k;
    cin>>k;
    while(k--){
        cin>>n>>m>>f;
        cin>>x>>y>>d;
        memset(a,0,sizeof(a));
        int s=1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        while(f){
            if(x+dx[d]>n||x+dx[d]<1){
                d=(d+1)%4;
                f--;
                continue;
            }
            if(y+dy[d]>m||y+dy[d]<1){
                d=(d+1)%4;
                f--;
                continue;
            }
            if(a[x+dx[d]][y+dy[y]]=='x'){
                d=(d+1)%4;
                f--;
                continue;
            }
            if(a[x+dx[d]][y+dy[y]]=='.'){
                s++;
                a[x+dx[d]][y+dy[d]]='$';
                x=x+dx[d];
                y=y+dy[d];
                f--;
                continue;               
            }
            if(a[x+dx[d]][y+dy[d]]=='$'){
                x=x+dx[d];
                y=y+dy[d];
                f--;
                continue;
            }
        }
        cout<<s<<"\n";
    }
    return 0;
}

by Zxm2013 @ 2024-11-03 18:08:28

@ shuyihang 我的代码

#include<iostream>
using namespace std;
int t;
int main(){
    cin>>t;
    for(int i=0;i<t;i++){
        int n,m,k,x,y,r,ans=0,a[1010][1010]={0},b[1010][1010]={0};
        cin>>n>>m>>k>>x>>y>>r;
        for(int j=1;j<=n;j++){
            for(int q=1;q<=m;q++){
                char num;
                cin>>num;
                if(num=='.'){
                    a[j][q]=1;
                }
            }
        }
        b[x][y]=1;
        for(int j=1;j<=k;j++){
            if(r==0){
                if(a[x][y+1]==1){
                    if(b[x][y+1]==0){
                        ans++;
                        b[x][y+1]=1;
                    }
                    y++;
                }
                else r=(r+1)%4;
            }
                        else if(r==1){
                if(a[x+1][y]==1){
                    if(b[x+1][y]==0){
                        ans++;
                            b[x+1][y]=1;
                    }
                    x++;
                }
                else r=(r+1)%4;
            }
                                else if(r==2){
                if(a[x][y-1]==1){
                    if(b[x][y-1]==0){
                        ans++;
                            b[x][y-1]=1;
                    }
                    y--;
                }
                else r=(r+1)%4;
            }
                                            else {
                if(a[x-1][y]==1){
                    if(b[x-1][y]==0){
                        ans++;
                            b[x-1][y]=1;
                    }
                    x--;
                }
                else r=(r+1)%4;
            }
        }
        cout<<ans+1<<endl;
    }
    return 0;
}

求关,我们可以互相学习qwq~


|