文章目录
- C++
- 总结
本题链接:I’m stuck!
本博客给出本题截图:
C++
#include <iostream> using namespace std; const int N = 60; int n, m; char g[N][N]; bool st1[N][N], st2[N][N]; int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; bool check(int x, int y, int k) { char c = g[x][y]; if (c == 'S' || c == 'T' || c == '+') return true; if (c == '-' && k % 2 == 1) return true; if (c == '|' && k % 2 == 0) return true; if (c == '.' && k == 2) return true; return false; } void dfs1(int x, int y) { st1[x][y] = true; for (int i = 0; i < 4; i ++ ) { int a = x + dx[i], b = y + dy[i]; if (a < 0 || a >= n || b < 0 || b >= m) continue; if (g[a][b] == '#') continue; if (st1[a][b]) continue; if (check(x, y, i)) dfs1(a, b); } } void dfs2(int x, int y) { st2[x][y] = true; for (int i = 0; i < 4; i ++ ) { int a = x + dx[i], b = y + dy[i]; if (a < 0 || a >= n || b < 0 || b >= m) continue; if (g[a][b] == '#') continue; if (st2[a][b]) continue; if (check(a, b, i ^ 2)) dfs2(a, b); } } int main() { cin >> n >> m; for (int i = 0; i < n; i ++ ) cin >> g[i]; int x, y; for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) if (g[i][j] == 'S') dfs1(i, j); else if (g[i][j] == 'T') { x = i, y = j; dfs2(i, j); } if (!st1[x][y]) puts("I'm stuck!"); else { int res = 0; for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) if (st1[i][j] && !st2[i][j]) res ++; cout << res << endl; } return 0; }
总结
st1数组为 true 表示可以从起点到达的点,st2数组为 true 代表可以从终点(倒着)可以到达的点,两个 dfs 分别对应两种的遍历情况,一种为正向遍历,另一个为反向遍历