40pts求调

P11228 [CSP-J 2024] 地图探险

itz_HanQAQ @ 2024-12-17 22:18:35

#include<bits/stdc++.h>
using namespace std;
struct map_k{
    int n,m,k;
    int x0,y0,d0;
    char char_map[1005][1005];
    bool true_or_false[1005][1005];
    int ans;
};
map_k a[10];

int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

int main(){
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>a[i].n>>a[i].m>>a[i].k;
        cin>>a[i].x0>>a[i].y0>>a[i].d0;
        for(int j=1;j<=a[i].n;j++){
            for(int l=1;l<=a[i].m;l++){
                cin>>a[i].char_map[j][l];
            }
        }
        a[i].ans=1;
    }
    int xn,yn;
    for(int i=1;i<=t;i++){
        for(int j=1;j<=a[i].k;j++){
            xn=a[i].x0+dx[a[i].d0];
            yn=a[i].y0+dy[a[i].d0];
            if(1<=xn && xn<=a[i].n && 1<=yn && xn<=a[i].m && a[i].char_map[xn][yn]=='.' && a[i].true_or_false[xn][yn]==false){
                a[i].x0=xn;
                a[i].y0=yn;
                a[i].ans++;
                a[i].true_or_false[xn][yn]=true;
                //cout<<a[i].x0<<' '<<a[i].y0<<endl;
            }
            else {
                a[i].d0=(a[i].d0+1)%4;
                //cout<<a[i].d0<<endl;
            }
        }
        cout<<a[i].ans<<endl;
    } 
    return 0;
}

by Lsm2013 @ 2025-01-07 18:57:34

附上最最简单易懂的代码:

#include<bits/stdc++.h>
using namespace std;
char rmap[1010][1010];
int vis[1010][1010],t; 
int main()
{
    cin>>t;
    while(t--)
    {
        int n,m,d,x,y,k,i,j;
        cin>>n>>m>>k>>x>>y>>d;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>rmap[i][j];
                vis[i][j]=0;
            }
        }
        vis[x][y]=1;
        for(i=1;i<=k;i++)
        {
            int x2,y2;
            if(d==0)
            {
                x2=x;
                y2=y+1;
            }
            if(d==1)
            {
                x2=x+1;
                y2=y;
            }
            if(d==2)
            {
                x2=x;
                y2=y-1;
            }
            if(d==3)
            {
                x2=x-1;
                y2=y;
            }
            if(x2>=1&&y2>=1&&x2<=n&&y2<=m&&rmap[x2][y2]=='.')
            {
                vis[x2][y2]=1;
                x=x2;
                y=y2;
            }
            else
            {
                d=(d+1)%4;
            }
        }
        int sum=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(vis[i][j])
                {
                    sum++;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

求关qwp


|