社区讨论
我本地运行bfs代码花了85.64s
学术版参与者 6已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mibpv1yc
- 此快照首次捕获于
- 2025/11/23 20:50 3 个月前
- 此快照最后确认于
- 2025/11/23 21:34 3 个月前
错误: 0
- 警告: 0
- 输出文件名: D:\Desktop\3818.exe
- 输出大小: 2.16203308105469 MiB
- 编译时间: 85.64s
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+10;
int h,w,d,r,vis[N][N][2];
int dx[5]={0,-1,0,1},dy[5]={-1,0,1,0};
char a[N][N];
struct node{
int x,y;
int h;
int sum;
};
queue<node>q;
void bfs(){
memset(vis,0x3f3f3f3f,sizeof(vis));
vis[1][1][0]=0;
q.push({1,1,0,0});
while(!q.empty()){
node t=q.front();
q.pop();
if(t.sum>vis[t.x][t.y][t.h]) continue;
for(int i=0;i<4;i++){
int xx=t.x+dx[i];
int yy=t.y+dy[i];
if(xx>=1&&xx<=h&&yy>=1&&yy<=w&&a[xx][yy]=='.'){
if(t.sum+1<vis[xx][yy][t.h]){
vis[xx][yy][t.h]=t.sum+1;
q.push({xx,yy,t.h,vis[xx][yy][t.h]});
}
}
}
if(t.h==0){
int xx=t.x+d;
int yy=t.y+r;
int sh=1;
if(xx>=1&&xx<=h&&yy>=1&&yy<=w&&a[xx][yy]=='.'){
if(t.sum+1<vis[xx][yy][sh]){
vis[xx][yy][sh]=t.sum+1;
q.push({xx,yy,sh,vis[xx][yy][sh]});
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>h>>w>>d>>r;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>a[i][j];
}
}
bfs();
if(vis[h][w][0]==0x3f3f3f3f&&vis[h][w][1]==0x3f3f3f3f){
cout<<-1;
}else{
if(vis[h][w][0]!=0x3f3f3f3f){
cout<<vis[h][w][0];
}else{
cout<<vis[h][w][1];
}
}
return 0;
}
怎么回事
回复
共 5 条回复,欢迎继续交流。
正在加载回复...