设有一个 n*n 方格的迷宫,入口和出口分别在左上角和右上角。迷宫格子中分别放 0 和 1 ,0 表示可通,1 表示不能,入口和出口处肯定是 0。迷宫走的规则如下所示:即从某点开始,有八个方向可走,前进方格中数字为 0 时表示可通过,为 1 时表示不可通过,要另找路径。找出所有从入口(左上角)到出口(右上角)的路径(不能重复),输出路径总数,如果无法到达,则输出 0。
共 n+1 行;第一行位正整数 n,表示迷宫的行数及列数;接下来 n 行,每行 n 个数,表示对应的格子是否可以通过。
路径总数。
3
0 0 0
0 1 1
1 0 0
2
【数据说明】
100%数据满足:2<=n<10。
#include
using namespace std;
int n, s;
bool b[13][13];
const int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1};
void go(int x, int y){
if(x == 1 && y == n){
s++;
return;
}
int xx, yy;
for(int i = 0; i <= 7; i++){
xx = x + dx[i];
yy = y + dy[i];
if(b[xx][yy]){
b[xx][yy] = false;
go(xx, yy);
b[xx][yy] = true;
}
}
}
int main(){
int x;
cin >> n;
memset(b, 0, sizeof(b));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> x;
b[i][j] = (x == 0);
}
}
s = 0;
b[1][1] = false;
go(1, 1);
cout << s << endl;
return 0;
}