Files
python/TangDou/LuoGuBook/P1605.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

55 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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[] = {0, 0, -1, 1};
int dy[] = {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;
}