40pts求调

P11228 [CSP-J 2024] 地图探险

HaloCode @ 2024-11-06 22:13:31

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 1e3 + 10;

struct Node{
    long long x, y, d;
};

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

int main(){

    cin >> T;
    while(T--){
        long long n, m, k, sx, sy, sd;
        char dt[MAXN][MAXN] = {0};
        bool bj[MAXN][MAXN] = {0};

        cin >> n >> m >> k;
        cin >> sx >> sy >> sd;
        for(int i = 1; i <= n; i++){
            string s;
            cin >> s;
            for(int j = 0; j < s.size(); j++){
                dt[i][j + 1] = s[j];
            }
        }

        //bfs

        Node dl[MAXN];
        long long head = 1, tail = 0, op = 0;
        dl[head].x = sx;
        dl[head].y = sy;
        dl[head].d = sd;
        bj[sx][sy] = 1;
        tail++;

        while(head <= tail && k > 0){
            int a = 0; 
            op = 0;
            int od = dl[head].d;
            int ox = dl[head].x + dx[od];
            int oy = dl[head].y + dy[od];
            if(bj[ox][oy] == 0){
                if(ox >= 1 && ox <= n && oy >= 1 && oy <= m && dt[ox][oy] == '.'){
                    tail++;
                    dl[tail].x = ox;
                    dl[tail].y = oy;
                    dl[tail].d = od;
                    bj[ox][oy] = 1;
                }else{
                    op = 1;
                    dl[tail].d = (od + 1) % 4;
                }

            }
            if(op == 0){
                head++;
            }
            k--;
        }

        long long cnt = 0;

        for(int i = 0; i <= n; i++){
            for(int j = 0; j <= m; j++){
                if(bj[i][j] > 0){
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
}
return 0;
}

by yangyuhan_yyh @ 2024-11-08 20:36:12

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

by Are_you_sure_cjj @ 2024-11-08 21:06:29

@kingsleykingsley 你好


by Are_you_sure_cjj @ 2024-11-08 21:07:12

#include<bits/stdc++.h>
using namespace std;
char M[1005][1005];
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int vis[1005][1005];
int main(){
    int T,n,m,cnt,k,x,y,d,ny,nx;
    cin>>T;
    while(T--)
    {
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(int i=0;i<1005;i++)
            for(int j=0;j<1005;j++)
                M[i][j]='x',vis[i][j]=0;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                cin>>M[i][j];
            }
        cnt=1;
        vis[x][y]=1;
        while(k--){
            nx=x+dx[d],ny=y+dy[d];
            if(M[nx][ny]=='x'){d=(d+1)%4;continue;}
            x=nx,y=ny;
            if(!vis[x][y]) vis[x][y]=1,cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

|