看题解都是dfs我看一下地图就写了个bfs才40分来个大佬看看

P1605 迷宫

突然感觉bfs行不通,标记走过会影响到其他路线,有没有大佬能指点指点呜呜呜
by wh_1024 @ 2022-08-10 17:51:47


不做标记的话直接给整8个tle
by wh_1024 @ 2022-08-10 17:54:57


其实bfs也可以的 但是vis数组不要用bool
by Xeqwq @ 2022-08-10 17:57:27


记录每个点是走了几步走到的
by Xeqwq @ 2022-08-10 17:58:05


@[wh_1024](/user/648053)
by Xeqwq @ 2022-08-10 18:00:24


@[Xeqwq](/user/229373) 有ac代码不,佬
by mrHCT @ 2023-03-28 15:38:02


``` #include<iostream> #include<queue> #include<algorithm> using namespace std; int a[10][10]; int b[10][10]; int sx,sy,fx,fy; typedef pair<int ,int > pii; queue<pii>q; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int sum; int n,m,t; void bfs() { q.push({sx,sy}); b[sx][sy]=1; while(q.size()) { pii j=q.front(); q.pop(); for(int i=0;i<4;i++) { int x=j.first+dx[i],y=j.second+dy[i]; if(x>=0&&y>=0&&x<=n&&y<=m&&a[x][y]==0&&b[x][y]==0) { q.push({x,y}); b[x][y]=1; } } } cout<<sum; } int main() { cin>>n>>m>>t; cin>>sx>>sy>>fx>>fy; while(t--) { int x1,y1; cin>>x1>>y1; a[x1][y1]=1; } bfs(); return 0; } ```
by mrHCT @ 2023-03-28 15:38:56


这我40分的代码
by mrHCT @ 2023-03-28 15:39:27


|