第十届蓝桥杯E.迷宫

思路:主要是不知道该如何存储路径,如下可以用一个struct,在造一个构造函数,这样用node(1,2,"abc")就相当于 node point; point.x=1; point.y=2; point.path="abc";

struct node{
    int x,y;
    string path;
    node(int _x,int _y, string _path){
        x=_x;
        y=_y;
        path=_path;
    }
    node() {}  //可以直接初始化 
};

AC code:

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;

struct node{
	int x,y;
	string path;
	node(int _x,int _y, string _path){
		x=_x;
		y=_y;
		path=_path;
	}
	node() {}  //可以直接初始化 
};
int dir[4][2]={1,0,0,-1,0,1,-1,0};
char ops[5]={'D','L','R','U'};
char mp[55][55];
bool vis[55][55];

string ans;
int n,m;
bool check(int x,int y){
	if(mp[x][y]=='0'&&x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0) return true;
	return false;
}
void bfs(){
	queue<node>que;
	que.push(node(0,0,""));
	while(!que.empty()){
		node point;
		point=que.front();
		que.pop();
		int x=point.x;
		int y=point.y;
		string path=point.path;
		
		
		for(int i=0;i<4;i++){
			int nx=x+dir[i][0]; 
			int ny=y+dir[i][1];
			if(nx==n-1&&ny==m-1){
				ans=path+ops[i];
				return;
			}			
			if(check(nx,ny)){
				vis[nx][ny]=1;
				que.push(node(nx,ny,path+ops[i]));
			}
		}
	}
}


int main(){
	n=30;m=50;	
	for(int i=0;i<n;i++){
		cin>>mp[i];
	}
	bfs();
	cout<<ans<<endl;
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值