DFS和BFS问题
一、问题 2178: [信息学奥赛一本通-T1255]迷宫问题
时间限制: 1Sec 内存限制: 128MB 提交: 9 解决: 5
题目描述
定义一个二维数组:
int maze[5][5] = {
0,1,0,0,0,
0,1,0,1,0,
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
输入
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
输出
左上角到右下角的最短路径,格式如样例所示。
样例输入
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
在进行BFS时,记录其前一步的位置。
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
const int maxn=20+66;
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int n,m;
int a[maxn][maxn];
int pre[maxn][maxn];
struct node
{
int x,y;
};
bool vis[maxn][maxn];
void bfs(int x,int y)
{
queue<node>q;
stack<node>ans;
q.push(node{x,y});
vis[x][y]=1;
while(q.size())
{
node tmp=q.front();
q.pop();
for(int i=0; i<4; i++)
{
int tx=tmp.x+dx[i];
int ty=tmp.y+dy[i];
if(!vis[tx][ty]&&tx>=0&&ty>=0&&tx<n&&ty<m&&!a[tx][ty])
{
q.push(node{tx,ty,});
vis[tx][ty]=1;
pre[tx][ty]=(tmp.x)*m+tmp.y;
if(tx==n-1&&ty==m-1)
{
int px=tx,py=ty;
while(px!=-1&&py!=-1)
{
ans.push(node{px,py});
int tx=px;
px=(pre[px][py])/m;
py=(pre[tx][py])%m;
}
while(ans.size())
{
printf("(%d, %d)\n",ans.top().x,ans.top().y);
ans.pop();
}
return ;
}
}
}
}
}
int main()
{
n=5,m=5;
memset(pre,-1,sizeof(pre));
rep(i,0,n-1)
{
rep(j,0,m-1)
{
cin>>a[i][j];
}
}
bfs(0,0);
}
也可以用DFS来做
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#include<string.h>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn=22+6;
#define INF 0x3f3f3f
int dx[10]= {-1,1,0,0};
int dy[10]= {0,0,-1,1};
int n,m;
int a[maxn][maxn];
int pre[maxn][maxn];
struct node
{
int x,y;
};
bool vis[maxn][maxn];
stack<node>ans;
int sum;
int ok=0;
void dfs(int x,int y,int step)
{
if(step>=sum)return;
if(x==n-1&&y==m-1&&step<sum)
{
sum=step;
while(ans.size())
ans.pop();
sum=step+1;
int px=x,py=y;
while(px!=-1&&py!=-1)
{
ans.push(node{px,py});
int ttx=px;
px=(pre[px][py])/m;
py=(pre[ttx][py])%m;
}
return ;
}
for(int i=0; i<4; i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(!vis[tx][ty]&&tx>=0&&ty>=0&&tx<n&&ty<m&&!a[tx][ty])
{
vis[tx][ty]=1;
int tmpx=pre[tx][ty];
pre[tx][ty]=(x)*m+y;
dfs(tx,ty,step+1);
vis[tx][ty]=0;
swap(tmpx,pre[tx][ty]);
}
}
return ;
}
int main()
{
n=5,m=5;
sum=9999999;
memset(pre,-1,sizeof(pre));
rep(i,0,n-1)
{
rep(j,0,m-1)
{
cin>>a[i][j];
}
}
dfs(0,0,1);
while(ans.size())
{
printf("(%d, %d)\n",ans.top().x,ans.top().y);
ans.pop();
}
//cout<<sum<<endl;
return 0;
}
如果求起点到终点路径总数,代码如下
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#include<string.h>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
const int maxn=22+6;
#define INF 0x3f3f3f
int dx[10]= {-1,1,0,0};
int dy[10]= {0,0,-1,1};
int n,m;
int a[maxn][maxn];
int pre[maxn][maxn];
struct node
{
int x,y;
};
bool vis[maxn][maxn];
stack<node>ans;
int sum;
int ok=0;
void dfs(int x,int y,int step)
{
if(x==n-1&&y==m-1)
{
ok++;
return;
}
for(int i=0; i<4; i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(!vis[tx][ty]&&tx>=0&&ty>=0&&tx<n&&ty<m&&!a[tx][ty])
{
vis[tx][ty]=1;
dfs(tx,ty,step+1);
vis[tx][ty]=0;
}
}
return ;
}
int main()
{
n=5,m=5;
sum=9999999;
memset(pre,-1,sizeof(pre));
rep(i,0,n-1)
{
rep(j,0,m-1)
{
cin>>a[i][j];
}
}
dfs(0,0,1);
cout<<ok<<endl;
return 0;
}