广搜BFS 做题记录(python)
【有序队列】
1.此题存在"慢路"(有守卫的位置)与"快路",遇到"慢路"需要消耗多倍时间/资源。故需要给类加一个是否"可直接判断"累减属性,对于"慢路",每次访问都减一,直到该属性转变为"可判断",此后与正常通路点一致;
2.关于python题解版本,参考hzw大佬的代码,发现POJ上使用一维无限延伸列表+维护头尾指针构造的队列可以过(耗时不到4000ms),但使用collections库自带的双向队列一定会超时(>10000ms)。不知是什么原因,可能和评测程序环境、调用库函数的时间成本有关。总之,之后在题目不做要求时可以多考虑直接用列表构造队列。
参考代码1(hzw大佬):

a = [''] * 200 xx = [0, 0, 1, -1] yy = [-1, 1, 0, 0] qx = [0] * 80000 qy = [0] * 80000 g = [0] * 80000 dis = [0] * 80000 mp = [] def bfs(bx, by): head = 0 tail = 1 qx[0] = bx qy[0] = by while head != tail: x, y, st, d = qx[head], qy[head], g[head], dis[head] head = head + 1 if st == 1: qx[tail] = x qy[tail] = y dis[tail] = d + 1 g[tail] = 0 tail = tail + 1 continue for i in range(4): tx = x + xx[i] ty = y + yy[i] if tx < 0 or ty < 0 or tx >= n or ty >= m or a[tx][ty] == '#': continue if mp[tx][ty] == 0: if a[tx][ty] == 'a': return d + 1 mp[tx][ty] = 1 qx[tail] = tx qy[tail] = ty g[tail] = (a[tx][ty] == 'x') dis[tail] = d + 1 tail = tail + 1 return -1 t = int(input()) for test in range(t): mp.clear() n, m = map(int, input().split()) for i in range(n): mp.append([0] * 200) a[i] = input() for j in range(m): if(a[i][j] == 'r'): bx, by = i, j ans = bfs(bx, by) if ans == -1: print('Impossible') else: print(ans)
参考代码2(在hzw大佬版本上、根据个人习惯的调整):

maze = ["" for i in range(200)] dirx = [0,0,1,-1] diry = [-1,1,0,0] xseq = [0 for i in range(80000)] yseq = [0 for i in range(80000)] gd = [0 for i in range(80000)] stp = [0 for i in range(80000)] vis = [] n = 0 m = 0 def bfs(sx,sy): global maze,dirx,diry,xseq,yseq,gd,stp,vis,n,m hp = 0 tp = 1 xseq[0],yseq[0] = sx,sy while hp != tp: x,y,block,step = xseq[hp], yseq[hp], gd[hp], stp[hp] hp += 1 if block == 1: xseq[tp] = x yseq[tp] = y stp[tp] = step + 1 gd[tp] = 0 tp += 1 continue for i in range(4): tx = x + dirx[i] ty = y + diry[i] if (tx<0 or ty<0 or tx>=n or ty>=m \ or vis[tx][ty] == 1 or maze[tx][ty] == "#"): continue if maze[tx][ty] == "a": print(step+1) return if maze[tx][ty] == "x": gd[tp] = 1 else: gd[tp] = 0 xseq[tp],yseq[tp] = tx,ty vis[tx][ty] = 1 stp[tp] = step+1 tp += 1 print("Impossible") return def solu(): global maze,vis,n,m k = int(input()) for i in range(k): vis.clear() rget = False n,m = map(int, input().split()) for j in range(n): vis.append([0 for i in range(200)]) maze[j] = input() if rget: continue for r in range(m): if maze[j][r] == "r": sx,sy = j,r rget = True bfs(sx,sy) solu()
参考代码3(deque库版本,POJ超时,仅供思路参考):

import collections class pt(): def __init__(self,x_,y_,step_,gd_): self.x = x_ self.y = y_ self.step = step_ self.gd = gd_ maze = ["" for i in range(205)] dx = [0,0,1,-1] dy = [1,-1,0,0] vis = [] n,m = 0, 0 def bfs(sx,sy): global maze,vis,n,m q = collections.deque() q.append(pt(sx,sy,0,0)) while len(q) > 0: ptmp = q.popleft() if ptmp.gd == 1: q.append(pt(ptmp.x, ptmp.y, ptmp.step+1, 0)) continue for j in range(4): tx = ptmp.x + dx[j] ty = ptmp.y + dy[j] if (tx<0 or ty<0 or tx>=n or ty>=m \ or vis[tx][ty]==1 or maze[tx][ty]=="#"): continue if maze[tx][ty] == "a": print(ptmp.step+1) return if maze[tx][ty] == "x": q.append(pt(tx,ty,ptmp.step+1,1)) else: q.append(pt(tx,ty,ptmp.step+1,0)) print("Impossible") return def solu(): global maze,vis,n,m k = int(input()) for kk in range(k): rget = False vis.clear() n,m = map(int,input().split()) for i in range(n): maze[i] = input() vis.append([0 for i in range(205)]) if not rget: for j in range(m): if maze[i][j] == "r": sx_,sy_ = i,j rget = True bfs(sx_,sy_) return solu()
继续加油!
用代码改变世界!就是这样,喵!
posted on 2021-03-26 20:54 Mju_halcyon 阅读(178) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构