感悟与总结
1、典型的深度优先搜索,记录路线条数一般是深度优先搜索。
2、没了,就这些。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10;
int n, m, t; //n为行,m为列,t为障碍总数
int sx, sy, fx, fy; //起点坐标sx,sy,终点坐标fx,fy。
int a[N][N]; // 地图
//上下左右
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int cnt;
//不能走回头路
bool st[N][N];
//深度优先搜索
void dfs(int x, int y) {
//如果到达终点,那么算是多了一组解,并return返回
if (x == fx && y == fy) {
cnt++;
return;
}
for (int i = 0; i < 4; i++) {
//可能走到哪个位置
int x1 = x + dx[i];
int y1 = y + dy[i];
//1、不出界
//2、没走过
//3、没有障碍物
if (x1 >= 1 && x1 <= n && y1 <= m && y1 >= 1 && !st[x1][y1] && !a[x1][y1]) {
st[x1][y1] = true;
dfs(x1, y1);
st[x1][y1] = false;
}
}
}
int main() {
//输入
cin >> n >> m >> t >> sx >> sy >> fx >> fy;
while (t--) {
int x, y;
cin >> x >> y;
a[x][y] = -1;
}
//出发点走过了
st[sx][sy] = true;
// 从sx,sy出发,想要到达fx,fy,中间有障碍,深度优先搜索
dfs(sx, sy);
//输出方案数
printf("%d", cnt);
return 0;
}