HzuCZN


私信TA

用户名:0309

访问量:7995

签 名:

等  级
排  名 98
经  验 8352
参赛次数 6
文章发表 12
年  龄 0
在职情况 学生
学  校 贺州学院
专  业 软件工程

  自我简介:

算法极差,要哭了0.0.。。。。。。

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=500;
int dx[]={1,0,0,-1}; // 下 D 左 L 右 R  上 U
int dy[]={0,-1,1,0};
bool vis[maxn][maxn];
char  mat[maxn][maxn];
string s[maxn],ans;
struct node
{
 int x,y,step;
 string s;
};
int main()
{ 
 int n,m;
 cin>>n>>m;
 memset(mat,'1',sizeof(mat));
 for(int i=1;i<=n;i++){
  for(int j=1;j<=m;j++){
   cin>>mat[i][j];
  }
 }
 node top,next;
 top.x=1;top.y=1;top.step=0,top.s="";
 vis[1][1]=1;
 queue<node> q;
 q.push(top);
 while(!q.empty()){
  top=q.front();
  q.pop();   
  for(int i=0;i<4;i++){
   next.x=top.x+dx[i];
   next.y=top.y+dy[i];
   if(mat[next.x][next.y]=='1'||next.x<1||next.y<1||vis[next.x][next.y]||next.x>n||next.y>m)
    continue;
   if(i==0){
    next.s=top.s+"D";
   }else if(i==1){
    next.s=top.s+"L"; 
   }else if(i==2){
    next.s=top.s+"R";
   }else{
    next.s=top.s+"U"; 
   }
   next.step=top.step+1;  
   if(next.x==n&&next.y==m){
    cout<<next.step <<endl;
    cout<<next.s<<endl;
    return 0;
   }    
   vis[next.x][next.y]=1;
   q.push(next);
  }
 }
 return 0; 
}


 

0.0分

2 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区