您的位置:首页 > 其它

走迷宫(dfs)

2013-10-12 16:36 253 查看
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2449

#include<stdio.h>
#include<string.h>
const int N=520;
int vis

,map

;
int n,m,cnt;
void dfs(int x,int y)
{
if (x==n&&y==m)
{
cnt++;
return ;
}
if (x-1>=1&&map[x-1][y]==0&&!vis[x-1][y])
{
vis[x][y] = 1;
dfs(x-1,y);
vis[x][y] = 0;
}
if (x+1<=n&&map[x+1][y]==0&&!vis[x+1][y])
{
vis[x][y] = 1;
dfs(x+1,y);
vis[x][y] = 0;
}
if (y-1>=1&&map[x][y-1]==0&&!vis[x][y-1])
{
vis[x][y] = 1;
dfs(x,y-1);
vis[x][y] = 0;
}
if (y+1<=m&&map[x][y+1]==0&&!vis[x][y+1])
{
vis[x][y] = 1;
dfs(x,y+1);
vis[x][y] = 0;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cnt = 0;
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++ )
{
scanf("%d",&map[i][j]);
}
}
vis[1][1] = 1;
dfs(1,1);
printf("%d\n",cnt);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: