hiYE_ame @ 2023-07-19 23:09:25
#include<iostream>
using namespace std;
char Map0[100][100];
char Map1[101][101];
int m,n;
int SouX=1,SouY=1;
int DesX,DesY;
int c=0;
int PathX[10000],PathY[10000];
bool Visited[100][100];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
void print(int);
void DFS(int x,int y,int step){
PathX[step]=x;
PathY[step]=y;
Visited[x][y]=true;
if(x==DesX && y==DesY){
c++;
}
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(Map1[nx][ny]=='.' && !Visited[nx][ny]){
DFS(nx,ny,step+1);
Visited[nx][ny]=false;
}
}
}
void ChangeMap(){
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
Map1[i][j]=Map0[i-1][j-1];
}
}
}
int main(){
cin>>m>>n;
DesX=m;
DesY=n;
for(int i=0;i<m;i++){
cin>>Map0[i];
}
ChangeMap();
DFS(SouX,SouY,1);
if(c==0){
cout<<"No";
}
else{
cout<<"Yes";
}
return 0;
}
by lucas_777 @ 2023-07-21 08:45:23
改亿点点就行了
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int n,m,a[200][200];
bool f = false;
char x;
int dx[5] = {0,-1,0,1,0},dy[5] = {0,0,-1,0,1};
void dfs(int x,int y){
if(x == n && y == m){
f = true;
return;
}
a[x][y] = 1;
for(int i = 1;i <= 4;++i){
int x2 = x + dx[i],y2 = y + dy[i];
if(x2 > 0 && x2 <= n && y > 0 && y <= m){
if(a[x2][y2] == 0){
dfs(x2,y2);
}
}
}
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= m;++j){
cin >> x;
if(x == '#'){
a[i][j] = 1;
}
}
if(a[1][1] == 1 || a[n][m] == 1){
cout << "No";
return 0;
}
}
dfs(1,1);
if(f){
cout << "Yes";
}else{
cout << "No";
}
return 0;
}