#include <bits/stdc++.h>
#define mem(arr, num) memset(arr, num, sizeof arr)
#define F first
#define S second
#define INF 0x3f3f3f3f
using namespace std;
const int N = 10;
int sx, sy, ex, ey, n, m, t, ans;
int g[N][N], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dfs(int x, int y)
{
if (x == ex && y == ey)
{
ans ++;
return;
}
for (int i = 0; i < 4; i ++ )
{
int xx = x + dx[i], yy = y + dy[i];
if (xx > 0 && xx <= n && yy > 0 && yy <= m && !g[xx][yy])
{
g[xx][yy] = 1;
dfs(xx, yy);
g[xx][yy] = 0;
}
}
}
int main()
{
cin.tie(0), cout.tie(0);
cin >> n >> m >> t >> sx >> sy >> ex >> ey;
while (t -- )
{
int a, b;
cin >> a >> b;
g[a][b] = 1;
}
dfs(sx, sy);
cout << ans << endl;
return 0;
}
最后才发现是没有对开始点初始化
#include <bits/stdc++.h>
#define mem(arr, num) memset(arr, num, sizeof arr)
#define F first
#define S second
#define INF 0x3f3f3f3f
using namespace std;
const int N = 10;
int sx, sy, ex, ey, n, m, t, ans;
int g[N][N], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dfs(int x, int y)
{
if (x == ex && y == ey)
{
ans ++;
return;
}
for (int i = 0; i < 4; i ++ )
{
int xx = x + dx[i], yy = y + dy[i];
if (xx > 0 && xx <= n && yy > 0 && yy <= m && !g[xx][yy])
{
g[xx][yy] = 1;
dfs(xx, yy);
g[xx][yy] = 0;
}
}
}
int main()
{
cin.tie(0), cout.tie(0);
cin >> n >> m >> t >> sx >> sy >> ex >> ey;
g[sx][sy] = 1;
while (t -- )
{
int a, b;
cin >> a >> b;
g[a][b] = 1;
}
dfs(sx, sy);
cout << ans << endl;
return 0;
}
这是过了的代码,记录与此,引以为戒;