站外题求助:还是一关
  • 板块学术版
  • 楼主Chalage_2010
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/4/23 22:30
  • 上次更新2023/10/23 17:40:25
查看原帖
站外题求助:还是一关
760690
Chalage_2010楼主2023/4/23 22:30

题目描述

定义一个二维数组:

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)
 

时间限制:
1000

 

空间限制:
65536


我的代码

#include<bits/stdc++.h>
using namespace std;
int head,tail;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int n,vis[6][6],a[6][6];
struct node{
	int x,y,step,pre;
}que[100005];
void f(int x)
{
	if(x==-1)
	{
		return ;
	}
	else
	{
		f(que[x].pre);
		printf("(%d, %d)\n",que[x].x,que[x].y);
	}
}
void bfs()
{
	que[++tail]=node{0,0,0,-1};
	vis[0][0]=1;
	while(tail>=head)
	{
		for(int i=0;i<4;i++)
		{
			int tx=que[head].x+dy[i];
			int ty=que[head].y+dy[i];
			if(tx>=0 and ty>=0 and tx<=5 and ty<=5 and vis[tx][ty]==0 and a[tx][ty]==0)
			{
				que[++tail]=node{tx,ty,que[head].step+1,head};
				vis[tx][ty]=1;
				if(tx==4 and ty==4)
				{
					f(tail);
					exit(0);
				}
			}
		}
		head++;
	}
}
int main()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			cin>>a[i][j];
		}
	}
	bfs();
}

请各位大佬看看有什么问题

2023/4/23 22:30
加载中...