题目大意:
给定一个N×M 方格的迷宫,迷宫里有T处障碍,障碍处不可通过。
在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。
给定起点坐标和终点坐标,每个方格最多经过一次,问有多少种从起点坐标到终点坐标的方案。
思路:
首先定义方向数组,然后写一个check函数,判断边界条件
然后dfs函数里面先判断如果x == bx && y == by,方法加一
然后退出,如果没有,则调用check函数,判断边界条件
然后清空标记数组
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
bool a[N][N], vis[N][N];
int ax, ay, bx, by;
int n, m, t;
int ans = 0;
bool check(int x, int y) {
return (x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]&&vis[x][y]==0);
}
void dfs(int x, int y) { //(x,y)
if (x == bx && y == by) {
ans++;
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (check(tx, ty)) dfs(tx, ty); //(x,y)->(tx,ty)
}
vis[x][y] = 0;
}
int main() {
scanf("%d%d%d", &n, &m, &t);
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
int x, y;
for (int i = 1; i <= t; i++) {
scanf("%d%d", &x, &y);
a[x][y] = true;
}
dfs(ax, ay);
printf("%d\n", ans);
return 0;
}