40分求助

B3625 迷宫寻路

light_searcher @ 2022-09-08 21:01:00

为什么不对呀?


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,vis[105][105];
    char a[105][105];
    int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
    queue<pair<int,int>>q;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    q.push(make_pair(1,1));
    vis[1][1]=1;
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        for(int i=0;i<4;i++){
            int mx=dx[i]+x,my=dy[i]+y;
            if(vis[mx][my]||!(mx>=1&&mx<=n&&my>=1&&my<=m)||a[mx][my]=='#') continue;
            if(mx==n&&my==m){
                printf("Yes");
                return 0;
            }           
            q.push(make_pair(mx,my));
            vis[mx][my]=1;
        }
    }
    printf("No");
    return 0;
} 

by goxjanskloon @ 2022-09-08 21:59:17

首先第七行那两个反折,编译器会把它识别成位右移,回报错.在中间打个空格


by hecc @ 2022-09-08 22:15:51

vis数组没有初始化为0


by hecc @ 2022-09-08 22:16:38

建议把数组开到外面


by hecc @ 2022-09-08 22:17:53

#include<bits/stdc++.h>
using namespace std;
int n,m,vis[105][105];
char a[105][105];
int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};
queue< pair<int,int> >q;
int main(){

    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    q.push(make_pair(1,1));
    vis[1][1]=1;
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        for(int i=0;i<4;i++){
            int mx=dx[i]+x,my=dy[i]+y;
            if(vis[mx][my]||!(mx>=1&&mx<=n&&my>=1&&my<=m)||a[mx][my]=='#') continue;
            if(mx==n&&my==m){
                printf("Yes");
                return 0;
            }           
            q.push(make_pair(mx,my));
            vis[mx][my]=1;
        }
    }
    printf("No");
    return 0;
}

像这样


by light_searcher @ 2022-09-09 06:48:17

@hecc 谢谢大佬,已经ac了


|