花同学


私信TA

用户名:H1810819089

访问量:1908

签 名:

等  级
排  名 687
经  验 3832
参赛次数 4
文章发表 2
年  龄 18
在职情况 学生
学  校 贺州学院
专  业 软件工程

  自我简介:

解题思路:就是用广度优先搜索,十分简单

注意事项:

参考代码:

#include<bits/stdc++.h>

using namespace std;


char mp[101][101];//设置地图

int book[101][101];//标记地图

int r,c,i,j;

int dir[4][2]={0,1,0,-1,-1,0,1,0};//上下左右


struct state{

int x;

int y;

int step;

};


queue<state> q;


bool panduan(state s){   //判断出没出地图边界或者走没走过该地方

if(!book[s.x][s.y]&&s.x>=0&&s.x<r&&s.y>=0&&s.y<c&&mp[s.x][s.y]=='.')

return true;

return false;

}


int bfs(state st){    //开始搜索

state now,next;

while(!q.empty()){

now=q.front();

q.pop();

if(now.x==r-1&&now.y==c-1){

return now.step;

}

for(i=0;i<4;i++){

next.x=now.x+dir[i][0];

next.y=now.y+dir[i][1];

next.step=now.step+1;

if(panduan(next)){

q.push(next);

book[next.x][next.y]=1;

}

}

}

return -1;

}


int main(){

int ans=0;

state st;

cin>>r>>c;

for(i=0;i<r;i++){

for(j=0;j<c;j++){

cin>>mp[i][j];

if(mp[i][j]=='#'){

book[i][j]=1;

}

}

}

st.x=0;

st.y=0;

st.step=0;

book[st.x][st.y]=1;

q.push(st);

cout<<bfs(st)+1;

}


 

0.0分

7 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区