40分求助QWQ

B3625 迷宫寻路

XIAOHAOYU1 @ 2024-09-29 19:27:29

代码:

#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int v[105][105];
int n,m;
void dfs(int x,int y){
    if(x==n&&m==y&&a[x][y]!='#'){
        cout<<"Yes";
        exit(0);
    }
    if(v[x][y]==0){
        v[x][y]=1;
        if(a[x+1][y]=='.'&&!v[x+1][y]) dfs(x+1,y);
        if(a[x-1][y]=='.'&&!v[x-1][y]) dfs(x-1,y);
        if(a[x][y+1]=='.'&&!v[x][y+1]) dfs(x,y+1);
        if(a[x][y-1]=='.'&&!v[x][y-1]) dfs(x,y-1);
        v[x][y]=0;
    }
    if(a[x+1][y]=='#'||v[x+1][y]==1&&a[x-1][y]=='#'||v[x-1][y]==1&&
    a[x][y+1]=='#'||v[x][y+1]==1&&a[x][y-1]=='#'||v[x][y-1]==1){
        cout<<"No";
        exit(0);
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) cin>>a[i][j]; 
    }
    if(a[n][m]=='#'){
        cout<<"No";
        return 0;
    }
    dfs(1,1);
    return 0;
}

哪里错了啊啊啊啊啊

TIPS: WA:234579 https://www.luogu.com.cn/record/179006135


by XIAOHAOYU1 @ 2024-09-29 19:28:18

啊哈哈地址发错了 惨案现场


by fire_hua @ 2024-11-26 18:03:12

if(a[x+1][y]=='#'||v[x+1][y]==1&&a[x-1][y]=='#'||v[x-1][y]==1&&a[x][y+1]=='#'||v[x][y+1]==1&&a[x][y-1]=='#'||v[x][y-1]==1){
    cout<<"No";
        exit(0);
    }

这个成效应该不大,如果周围都不能走的话应该不存在吧

if(v[x][y]==0){
        v[x][y]=1;
        if(a[x+1][y]=='.'&&!v[x+1][y]) dfs(x+1,y);
        if(a[x-1][y]=='.'&&!v[x-1][y]) dfs(x-1,y);
        if(a[x][y+1]=='.'&&!v[x][y+1]) dfs(x,y+1);
        if(a[x][y-1]=='.'&&!v[x][y-1]) dfs(x,y-1);
        v[x][y]=0;
    }

可以试试把最后那个v[x][y]=0;删去


|