版本比较
搜索进阶-BFS进阶
正文
增加 15 行删除 15 行
66 行未改动676869707172737475201 行未改动全部展开277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319114 行未改动全部展开434435436437438439440441442443444445446447448449450451452291 行未改动全部展开66 行未改动清空堆,初始化距离数组。在堆中插入起始节点,设置起始节点的距离。- 找到堆首元素- 遍历堆首元素能到达的所有儿子节点201 行未改动全部展开int fy[]={0,1,-1,0,0};struct node{int x,y,step;};queue<node> q;int main(){//读入n行m列的一个矩阵scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&s[i][j]);//读入起始位置和终点的位置scanf("%d%d%d%d",&startx,&starty,&endx,&endy);//搜索前的初始化vis[startx][starty]=1;q.push({startx,starty,0});bool find=false;//广度优先搜索while(!q.empty()){/*empty函数:队列为空时返回true*///取出队首元素node t=q.front();//队首出队q.pop();if(t.x==endx and t.y==endy){//找到终点find=true;//标记找到了cout<<t.step<<'\n';break;}for(int i=1;i<=4;i++){int nowx=t.x+fx[i],nowy=t.y+fy[i];//尝试拓展的节点坐标if(nowx<1 or nowy<1 or nowx>n or nowy>m)continue;//越界if(s[nowx][nowy]==1 or vis[nowx][nowy])continue;//被访问过或无法访问vis[nowx][nowy]=1;//标记q.push({nowx,nowy,t.step+1});//存入该状态}}if(!find)cout<<"找不到路径\n";//没有被标记return 0;}```:::114 行未改动全部展开:::**思路:**定义状态 $(x,y,State)\Rightarrow\begin{cases}(x,y)&State=1\\(x,y),(x,y+1)&State=2\\(x,y),(x+1,y)&State=3\end{cases}$显然有转移 $\begin{cases}(x,y,1)\Rightarrow\begin{cases}(x,y+1,2)\\(x,y-2,2)\\(x+1,y,3)\\(x-2,y,3)\end{cases}\\(x,y,2)\Rightarrow\begin{cases}(x,y-1,1)\\(x,y+3,1)\end{cases}\\(x,y,3)\Rightarrow\begin{cases}(x-1,y,1)\\(x+3,y,1)\end{cases}\end{cases}$:::success[代码]```cppint n,m,vis[505][505][5],ans=-1;char s[505][505];struct node{int x,y,st,step;//1立2左右3上下}st,ed;void bfs(){queue<node> q;q.push(st);291 行未改动全部展开66 行未改动676869707172737475201 行未改动全部展开277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319114 行未改动全部展开434435436437438439440441442443444445446447448449450451452291 行未改动全部展开66 行未改动清空堆,初始化距离数组。在堆中插入起始节点,设置起始节点的距离。- 找到堆首元素- 遍历堆首元素能到达的所有儿子节点201 行未改动全部展开int fy[]={0,1,-1,0,0};struct node{int x,y,step;};queue<node> q;int main(){//读入n行m列的一个矩阵scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&s[i][j]);//读入起始位置和终点的位置scanf("%d%d%d%d",&startx,&starty,&endx,&endy);//搜索前的初始化vis[startx][starty]=1;q.push({startx,starty,0});bool find=false;//广度优先搜索while(!q.empty()){/*empty函数:队列为空时返回true*///取出队首元素node t=q.front();//队首出队q.pop();if(t.x==endx and t.y==endy){//找到终点find=true;//标记找到了cout<<t.step<<'\n';break;}for(int i=1;i<=4;i++){int nowx=t.x+fx[i],nowy=t.y+fy[i];//尝试拓展的节点坐标if(nowx<1 or nowy<1 or nowx>n or nowy>m)continue;//越界if(s[nowx][nowy]==1 or vis[nowx][nowy])continue;//被访问过或无法访问vis[nowx][nowy]=1;//标记q.push({nowx,nowy,t.step+1});//存入该状态}}if(!find)cout<<"找不到路径\n";//没有被标记return 0;}```:::114 行未改动全部展开:::**思路:**定义状态 $(x,y,State)\Rightarrow\begin{cases}(x,y)&State=1\\(x,y),(x,y+1)&State=2\\(x,y),(x+1,y)&State=3\end{cases}$显然有转移 $\begin{cases}(x,y,1)\Rightarrow\begin{cases}(x,y+1,2)\\(x,y-2,2)\\(x+1,y,3)\\(x-2,y,3)\end{cases}\\(x,y,2)\Rightarrow\begin{cases}(x,y-1,1)\\(x,y+3,1)\end{cases}\\(x,y,3)\Rightarrow\begin{cases}(x-1,y,1)\\(x+3,y,1)\end{cases}\end{cases}$:::success[代码]```cppint n,m,vis[505][505][5],ans=-1;char s[505][505];struct node{int x,y,st,step;//1立2左右3上下}st,ed;void bfs(){queue<node> q;q.push(st);291 行未改动全部展开