博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搜索专题:问题 E: 挑战ACM迷宫
阅读量:6970 次
发布时间:2019-06-27

本文共 4020 字,大约阅读时间需要 13 分钟。

这里写图片描述

这是往年校赛的一道题,最开始做这道题的时候还没有系统的学习过搜索,用了C语言学的回溯法尝试,毫无疑问的TLE;
学习了DFS之后,自己的剪枝功力不够,又是TLE,最后学了BFS之后,哇,终于做出来了,别提多开心了,然后意识到这道题其实很简单的,剋以用BFS标记法和更改步数法(更改最小消耗),后来发现这种题也可以建图跑迪杰斯特拉做;
BFS标记法:

#include
#include
#include
#include
using namespace std;const int N = 100 + 5;int mat[N][N];bool visit[N][N];typedef struct node{ int x,y,val; bool operator < (const node x)const { return val > x.val; }}Node;const int dir[4][2] = {
{
1,0},{-1,0},{
0,1},{
0,-1}};int BFS(int n){ priority_queue
Q; Node t; memset(visit,0,sizeof(visit)); int x=0,y=0,goalx=n-1,goaly=n-1,newx,newy; t=(Node){x,y,mat[x][y]}; visit[x][y] = true; Q.push(t); while(!Q.empty()){ t = Q.top();Q.pop(); if(t.x == goalx && t.y == goaly) return t.val; for(int d=0;d<4;d++){ newx = t.x + dir[d][0]; newy = t.y + dir[d][1]; if(newx>=0 && newx
=0 && newy

BFS更改步数法:

#include
#include
#include
#include
using namespace std;const int N = 100+5;const int MaxSize = 1e5;const int INF = (1<<30);int mat[N][N];int Min[N][N];typedef struct node{ int x,y,val; bool operator < (const node x) const { return val > x.val; }}Node;const int dir[4][2]={
{
1,0},{-1,0},{
0,1},{
0,-1}};int BFS(int n){ priority_queue
Q; int newx,newy,x=0,y=0,goalx= n-1,goaly=n-1; Node t,s; t.x = x,t.y = y,t.val = mat[0][0]; Min[x][y] = mat[x][y]; Q.push(t); while(true){ t = Q.top(); if(t.x == goalx && t.y == goaly ) return t.val; for(int d=0;d<4;d++){ newx = t.x+dir[d][0]; newy = t.y+dir[d][1]; if(newx>=0 && newx
=0 && newy

建图跑迪杰斯特拉:

#include
#include
#include
#define _cp(a,b)((a.val)<(b.val))using namespace std;const int MAXN = 100000;const int INF = (1<<30);const int N = 100 + 5;int mat[N][N],D[N*N];bool visit[N*N];int Size[N*N];typedef struct node{ short val,to;}Node;typedef Node elem_t;Node edge[N*N][4];const int dir[4][2]={
{-1,0},{
1,0},{
0,1},{
0,-1}};void Init(){ for(int i=0;i
=0 && newx
=0 && newy
1 && _cp(e,h[p>>1]);h[p]=h[p>>1],p>>=1); h[p] = e; } int del(elem_t &e){ if(!n) return 0; for(e=h[p=1],c=2;c
D[u] + edge[u][j].val){ D[v] = D[u] +edge[u][j].val; t.to = v,t.val = D[v]; H.ins(t); } } } return D[n*n-1] + mat[0][0];}void Input_data(int n){ for(int i=0;i

双向BFS:

#include
#include
#include
#include
using namespace std;const int N = 100+5;const int MaxSize = 1e5;const int INF = (1<<30);int mat[N][N];struct visit{ int val,vis;}Min[N][N];typedef struct node{ int x,y,val; bool operator < (const node x) const { return val > x.val; }}Node;const int dir[4][2]={
{1,0},{-1,0},{0,1},{0,-1}};int BFS(int n){ priority_queue
Q1,Q2; int newx,newy,cnt,x=0,y=0,goalx= n-1,goaly=n-1,ans = INF; Node t,s; t.x = x,t.y = y,t.val = mat[0][0]; s.x = goalx,s.y = goaly,s.val = mat[goalx][goaly]; Min[x][y].vis = 1,Min[x][y].val = mat[x][y]; Min[goalx][goaly].vis = 2,Min[goalx][goaly].val = mat[goalx][goaly]; Q1.push(t); Q2.push(s); while(!Q1.empty()|| !Q2.empty()){ cnt = Q1.size(); while(cnt--){
t = Q1.top(); Q1.pop(); if(t.x == goalx && t.y == goaly ) ans = min(ans,t.val); //这里一定要注意,要拓展完才能确定最小值,不能相遇就跳出 for(int d=0;d<4;d++){
newx = t.x+dir[d][0]; newy = t.y+dir[d][1]; if(newx>=0 && newx
=0 && newy
=0 && newx
=0 && newy

在这几种方法里,双向BFS和迪杰斯特拉耗时都是12ms,比其他两种BFS耗时都短,虽然说在这里使用迪杰斯特拉有点大材小用的意味,但是这种方法也去可以应用到其他问题上

转载于:https://www.cnblogs.com/Pretty9/p/7347724.html

你可能感兴趣的文章
mysql数据库索引页号为什么从3开始_【转载】MySQL索引背后的数据结构及算法原理...
查看>>
mysql的both_MySQL中Global、Session和Both(Global & Session)范围
查看>>
mysql转日期比较_mysql 日期转换 比较
查看>>
python下载图片脚本_Python实现批量下载图片的方法
查看>>
堆排序python理解_python堆排序如何使用呢?
查看>>
5道java面试题_5道常见的Java面试题!值得一看
查看>>
mysql数据类型支持比较运_MySQL整理5—数据类型和运算符
查看>>
java系统课程设计报告_JAVA学生管理系统课程设计报告
查看>>
java反序列化成object_java常用知识:ObjectInputStream反序列化流
查看>>
java打印日期序列_关于日期:如何使用java获取上一季度的详细信息
查看>>
Java程序编译后的扩展名_一个Java源程序经过编译后,得到的文件扩展名一定是.class。...
查看>>
python graphics让长方形旋转_python - 您如何使汽车向其朝向移动? (使用python和turtle图形) - 堆栈内存溢出...
查看>>
java 菱形 乱码_(04)Spring MVC之Get方式传参访问Controller,从Controller返回json串出现菱形问号(?????)乱码,解决方法。...
查看>>
php怎么连kafka,php连接kafka
查看>>
java怎么使用取余符号,Java中的相除(/)和取余(%)的实现方法
查看>>
php动态生成html,通用PHP动态生成静态HTML网页的代码
查看>>
php 方法不区分大小写,为什么PHP中的函数和方法不区分大小写?
查看>>
php后台批量删除,php如何实现批量删除数据_后端开发
查看>>
dede个人中心php在哪,dedecms织梦如何自定义会员中心目录名称的方法
查看>>
php怎么升级到5.4,php从5.2升级到5.4
查看>>