(壶关)(回复请@我)WA 0求调,在线等急

P11228 [CSP-J 2024] 地图探险

programmer330 @ 2024-10-26 21:34:53

#include<bits/stdc++.h>
using namespace std;
int t;
int n,m,k,d,xx,yy,dd;
int x,y;
int a[1001][1001];
bool check[1001][1001];
int main(){
    cin>>t;
    for(int T=1;T<=t;T++){
        cin>>n>>m>>k>>xx>>yy>>dd;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>a[i][j];
                check[i][j]=false;
            }
        }
        d=dd;
        x=xx;
        y=yy;
        check[x][y]=true;
        for(int i=1;i<=k;i++){
            if(d==0){
                if (a[x][y+1]=='.'&&y+1<=m){
                    y+=1;
                    check[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            if(d==1){
                if (a[x+1][y]=='.'&&x+1<=n){
                    x+=1;
                    check[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }

            if(d==2){
                if (a[x][y-1]=='.'&&y-1>0){
                    y-=1;
                    check[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            if(d==3){
                if (a[x-1][y]=='.'&&x-1>0){
                    x-=1;
                    check[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
        }
        int sum=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(check[i][j]==true)sum+=1;
            }
        }
        cout<<sum<<endl;
    }

    return 0;
}

by yanghaoyu123 @ 2024-10-26 21:37:48

@programmer330 没判断是否走过这个地方


by programmer330 @ 2024-10-26 21:48:30

@yanghaoyu123

WA 0

#include<bits/stdc++.h>
using namespace std;
int t;
int n,m,k,d,xx,yy,dd;
int x,y;
int a[1001][1001];
bool check[1001][1001];
bool again[1001][1001];
int main(){
    cin>>t;
    for(int T=1;T<=t;T++){
        cin>>n>>m>>k>>xx>>yy>>dd;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>a[i][j];
                check[i][j]=false;
                again[i][j]=false;
            }
        }
        d=dd;
        x=xx;
        y=yy;
        check[x][y]=true;
        int sum=0;
        for(int i=1;i<=k;i++){
            if(d==0){
                if (a[x][y+1]=='.'&&y+1<=m){
                    y+=1;
                    if(again[x][y]=true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            else if(d==1){
                if (a[x+1][y]=='.'&&x+1<=n){
                    x+=1;
                    if(again[x][y]=true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }

            else if(d==2){
                if (a[x][y-1]=='.'&&y-1>0){
                    y-=1;
                    if(again[x][y]=true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            else if(d==3){
                if (a[x-1][y]=='.'&&x-1>0){
                    x-=1;
                    if(again[x][y]=true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(check[i][j]==true)sum+=1;
            }
        }
        cout<<sum<<endl;
    }

    return 0;
}

by yanghaoyu123 @ 2024-10-26 21:49:32

@programmer330 绿名蒟蒻看不出来


by Luyutao2023 @ 2024-10-26 21:54:18

@programmer330 在更新路径信息时,if (again[x][y] = true) 中的赋值操作应为比较操作,导致逻辑错误。应使用 == 来比较是否已访问过。


by programmer330 @ 2024-10-26 22:12:42

@Luyutao2023

WA 0

#include<bits/stdc++.h>
using namespace std;
int t;
int n,m,k,d,xx,yy,dd;
int x,y;
int a[1001][1001];
bool check[1001][1001];
bool again[1001][1001];
int main(){
    cin>>t;
    for(int T=1;T<=t;T++){
        cin>>n>>m>>k>>xx>>yy>>dd;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>a[i][j];
                check[i][j]=false;
                again[i][j]=false;
            }
        }
        d=dd;
        x=xx;
        y=yy;
        check[x][y]=true;
        int sum=0;
        for(int i=1;i<=k;i++){
            if(d==0){
                if (a[x][y+1]=='.'&&y+1<=m){
                    y+=1;
                    if(again[x][y]==true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            else if(d==1){
                if (a[x+1][y]=='.'&&x+1<=n){
                    x+=1;
                    if(again[x][y]==true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }

            else if(d==2){
                if (a[x][y-1]=='.'&&y-1>0){
                    y-=1;
                    if(again[x][y]==true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
            else if(d==3){
                if (a[x-1][y]=='.'&&x-1>0){
                    x-=1;
                    if(again[x][y]==true)sum-=1;
                    check[x][y]=true;
                    again[x][y]=true;
                }else{
                    d=(d+1)%4;
                }
            }
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(check[i][j]==true)sum+=1;
            }
        }
        cout<<sum<<endl;
    }

    return 0;
}

|