Baymax1


私信TA

用户名:Nerd

访问量:1909

签 名:

等  级
排  名 15990
经  验 773
参赛次数 1
文章发表 2
年  龄 21
在职情况 学生
学  校 五邑大学
专  业 计算机科学与技术

  自我简介:

解题思路:
    刚开始没想出到怎么记录路径。。。看了下别人的,用个结构体,在结构体中用string记录路径就好。

其他的也没啥可说的。纯bfs,水题
注意事项:

参考代码:

#include <bits/stdc++.h>	//[蓝桥杯][算法提高VIP]学霸的迷宫 
using namespace std;
const int maxn=500;
int dire[4][2]={{1,0},{0,-1},{0,1},{-1,0}};	//按照DLRU的顺序进行搜索
char dir[4]={'D','L','R','U'}; 
int n,m;
string map1[maxn];
int vis[maxn][maxn];
struct node{
	int x,y;
	string r;	//表示路径 
}; 
void bfs()
{
	queue<node> way;
	node cur;	cur.x=0;	cur.y=0;	cur.r="";
	way.push(cur);
	vis[cur.x][cur.y]=1;
	node l,k;
	while(!way.empty())
	{
		l=way.front();
		way.pop();
		if(l.x==(n-1) && l.y==(m-1))
			break;
		int tx,ty;
		for(int i=0;i<4;i++)
		{
			tx=l.x+dire[i][0];	ty=l.y+dire[i][1];
			if(tx>=0 && tx<n && ty>=0 && ty<m && map1[tx][ty]=='0' && !vis[tx][ty])	//刚开始想着应该不用标记的... 
			//果然超内存... 
			{
				k.x=tx;	k.y=ty;	k.r=l.r+dir[i];
				way.push(k);
				vis[tx][ty]=1;
			}
		}
	}
	cout<<l.r.length()<<"\n"<<l.r;
	return ;
}
int main()
{
	scanf("%d %d",&n,&m);
	getchar();
	for(int i=0;i<n;i++)
		getline(cin,map1[i]);
	memset(vis,0,sizeof(vis));
	bfs();
	return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区