走迷宫

mayijun123456 2023-11-18 9:24:49

#include <bits/stdc++.h> using namespace std; const int N = 10; char maze[N][N]; bool vis[N][N]; int n, m; int dfs(int x, int y){ if(x<1 || x>n || y<1 || y>m || maze[x][y]!='.' || vis[x][y]) return 0; if (x == n && y == m)return 1; vis[x][y] = true; int sum=dfs(x - 1,y) + dfs(x + 1,y) + dfs(x,y - 1) + dfs(x,y + 1); vis[x][y] = false; return sum; } int main() { cin >> n >> m; for (int i=1; i<=n; i++){ for (int j=1; j<=m; j++) cin >> maze[i][j]; } cout << dfs(1,1) << endl; return 0; }