destiny_w @ 2024-10-22 21:06:54
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int a[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
string ans[4]={"Yes","No"};
int vis[N][N];
int n , m;
int f(int x,int y){
//cout<<x<<" "<<y<<endl;
if(x==n && y==m){
return 1;
}
int flag=0;
for(int i=0;i<4;++i){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>=0 && xx<=n && yy>=0 && yy<=m && a[xx][yy]==1 && vis[xx][xx]==0){
vis[xx][yy]=1;
f(xx,yy);
vis[xx][yy]=0;
}
}
return 0;
}
int main(){
cin >> n >> m;
for(int i = 1 ; i <= n ; ++ i) {
for(int j = 1 ; j <= m ; ++ j) {
char s;
cin >> s;
if(s == '.') {
a[i][j] = 1;
}
else {
a[i][j] = 0;
}
}
}
cout<<ans[f(1,1)];
}
by yuechenxi130407 @ 2024-10-22 21:22:21
#include <bits/stdc++.h>
using namespace std;
char a[101][101];
int n,m,ans,vis[101][101];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
void DFS(int x,int y){
if (x==n && y==m){
ans=1;
return;
}
if (x<1 || y<1 || x>n || y>m || vis[x][y] || a[x][y]=='#') return;
vis[x][y]=1;
for (int i=0;i<4;++i){
int xx=x+dx[i];
int yy=y+dy[i];
DFS(xx,yy);
}
}
int main(){
cin>>n>>m;
for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) cin>>a[i][j];
DFS(1,1);
if (ans) cout<<"Yes";
else cout<<"No";
return 0;
}