中文题意:求一个囚犯能逃出监狱(迷宫)的多少种走法,1表示有墙,0表示可以走(见下图)
Alfie was a prisoner in mythland. Though Alfie was a witty and intelligent guy.He was confident of escaping prison.After few days of observation,He figured out that the prison consists of (N×N) cells.i.e The shape of prison was (N×N) matrix. Few of the cells of the prison contained motion detectors.So Alfie planned that while escaping the prison he will avoid those cells containing motion detectors.Yet before executing his plan,Alfie wants to know the total number of unique possible paths which he can take to escape the prison.Initially Alfie is in cell
(1,1) while the exit of the cell (N,N).
note:->Alfie can move in all four direction{ if his current location is (X,Y), he can move to either
(X+1,Y), (X−1,Y), (X,Y+1), (X,Y−1) }. If the first cell (1,1) and the last cell(N,N) contain motion detectors,then Alfie can't break out of the prison.
INPUT:
The first line contain number of test cases "T".T test cases follow.The first line of each test case contains an integer "N",(i.e the size of the (N×N) matrix).The next n lines contain N space separated values either 0 or 1."1" represents a cell containing motion detectors.
OUTPUT:
output total number of unique possible paths which he can take to escape the prison.
Constraint:
1≤T≤20
1≤N≤20
3 4 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 4 0 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 4 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0
#include<vector>
#include<iostream>
using namespace std;
int adj[20][20];
int visited[20][20];
int n;
int counti = 0;
void dfs(int x, int y)
{
if (x == n&&y == n)
{
counti++;
return;
}
visited[x][y] = 1;
if (x + 1 <= n&&adj[x+1][y] == 0 && visited[x + 1][y] == 0)dfs(x + 1, y);
if (x - 1 >= 1 && adj[x-1][y] == 0 && visited[x - 1][y] == 0)dfs(x - 1, y);
if (y + 1 <= n&&adj[x][y+1] == 0 && visited[x][y + 1] == 0)dfs(x, y + 1);
if (y - 1 >= 1 && adj[x][y-1] == 0 && visited[x][y - 1] == 0)dfs(x, y - 1);
visited[x][y] = 0;
return;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cin >> adj[i][j];
visited[i][j] = 0;
}
}
counti = 0;
dfs(1, 1);
cout << counti << endl;
counti = 0;
}
return 0;
}