题目背景
给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。
题目描述
无
输入格式
第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX,SY,终点坐标FX,FY。接下来T行,每行为障碍点的坐标。
输出格式
给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方案总数。
输入输出样例
输入 #1复制
2 2 1 1 1 2 2 1 2
输出 #1复制
1
说明/提示
【数据规模】
1≤N,M≤5
深度优先搜索
#include<iostream>
#include<vector>
#include<climits>
#include<queue>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int t[7][7];
int b[7][7];
int N, M, T, sx, sy, fx, fy;
int c[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
void dsf(int &count,int p,int q)
{
if (p == fx && q == fy)
count++;
int x = p, y = q;
for (int i = 0; i < 4; i++)
{
x = x + c[i][0];
y = y + c[i][1];
if (b[x][y] == 1 || t[x][y] == 1 || (x > N) || (x < 1) || y > M || y < 1)
{
x = x - c[i][0];
y = y - c[i][1];
continue;
}
else
b[p][q] = 1;
dsf(count, x, y);
b[p][q] = 0;
x -= c[i][0];
y -= c[i][1];
}
}
int main()
{
cin >> N >> M >> T >> sx >> sy >> fx >> fy;
for (int i = 1; i <= T ; i++)
{
int h, k;
cin >> h >> k;
t[h][k] = 1;
}
int count = 0;
b[sx][sy] = 1;
dsf(count,sx,sy);
cout << count;
//system("pause");
return 0;
}