Prison Break

中文题意:求一个囚犯能逃出监狱(迷宫)的多少种走法,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×NN×N) cells.i.e The shape of prison was (N×NN×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,11,1) while the exit of the cell (N,NN,N).

note:->Alfie can move in all four direction{ if his current location is (X,YX,Y), he can move to either 
(X+1,YX+1,Y)(X1,YX−1,Y)(X,Y+1X,Y+1)(X,Y1X,Y−1) }. If the first cell (1,11,1) and the last cell(N,NN,N) contain motion detectors,then Alfie can't break out of the prison.

INPUT:

The first line contain number of test cases "TT".TT test cases follow.The first line of each test case contains an integer "NN",(i.e the size of the (N×NN×N) matrix).The next nn lines contain NN space separated values either 00 or 11."11" represents a cell containing motion detectors.

OUTPUT:

output total number of unique possible paths which he can take to escape the prison.

Constraint:

1T201≤T≤20

1N201≤N≤20

SAMPLE INPUT
 
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 
 


SAMPLE OUTPUT
 
2
4
4
Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值