深度优先搜索
洛谷P1605
可先看我的另一个博客
题目简单直接附代码了
#include<bits/stdc++.h>
using namespace std;
int n, m, t;
int x_0, y_0, xfinal, yfinal, ans = 0, flag[10][10] = {0};
int d[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}};
bool check(int x, int y) {
if(x > n || y > m || x <= 0 || y <= 0) return false;
if(flag[x][y]) return false;
return true;
}
void dfs(int x, int y) {
if(x == xfinal && y == yfinal) ans++;
else {
for(int i = 0; i < 4; i++) {
if(check(x+d[i][0], y+d[i][1])) {
flag[x][y] = 1;
dfs(x+d[i][0], y+d[i][1]);
flag[x][y] = 0;
}
}
}
}
int main() {
cin >> n >> m >> t;
cin >> x_0 >> y_0 >> xfinal >> yfinal;
for(int i = 0; i < t; i++) {
int x, y;
cin >> x >> y;
flag[x][y] = 1;
}
dfs(x_0, y_0);
cout << ans;
system("pause");
}