版本比较
广度优先搜索 BFS I && 回顾--深度优先搜索
正文
增加 69 行删除 69 行
1234567891011121345678910111213141516151617181918192021222311 行未改动353637383939404142436 行未改动505152535455545556575859596061626336 行未改动1001011021031041041051061071081091091101111121133 行未改动11711811912012112112212312412512412512612712812913 行未改动14314414514614714714814915015122 行未改动17417517617717817817918018118214 行未改动19719819920020120220120220320420520634 行未改动2412422432442452452462472482498 行未改动2582592602612622622632642652662672672682692702712722722732742752762772772782792802812822822832842852862872872882892902912922932942942952962972982993003013023033033043053053063073073083093093103113123123133143143153163173173183193203213223223233243243253263263273283293303303313323323333343353363373373383393393403413413423433443443453463463473483493503513513523533543553563563573583583593603613603613623633633643653663663673683693693703713723734 行未改动3783793803813823823833843853864 行未改动391392393394395395396397397398399399400401402403403404405406406407408409410411412413414414415416417417418419419420我们先来回顾一下深度优先搜索,先来看一张图。```cpp 11 | . . . . . . . . . . 10 | . . . . * . . . . . 9 | . . . . . . . . . . 8 | . . . * . * . . . . 7 | . . . . . . . * . . 6 | . . * . . * . . . H 5 | * . . . . . . . . . 4 | . . . * . . . * . . 3 | . K . . . . . . . . 2 | . . . * . . . . . * 1 | . . * . . . . * . . 10 | . . . . * . . . . . 9 | . . . . . . . . . . 8 | . . . * . * . . . . 7 | . . . . . . . * . . 6 | . . * . . * . . . H 5 | * . . . . . . . . . 4 | . . . * . . . * . . 3 | . K . . . . . . . . 2 | . . . * . . . . . * 1 | . . * . . . . * . . 0 ---------------------- 1 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 4 5 6 7 8 9 0```假设我们要从 $K$ 的位置走到 $H$ 的位置,`*`为障碍物,`.`为空地。 首先我们先看能不能往上走,如果可以就走。若不能,则依次向左,右,下这几个方向看,如果还是不能,则退回去,否则继续向前走。 假设我们要从 $K$ 的位置走到 $H$ 的位置,`*`为障碍物,`.`为空地。首先我们先看能不能往上走,如果可以就走。若不能,则依次向左,右,下这几个方向看,如果还是不能,则退回去,否则继续向前走。
反正就是一句话: 不撞南墙不回头。
注意,在走的时候,要注意几个地方:11 行未改动1.在走的时候,要注意几个地方:- 不能走出边界- 不能重复走同一个格子- 不能有障碍物
2.走动的时候,要根据偏移数组来求位置,数组求法如下:
||-1,0|||:-:|:-:|:-:|6 行未改动int dx[]={0,-1,1,0,0};int dy[]={0,0,0,-1,1};```
3.我们要注意到题目中的一句话: -->注意到ha, la, hb, lb全部是从0开始计数的。 3.我们要注意到题目中的一句话:-->注意到ha, la, hb, lb全部是从0开始计数的。而我们输入时是按从下标 $1-n$ 输入的,所以需要偏移位置,操作为: $$ha++,la++,hb++,lb++;$$
4.起始位置和终止位置考虑 4.起始位置和终止位置考虑如果起始位置或终止位置有一个点有障碍物,那么就永远走不到,输出无解。
```cpp36 行未改动 ha++,la++,hb++,lb++;//位置偏移,同下标 if(a[ha][la]=='#'||a[hb][lb]=='#')//但凡起始或终止位置有障碍物一概输出无解 { cout<<"NO\n"; return ; return ; } vis[ha][la]=true;//起始位置要标记,否则会再次走到起点 dfs(ha,la,hb,lb); if(vis[hb][lb]==true) cout<<"YES\n"; else cout<<"NO\n"; else cout<<"NO\n"; return ;}signed main(){3 行未改动 { work(); } return 0;} }```
### 广度优先搜索解法: 假设起始位置是 $(2,3)$ ,终止位置是 $(4,3)$ ,我们就可以先找到一步能走到的地方,然后做好标记。接着考虑走两步能走到的地方。注意:在这里不需要从起点开始搜索,只要在一步能走到的地方进行扩散操作即可,也就是把一步能走到的地方看成起点,求走一步能走到的地方。以此类推..... ### 广度优先搜索解法:假设起始位置是 $(2,3)$ ,终止位置是 $(4,3)$ ,我们就可以先找到一步能走到的地方,然后做好标记。接着考虑走两步能走到的地方。注意:在这里不需要从起点开始搜索,只要在一步能走到的地方进行扩散操作即可,也就是把一步能走到的地方看成起点,求走一步能走到的地方。以此类推.....
不过还是要遵循之前的原则:- 不能走出边界- 不能重复走同一个格子13 行未改动- 若在坐标时 `t.x==yyy1&&t.y==yyy2` ,则是终点,不需要扩散,直接返回即可
其余的跟上面的分析一样,例如特殊情况。
```cpp#include<bits/stdc++.h>using namespace std;#define int long long22 行未改动 if(t.x==yyy1&&t.y==yyy2)//是终点就不用扩散 { return 1; }
for(int i=0;i<4;i++) { int nx=t.x+dx[i];//偏移位置 int ny=t.y+dy[i];//偏移位置14 行未改动 for(int j=1;j<=n;j++) { cin>>a[i][j]; vis[i][j]=false;//清空 } } } } int ha,la,hb,lb; cin>>ha>>la>>hb>>lb; ha++,la++; hb++,lb++;34 行未改动
struct node{ int x,y;
int step;};
char a[maxn][maxn];8 行未改动 if(ax<1) { return true; }
if(ay<1) { return true; }
if(ax>n) { return true; }
if(ay>m) { return true; }
if(vis[ax][ay]==true) { return true; }
if(a[ax][ay]=='#') { return true; }
return false;
}
int bfs(int posx,int posy,int yyy1,int yyy2){
if(a[posx][posy]=='#') { return -1; } if(a[yyy1][yyy2]=='#') { return -1; }
queue<node>q;
vis[posx][posy]=false;
q.push({posx,posy,0});
while(q.empty()==false) {
node t=q.front();
if(t.x==yyy1) {
if(t.y==yyy2) { return t.step; }
}
q.pop();
for(int i=0;i<4;i++) { int nx=dx[i]+t.x;
int ny=dy[i]+t.y;
if(check(nx,ny)) { continue; }
vis[nx][ny]=true;
q.push({nx,ny,t.step+1});
} }
return -1;
}
void work(int r,int c){
for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) {
cin>>a[i][j];
vis[i][j]=false; }
} } //起始 //起始 int qx; int qy; //终止 //终止 int zx; int zy;
for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) {4 行未改动 break; } } }
for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) {4 行未改动 break; } } }
cout<<bfs(qx,qy,zx,zy)<<"\n";
return ;
}signed main(){
while(cin>>n>>m) {
if(n==0) { if(m==0) { break; } }
work(n,m); }
return 0;} }```