序号:3148
迷宫
Time Limit:1s Memory Limit:125MB
题目描述
给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。
输入格式
第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX,SY,终点坐标FX,FY。接下来T行,每行为障碍点的坐标。
输出格式
输出仅包含一个整数,代表有多少种从起点坐标到终点坐标的可行方案。若没有可行方案则输出0。
输入输出样例
输入 #1
2 2 1
1 1 2 2
1 2
输出 #1
1
说明/提示
【数据规模】
1≤N,M≤5
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int mm[6][6];
bool temp[6][6];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
int total, n, m, t, fx, fy, sx, sy;
void search(int x, int y) {
if (x == fx && y == fy) {
total++;
return;
} else {
for (int i = 0 ; i <= 3 ; i++) {
if (temp[x + dx[i]][y + dy[i]] == 0 && mm[x + dx[i]][y + dy[i]] == 1) {
temp[x][y] = 1;
search(x + dx[i], y + dy[i]);
temp[x][y] = 0;
}
}
}
}
int main() {
cin >> n >> m >> t;
int l, r;
for (int ix = 1 ; ix <= n ; ix++) {
for (int iy = 1 ; iy <= n ; iy++) {
mm[ix][iy] = 1;
}
}
cin >> sx >> sy;
cin >> fx >> fy;
for (int u = 1 ; u <= t ; u++) {
cin >> l >> r;
mm[l][r] = 0;
}
search(sx, sy);
cout << total;
return 0;
}