迷宫问题

#include<bits/stdc++.h>
using namespace std;
int a,ans=0,c[105][105];
char b[105][105];

int p(int x,int y)
{
	if(x==1&&y==a)
	{
		ans++;
		return 1;
	}
	bool sf=0;
	if(b[x-1][y]=='0'&&!c[x-1][y]) c[x-1][y]=1,p(x-1,y),sf=1,c[x-1][y]=0;
	if(b[x-1][y+1]=='0'&&!c[x-1][y+1]) c[x-1][y+1]=1,p(x-1,y+1),sf=1,c[x-1][y+1]=0;
	if(b[x-1][y-1]=='0'&&!c[x-1][y-1]) c[x-1][y-1]=1,p(x-1,y-1),sf=1,c[x-1][y-1]=0;
	if(b[x][y+1]=='0'&&!c[x][y+1]) c[x][y+1]=1,p(x,y+1),sf=1,c[x][y+1]=0;
	if(b[x][y-1]=='0'&&!c[x][y-1]) c[x][y-1]=1,p(x,y-1),sf=1,c[x][y-1]=0;
	if(b[x+1][y]=='0'&&!c[x+1][y]) c[x+1][y]=1,p(x+1,y),sf=1,c[x+1][y]=0;
	if(b[x+1][y+1]=='0'&&!c[x+1][y+1]) c[x+1][y+1]=1,p(x+1,y+1),sf=1,c[x+1][y+1]=0;
	if(b[x+1][y-1]=='0'&&!c[x+1][y-1]) c[x+1][y-1]=1,p(x+1,y-1),sf=1,c[x+1][y-1]=0;
	
	return 0;
}

int main()
{
	cin>>a;
	c[1][1]=1;
	for(int i=1;i<=a;i++)
		for(int j=1;j<=a;j++) cin>>b[i][j];
	p(1,1);
	cout<<ans;
	return 0;
}
posted @ 2018-07-09 13:41  wbss  阅读(199)  评论(0编辑  收藏  举报