普通BFS
/**
建议配合P0422食用
*/
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
constexpr int maxn = 50;
char g[maxn][maxn];
bool st[maxn][maxn];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int n, m;
struct node{
int x, y, cnt;
node(int _x, int _y, int _cnt) : x(_x), y(_y), cnt(_cnt) {}
};
int bfs(int sx, int sy, int ex, int ey) {
queue<node> q;
q.push({sx, sy, 1});
while (!q.empty()) {
node t = q.front();
q.pop();
if (t.x == ex && t.y == ey) return t.cnt;
for(int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue;
if (st[x][y] || g[x][y] == '#') continue;
st[x][y] = true;
q.push({x, y, t.cnt + 1});
}
}return -1;
}signed main(signed argc, char *argv[], char *env[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int sx, sy, ex, ey;
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> g[i][j];
if (g[i][j] == 'S') {
sx = i, sy = j;
}if (g[i][j] == 'T') {
ex = i, ey = j;
}
}
}cout << bfs(sx, sy, ex, ey);
return 0;
}
普通DFS
/**
建议配合P0802食用
*/
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
constexpr int maxn = 1e2 + 10;
char g[maxn][maxn];
bool st[maxn][maxn];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
struct node{
int x, y;
node(int _x, int _y) : x(_x), y(_y) {}
};
bool dfs(int x, int y) {
if (g[x][y] == 'T') return true;
for(int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
if (g[xx][yy] == '*' || st[xx][yy]) continue;
st[xx][yy] = true;
if (dfs(xx, yy)) return true;
st[xx][yy] = false;
}return false;
}signed main(signed argc, char *argv[], char *env[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int sx, sy;
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> g[i][j];
if (g[i][j] == 'S') {
sx = i, sy = j;
}
}
}cout << (dfs(sx, sy) ? "yes" : "no");
return 0;
}
BFS版DFS
/**
建议配合P0802食用
*/
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
constexpr int maxn = 20;
char g[maxn][maxn];
bool visited[maxn][maxn];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
struct node{
int x, y;
node(int _x, int _y) : x(_x), y(_y) {}
};
bool dfs(int sx, int sy, int ex, int ey) {
stack<node> st;
visited[sx][sy] = true;
st.push({sx, sy});
while (!st.empty()) {
node t = st.top();
st.pop();
if (t.x == ex && t.y == ey) return true;
for(int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m) continue;
if (visited[x][y] || g[x][y] == '*') continue;
visited[x][y] = true;
st.push({x, y});
}
}return false;
}signed main(signed argc, char *argv[], char *env[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int sx, sy, ex, ey;
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> g[i][j];
if (g[i][j] == 'S') {
sx = i, sy = j;
}if (g[i][j] == 'T') {
ex = i, ey = j;
}
}
}cout << (dfs(sx, sy, ex, ey) ? "yes" : "no");
return 0;
}