定义一个二维数组:
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
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
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
#include <queue>
#include <iostream>
using namespace std;
int maze[7][7], pre[30], vis[30];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool isLeagal ( int r, int c )
{
if ( r < 0 || c < 0 || r > 4 || c > 4 )
return false;
if ( maze[r][c] == 1 )
return false;
return true;
}
void print ( int des )
{
if ( pre[des] != -1 )
print ( pre[des] );
cout << '(' << des / 5 << ", " << des % 5 << ')' << endl;
}
void bfs ()
{
queue<int> que;
memset(vis,0,sizeof(vis));
pre[0] = -1;
vis[0] = true;
que.push ( 0 );
int now, next;
int r, c, tr, tc;
while ( ! que.empty () )
{
now = que.front();
que.pop();
r = now / 5;
c = now % 5;
for ( int i = 0; i < 4; i++ )
{
tr = r + dir[i][0];
tc = c + dir[i][1];
next = tr * 5 + tc;
if ( isLeagal ( tr, tc ) && !vis[next] )
{
pre[next] = now;
if ( next == 24 ) return;
vis[next] = true;
que.push(next);
}
}
}
}
int main()
{
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 5; j++ )
cin >> maze[i][j];
bfs();
print(24);
return 0;
}
<pre name="code" class="cpp">#include<iostream>
using namespace std;
#define QSize 50
int a[5][5];
//把迷宫想象成x轴,y轴
//int dx[4]={-1,1,0,0};//x轴方向上的变化
//int dy[4]={0,0,-1,1};//y轴方向上的变化
int dis[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//定义4个方向
struct Node{
int x,y,pre;
}queue[QSize];//设置一个50个格子的队列
int front=0;
int rear=0;//设置队头和队尾,头指针指向头元素,尾指针指向队尾的下一个位置
int visit[5][5];//记录是否访问过的数组
//广度优先遍历
void bfs(int beginX,int beginY,int endX,int endY){
queue[0].x=beginX,queue[0].y=beginY,queue[0].pre=-1;//将初始结点[0,0]压入队列
rear=rear+1;
visit[beginX][beginY]=1;
while(front<rear){//如果队列不为空
for(int i=0;i<4;i++){//4个方向搜索可达的方向
int newx=queue[front].x+dis[i][0];
int newy=queue[front].y+dis[i][1];
if(newx<0||newx>5||newy<0||newy>5||a[newx][newy]==1||visit[newx][newy]==1)//是否在迷宫内,是否撞墙,是否已走过
continue;
//进队
queue[rear].x=newx;
queue[rear].y=newy;
queue[rear].pre=front;
rear++;
visit[newx][newy]=1;//给走过的位置做标记
if(newx==endX&&newy==endY){
return;
}
}
front++;//出队
}
}
void print(Node now){
if(now.pre==-1)
cout<<"("<<now.x<<", "<<now.y<<")"<<endl;
else{
print(queue[now.pre]);
cout<<"("<<now.x<<", "<<now.y<<")"<<endl;
}
}
int main(){
//初始化迷宫
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>a[i][j];
}
}
bfs(0,0,4,4);
print(queue[rear-1]);
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
int i,j,ff,num,front,a[5][5],b[5][5],c[5][5];
stack<int>x;
stack<int>y;
void dfs(int i,int j)//这里函数名命名错了,但是不影响程序运行。。。
{
if(a[i][j]==1||i<0||j<0||i>=5||j>=5||b[i][j]==1) return ;
if(c[i][j]==0&&a[i][j]==0&&b[i][j]==0)
{
c[i][j]=ff;
b[i][j]=1;
ff++;
}
dfs(i,j+1);
dfs(i+1,j);
dfs(i-1,j);
dfs(i,j-1);
}
int main()
{
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
cin>>a[i][j];
b[i][j]=0;
c[i][j]=0;
}
ff=1;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==0)
{dfs(i,j);}
}
}
front = c[4][4];
int fu=front;
for(num=fu-1;num>0;num--)
{
for(i=4;i>=0;i--)
{
for(j=4;j>=0;j--)
{
if(c[i][j]==front-1&&c[i][j]!=0)
{
x.push(i);
y.push(j);
front=c[i][j];
}
}
}
}
while(!x.empty()&&!y.empty())
{
cout<<"("<<x.top()<<", "<<y.top()<<")\n";
x.pop();y.pop();
}
cout<<"("<<4<<", "<<4<<")\n";
return 0;
}