迷宫
#include<iostream>using namespace std;
char f;
bool vis;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m;
// 在 (x,y) 这个位置,走到重点的方案数
int dfs(int x, int y) {
if (f == 'e') {
return 1;
}
int cnt = 0;
for(int i = 0; i < 4; i++) {
int tx = x + dx;
int ty = y + dy;
if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis && f != '#') {
vis = 1;
cnt += dfs(tx, ty);
vis = 0;
}
}
return cnt;
}
int main() {
int x, y;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> f;
for (int j = 0; j < m; j++) {
if (f == 's') {
x = i;
y = j;
}
}
}
vis = 1;
cout << dfs(x, y) << endl;
return 0;
}
页:
[1]