我累个骚刚
2024-09-10 21:15:24
发布于:北京
7阅读
0回复
0点赞
/*
BUG:
39 29
....#......#.##...#..........
...................#.........
..#..................#...#...
.............................
..#....#.....#..#............
........#........#......#....
.....#..#.................#.#
#...#..........##............
.............................
...............#....##.......
......#....#.....#...#.......
............................#
.#....#......#...............
..............#..............
...#.....##.#.....#.....###..
........#..................#.
...............##.....#......
..#..........................
.......#......#.#...#.#..#...
.....#...............#.......
#...#........................
............#....#...#.......
.....#...#....#....#.........
##...........##..........##.#
............................#
...........#..............#..
.....#...#....#..............
........................#...#
..#.........#................
.......#......##.............
.#.........#.................
...#..........#.......#......
.....#.#...............#.....
.........#...................
........................#....
........................##...
.............................
....................#..#.....
........#.........#...#......
*/
#include<bits/stdc++.h>
using namespace std;
int walk[8][2]={{1,0},{-1,0},{0,1},{0,-1}/*,{1,1},{-1,1},{-1,-1},{1,-1}*/};
string mp[50];
int n,m;
struct node{
int x,y,steps;
};
bool vis[50][50];
bool check(node x){
return x.x>=0 && x.x<n && x.y>=0 && x.y<m && mp[x.x][x.y]=='.' && !vis[x.x][x.y];
}
int ans=0;
int Depth_first_search(node start,node end){
queue<node> q;
q.push(start);
while(q.size()){
node f=q.front();
q.pop();
vis[f.x][f.y]=1;
if(f.x==end.x && f.y==end.y) return f.steps;
for(int i=0;i<4;i++){
int nextx=f.x+walk[i][0];
int nexty=f.y+walk[i][1];
node next={nextx,nexty,f.steps+1};
if(check(next)){
vis[next.x][next.y]=1;
q.push(next);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>mp[i];
}
cout<<Depth_first_search({0,0,1},{n-1,m-1});
return 0;
}
这里空空如也
有帮助,赞一个