Enmin @ 2024-08-05 17:53:37
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using str = string;
#define s(i) cin>>i
#define p(i) cout <<i
const int N = 1e3;
//升:priority_queue <int,vector<int>,greater<int>> qe;
//降:priority_queque <int,vector<int>,less<int>> qe;
int g[5] = {0,0,1,0,-1};
int h[5] = {0,1,0,-1,0};
int n,m,sum=1,tail=1,x[N],y[N];
char a[N][N];
bool admin() {
x[1]=1;
y[1]=1;
while(sum<=tail) {
for(int i=1;i<=4;i++) {
int t1 = x[sum] + g[i];
int t2 = y[sum] + h[i];
if(a[t1][t2]=='.') {
a[t1][t2] = '#';
tail++;
x[tail] = t1;
y[tail] = t2;
if(a[t1][t2] == a[n][m]) {
return 1;
}
}
}
sum++;
}
return 0;
}
int main() {
s(n);
s(m);
if(n==1&&m==1) {
p("Yes\n");
return 0;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
s(a[i][j]);
}
}
if(admin()) {
p("Yes\n");
}else {
p("No\n");
}
return 0;
}
by XQL67 @ 2024-08-05 18:00:38
@Enmin 建议用深搜
by Enmin @ 2024-08-05 20:33:20
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using str = string;
const int N = 1e3;
//升:priority_queue <int,vector<int>,greater<int>> qe;
//降:priority_queque <int,vector<int>,less<int>> qe;
int g[5] = {0,0,1,0,-1};
int h[5] = {0,1,0,-1,0};
int n,m,sum=1,tail=1,x[N],y[N];
char a[N][N],dd;
bool admin() {
x[1]=1;
y[1]=1;
while(sum<=tail) {
for(int i=1;i<=4;i++) {
int t1 = x[sum] + g[i];
int t2 = y[sum] + h[i];
if(a[t1][t2]=='.') {
a[t1][t2] = '#';
tail++;
x[tail] = t1;
y[tail] = t2;
if(a[t1][t2] == a[n][m]) {
return 1;
}
}
}
sum++;
}
return 0;
}
int main() {
scanf("%d%d%c",&n,&m,&dd);
for(int i=1;i<=n;i++) {
char s;
for(int j=1;j<=m;j++) {
scanf("%c",&a[i][j]);
}
scanf("%c",&s);
}
if(n==1&&m==1) {
printf("Yes\n");
return 0;
}
if(admin()) {
printf("Yes\n");
}else {
printf("No\n");
}
return 0;
}