历届试题—迷宫

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

int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
char res[4]={'D','L','R','U'};
char map[30][50];
int vis[30][50]={0};
int x1,y1,x2,y2;

struct node{
 int x;
 int y;
 int step;
 string path;
};
void bfs(){
 queue<node> q;
 node p;
 p.x=x1,p.y=y1,p.step=0,p.path="",vis[0][0]=1;
 q.push(p);
 while(!q.empty()){
  node p;
  p=q.front();
  if(p.x==x2 && p.y==y2){
   if(p.step==0){
    
   }
   cout<<p.step<<endl<<p.path<<endl;
  }
  node v;
  for(int i=0;i<4;i++){
   v.x = p.x + dir[i][0];
   v.y = p.y + dir[i][1];
   if(v.x>=0 && v.y>=0 && v.x<30 && v.y<50 && vis[v.x][v.y]==0&&map[v.x][v.y]=='0'){
    vis[v.x][v.y] = 1;
    v.step = p.step+1;
    v.path = p.path+res[i];  
    q.push(v);
   }
  }
  q.pop();
 }
}
int main(){
 for(int i=0;i<30;i++){
  for(int j=0;j<50;j++){
   cin>>map[i][j];
  }
 }
 x1=0,y1=0,x2=29,y2=49;
 bfs();
 return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值