警示后人

P11228 [CSP-J 2024] 地图探险

wangcm @ 2024-11-07 18:11:07

在每组测试数据后记得重置vis数组。

100pts->0pts

0pts代码:

#include<bits/stdc++.h>
using namespace std;
int t,n,m,k,x,y,d;
char a[2001][2001];
bool vis[2001][2001]={false};
int main()
{
//  freopen("explore.in","r",stdin);
//  freopen("explore.out","w",stdout);
    cin>>t;
    while(t--){
        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];
            }
        }
        vis[x][y]=true;
        long long ans=0;
        for(int i=1;i<=k;i++){
            if(d==0&&1<=x&&x<=n&&1<=y+1&&y+1<=m&&a[x][y+1]=='.'){
                vis[x][y+1]=true;
                y++;
            }
            else if(d==1&&1<=x+1&&x+1<=n&&1<=y&&y<=m&&a[x+1][y]=='.'){
                vis[x+1][y]=true;
                x++;
            } 
            else if(d==2&&1<=x&&x<=n&&1<=y-1&&y-1<=m&&a[x][y-1]=='.'){
                vis[x][y-1]=true;
                y--;
            } 
            else if(d==3&&1<=x-1&&x-1<=n&&1<=y&&y<=m&&a[x-1][y]=='.'){
                vis[x-1][y]=true;
                x--;
            } 
            else{
                d=(d+1)%4;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(vis[i][j]==true&&a[i][j]!='x'){
                    ans++;
                } 
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

100pts代码:

#include<bits/stdc++.h>
using namespace std;
int t,n,m,k,x,y,d;
char a[2001][2001];
bool vis[2001][2001]={false};
int main()
{
//  freopen("explore.in","r",stdin);
//  freopen("explore.out","w",stdout);
    cin>>t;
    while(t--){
        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];
            }
        }
        vis[x][y]=true;
        long long ans=0;
        for(int i=1;i<=k;i++){
            if(d==0&&1<=x&&x<=n&&1<=y+1&&y+1<=m&&a[x][y+1]=='.'){
                vis[x][y+1]=true;
                y++;
            }
            else if(d==1&&1<=x+1&&x+1<=n&&1<=y&&y<=m&&a[x+1][y]=='.'){
                vis[x+1][y]=true;
                x++;
            } 
            else if(d==2&&1<=x&&x<=n&&1<=y-1&&y-1<=m&&a[x][y-1]=='.'){
                vis[x][y-1]=true;
                y--;
            } 
            else if(d==3&&1<=x-1&&x-1<=n&&1<=y&&y<=m&&a[x-1][y]=='.'){
                vis[x-1][y]=true;
                x--;
            } 
            else{
                d=(d+1)%4;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(vis[i][j]==true&&a[i][j]!='x'){
                    ans++;
                } 
            }
        }
        cout<<ans<<endl;
        memset(vis,false,sizeof(vis));
    }

    return 0;
}

前后只多了memset那行代码,我真服了……


by shawn0618 @ 2024-11-07 18:27:32

废话


by Wangyuqiao0629 @ 2024-11-08 19:43:54

同感

0pts

#include <bits/stdc++.h>
using namespace std;
int visited[1005][1005];
int main(){
    //freopen("explore.in","r",stdin);
    //freopen("explore.out","w",stdout);
    int t;
    cin>>t;
    int m,n,k,x,y,d,step;
    char maper[1005][1005];

    for(int i=0; i<t; i++){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        step=0;
        for(int a=1; a<=n; a++){
            for(int b=1; b<=m; b++){
                cin>>maper[a][b];
            }
        }
        visited[x][y]=1;
        for(int st=1; st<=k; st++){
            switch(d){
                case 0:
                    if((y+1)>m or maper[x][y+1]=='x'){
                        d=(d+1)%4;
                    }else{
                        y++;
                        visited[x][y]=1;
                    }
                    break;
                case 1:
                    if((x+1)>n or maper[x+1][y]=='x'){
                        d=(d+1)%4;
                    }else{
                        x++;
                        visited[x][y]=1;
                    }
                    break;
                case 2:
                    if((y-1)<=0 or maper[x][y-1]=='x'){
                        d=(d+1)%4;
                    }else{
                        y--;
                        visited[x][y]=1;
                    }
                    break;
                case 3:
                    if((x-1)<=0 or maper[x-1][y]=='x'){
                        d=(d+1)%4;
                    }else{
                        x--;
                        visited[x][y]=1;
                    }
                    break;
            }   
        }
        for(int a=1; a<=n; a++){
            for(int b=1; b<=m; b++){
                if(visited[a][b]==1)step++;
            }
        }
        cout<<step<<endl;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

100pts

#include <bits/stdc++.h>
using namespace std;
int visited[1005][1005];
int main(){
    //freopen("explore.in","r",stdin);
    //freopen("explore.out","w",stdout);
    int t;
    cin>>t;
    int m,n,k,x,y,d,step;
    char maper[1005][1005];

    for(int i=0; i<t; i++){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        step=0;
        for(int a=1; a<=n; a++){
            for(int b=1; b<=m; b++){
                cin>>maper[a][b];
                visited[a][b]=0;
            }
        }
        visited[x][y]=1;
        for(int st=1; st<=k; st++){
            switch(d){
                case 0:
                    if((y+1)>m or maper[x][y+1]=='x'){
                        d=(d+1)%4;
                    }else{
                        y++;
                        visited[x][y]=1;
                    }
                    break;
                case 1:
                    if((x+1)>n or maper[x+1][y]=='x'){
                        d=(d+1)%4;
                    }else{
                        x++;
                        visited[x][y]=1;
                    }
                    break;
                case 2:
                    if((y-1)<=0 or maper[x][y-1]=='x'){
                        d=(d+1)%4;
                    }else{
                        y--;
                        visited[x][y]=1;
                    }
                    break;
                case 3:
                    if((x-1)<=0 or maper[x-1][y]=='x'){
                        d=(d+1)%4;
                    }else{
                        x--;
                        visited[x][y]=1;
                    }
                    break;
            }   
        }
        for(int a=1; a<=n; a++){
            for(int b=1; b<=m; b++){
                if(visited[a][b]==1)step++;
            }
        }
        cout<<step<<endl;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

对比看看,差了什么?


|