不保证完全正确(保证能过这题)
2024-06-08 11:15:28
发布于:上海
30阅读
0回复
0点赞
起点也是点。
#include<iostream>
#include<queue>
using namespace std;
using ll = int;
using FLOAT = bool;
FLOAT vis[40][40];
ll dx[4]{0,0,-1,1},dy[4]{-1,1,0,0},r,c;
void bfs(){
auto f = [](ll x,ll n)->bool{
return x < 0||x >= n;
};
queue<ll>xq,yq,t;
xq.push(0),yq.push(0),t.push(0);
vis[xq.front()][yq.front()] = 1;
while(xq.size()){
if(xq.front()==r-1&&yq.front()==c-1)cout << t.front(),exit(0);
for(int i{0};i < 4;i++){
if(f(xq.front()+dx[i],r)||f(yq.front()+dy[i],c)||vis[xq.front()+dx[i]][yq.front()+dy[i]])continue;
vis[xq.front()+dx[i]][yq.front()+dy[i]] = 1;
xq.push(xq.front()+dx[i]);yq.push(yq.front()+dy[i]);t.push(t.front()+1);
}
xq.pop(),yq.pop(),t.pop();
}
}
int main(){
ios::sync_with_stdio(0);
cin >> r >> c;char chr;
for(ll i{0};i < r;i++){
for(ll j{0};j < c;j++){
cin >> chr;
if(chr == '#'){
vis[i][j] = 1;
}
}
}
bfs();
return 0;
}
这里空空如也
有帮助,赞一个