P1605 迷宫

#include 
using namespace std;
#define M 5
int arr[M + 5][M + 5];//棋盘
int n, m, t, sx, sy, fx, fy, ans = 0;
//四个方向封装成二维数组
int dir[4][2] = { {1, 0}, {0, -1}, {-1, 0}, {0, 1} };
void fun(int x, int y) {
	//边界条件
	if (x == fx && y == fy) {
		ans++;
		return;
	}
	//搜索下一步之前先锁住这个点,防止回到这个点
	arr[x][y] = 0;
	for (int i = 0; i < 4; i++) {
		int indx = x + dir[i][0], indy = y + dir[i][1];
		//障碍和边界都是0,下一步为1就递归下去
		if (arr[indx][indy]) fun(indx, indy);;
	}
	//出来之后,将这个点赋值为1,表示可以被访问
	arr[x][y] = 1;
	return;
}
int main() {
	cin >> n >> m >> t >> sx >> sy >> fx >> fy;
	//横纵都从1开始读入,迷宫周围自动全是0,相当于障碍
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			arr[i][j] = 1;
		}
	}
	for (int i = 0, a, b; i < t; i++) {
		cin >> a >> b;
		arr[a][b] = 0;
	}
	fun(sx, sy);
	cout << ans;
	return 0;
}

你可能感兴趣的:(算法题,算法,c++,数据结构)