jiangbaolin @ 2024-05-29 17:38:06
#include<bits/stdc++.h>
using namespace std;
long long a[10001][10001],k=1,n,m,s,plasx[4]={0,1,0,-1},plasy[4]={1,0,-1,0};
bool tf=false;
char c;
void go(int x,int y){
if(tf){
return;
}
if(x==n&&y==m){
tf=true;
return;
}
else{
for(int i=0;i<=3;i++){
if(a[x+plasx[i]][y+plasy[i]]==1){
a[x][y]=0;
go(x+plasx[i],y+plasy[i]);
a[x][y]=1;
}
}
}
}
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> c;
if(c=='.'){
a[i][j]=1;
}
else{
a[i][j]=0;
}
}
}
go(1,1);
if(tf==true){
cout << "Yes";
}
else{
cout << "No";
}
return 0;
}
by CaoSheng_zzz @ 2024-05-29 18:39:32
@jiangbaolin 你是不是 TLE 了
by CaoSheng_zzz @ 2024-05-29 18:43:40
新加一个
Code:
#include<bits/stdc++.h>
using namespace std;
long long a[10001][10001],k=1,n,m,s,plasx[4]={0,1,0,-1},plasy[4]={1,0,-1,0},vis[10001][10001];
bool tf=false;
char c;
void go(int x,int y){
vis[x][y] = 1;
if(tf){
return;
}
if(x==n&&y==m){
tf=true;
return;
}
else{
for(int i=0;i<=3;i++){
if(vis[x+plasx[i]][y+plasy[i]] == 1) continue;
if(a[x+plasx[i]][y+plasy[i]]==1){
a[x][y]=0;
go(x+plasx[i],y+plasy[i]);
a[x][y]=1;
}
}
}
}
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> c;
if(c=='.'){
a[i][j]=1;
}
else{
a[i][j]=0;
}
}
}
go(1,1);
if(tf==true){
cout << "Yes";
}
else{
cout << "No";
}
return 0;
}
by jiangbaolin @ 2024-05-30 16:45:47
谢谢
by jiangbaolin @ 2024-05-30 16:46:50
已关注