LichKing_Arthas @ 2023-10-17 20:40:18
测试点信息
#include<bits/stdc++.h>
using namespace std;
string ma[100];
int n,m,f;
int l[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
bool dfs(int x,int y){
if(x==n && y==m) { cout<<"No";f=1; }
for(int i=0;i<4;i++){
int xx=x+l[i][0];
int yy=y+l[i][1];
if(ma[xx][yy]=='.' && xx>0 && xx<=n && yy>0 && yy<=m){
ma[xx][yy]='#';
dfs(xx,yy);
ma[xx][yy]='.';
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>ma[i];
ma[1][1]='#';
dfs(1,1);
if(f!=1) cout<<"Yes";
return 0;
}
by LichKing_Arthas @ 2023-10-17 20:47:52
改后变成了下面这样子
#include<bits/stdc++.h>
using namespace std;
char ma[105][105];
int n,m,f;
int l[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
bool dfs(int x,int y){
if(x==n && y==m) { cout<<"Yes";exit(0); }
for(int i=0;i<4;i++){
int xx=x+l[i][0];
int yy=y+l[i][1];
if(ma[xx][yy]=='.' && xx>0 && xx<=n && yy>0 && yy<=m){
ma[xx][yy]='#';
dfs(xx,yy);
ma[xx][yy]='.';
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>ma[i][j];
ma[1][1]='#';
dfs(1,1);
cout<<"No";
return 0;
}
by __wjy__ @ 2023-11-03 18:51:22
先给你代码,不懂的说
#include<iostream>
using namespace std;
int n,m,vis[2000][2000],map[200][200],flag;
char g[200][200];
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
void dfs(int x,int y){
if(x == n && y == m){
cout<<"Yes";
flag = 1;
return;
}
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(vis[xx][yy] == 0 && map[xx][yy] == 0 && xx >= 1 && xx <= n && yy >= 1 && yy <= m){
vis[xx][yy] = 1;
dfs(xx,yy);
}
}
}
int main(){
cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>g[i][j];
if(g[i][j] == '.'){
map[i][j] = 0;
}
if(g[i][j] == '#'){
map[i][j] = 1;
}
}
}
dfs(1,1);
if(!flag){
cout<<"No";
}
return 0;
}
by __wjy__ @ 2023-11-03 18:52:30
@MC_dai要一个状态数组vis
by __wjy__ @ 2023-11-03 18:52:45
@MC_dai