50分
查看原帖
50分
1686377
ZhuYiTong楼主2025/7/1 22:00

TLE

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e2+10;
int n,m;
char c[maxn][maxn];
bool flag=false;
void dfs(int x,int y){
    if(flag){
        return;
    }
    if(x==n&&y==m){
        flag=true;
        return;
    }
    c[x][y] = '!';
    if(x+1 <= n && c[x+1][y]=='.'){
        dfs(x+1,y);
    }
    if(x-1 >= 1 && c[x-1][y]=='.'){
        dfs(x-1,y);
    }
    if(y+1 <= m && c[x][y+1]=='.'){
        dfs(x,y+1);
    }
    if(y-1 >= 1 && c[x][y-1]=='.'){
        dfs(x,y-1);
    }
    c[x][y] = '.';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){ 
            cin>>c[i][j];
        }
    }
    if(c[1][1] != '.' || c[n][m] != '.') {
        cout<<"No";
        return 0;
    }
    dfs(1,1);
    flag ? cout<<"Yes" : cout<<"No";
    return 0;
}
2025/7/1 22:00
加载中...