原题链接:蓝桥杯算法提高VIP-学霸的迷宫
解题思路:
做笔记专用
注意事项:
做笔记专用,不喜勿喷
参考代码:
#include<iostream>
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;
int n,m;
char mp[501][501];
int book[501][501];
int rode[4][2]={1,0,0,-1,0,1,-1,0};
struct node
{
int x,y,step,dir[10000];
};
void bfs()
{
queue<node> q;
node now,next;
now.x=0;now.y=0;
now.step=0;now.dir[0]=-1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==n-1&&now.y==m-1)
{
cout<<now.step<<endl;
for(int i=1;i<=now.step;i++)
{
if(now.dir[i]==0) cout<<"D";
else if(now.dir[i]==1) cout<<"L";
else if(now.dir[i]==2) cout<<"R";
else if(now.dir[i]==3) cout<<"U";
}
return ;
}
for(int i=0;i<4;i++)
{
int tx=now.x+rode[i][0];
int ty=now.y+rode[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&book[tx][ty]==0&&mp[tx][ty]=='0')
{
book[tx][ty]=1;
next=now;
next.x=tx;
next.y=ty;
next.step=now.step+1;
next.dir[next.step]=i;
q.push(next);
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
scanf("%s",mp[i]);
}
memset(book,0,sizeof(book));
bfs();
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复