当前位置: 首页 > news >正文

双向BFS

1034 Number Game
分数 35
作者 陈越
单位 浙江大学
A number game is to start from a given number A, and to reach the destination number B by a sequence of operations.

For the current number X, there are 3 types of operations:

X=X+1
X=X−1
X=X×N
Your job is to find the minimum number of steps required to reach B from A.

Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (≤10) which is the total number of cases. For each case, three integers are given: A, B and N, where −10
5
≤A,B≤10
5
and 1<N<10. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in a line the minimum number of steps required to reach B from
创建两个队列和两个哈希表

queue<string> que1;
queue<string> que2;
unordered_map da<string,int> da1;
unordered_map db <string,int> db2;

(25)分 双向bfs
还能怎么优化呢? 请赐教

#include <bits/stdc++.h>
using namespace std;
const int K = 20;
int k;int extend(queue<int>& que,int N,map<int,int>& set_1,map<int,int>& set_2,bool temp,int d){int len = que.size();int ans = 0xffffffff;while(len--){int top  = que.front();que.pop();if(set_1.find(top+1) == set_1.end()){set_1[top+1] = d;que.push(top+1);if(set_2.find(top+1) != set_2.end()){ans = max(d + set_2[top+1],ans);}}if(set_1.find(top-1) == set_1.end()){set_1[top-1] = d;que.push(top-1);if(set_2.find(top-1) != set_2.end()){ans = max(d+set_2[top-1],ans);}}if(temp == true){if(set_1.find(top*N) == set_1.end()){set_1[top*N] = d;que.push(top*N);if(set_2.find(top*N) != set_2.end()){ans = min(set_2[top*N] + d,ans);}}}else{if(N != 0 && top % N == 0){if(set_1.find(top/N) == set_1.end()){set_1[top/N] = d;que.push(top/N);if(set_2.find(top/N) != set_2.end()){ans = min(set_2[top/N]+d,ans);}}}}}return ans;
}
int bfs(int s,int e,int n){queue<int> que1;queue<int> que2;map<int,int> set1;map<int,int> set2;que1.push(s);set1[s] = 0;que2.push(e);set2[e] = 0;if(s == e){return 0;}int d1 = 0;int d2 = 0;while(que1.size() && que2.size()){if(que1.size() < que2.size()){d1++;int t = extend(que1,n,set1,set2,true,d1);if(t != 0xffffffff){//cout<<d1<<" "<<d2<<endl;return t;}}else{d2++;int t = extend(que2,n,set2,set1,false,d2);if(t != 0xffffffff){// cout<<d1<<" "<<d2<<endl;return t;}}}return -1;
}
int main(){cin>>k;while(k--){int s,e,n;cin>>s>>e>>n;int a = bfs(s,e,n);cout<<a<<endl;}
}
http://www.lryc.cn/news/153390.html

相关文章:

  • 数据艺术:精通数据可视化的关键步骤
  • MySQL 是如何实现事务的四大特性的?
  • python实现zscore归一化和minmax标准化
  • 架构师成长之路Redis第三篇|Redis key过期清除策略
  • C++智能指针之weak_ptr(保姆级教学)
  • ElementUI浅尝辄止18:Avatar 头像
  • 1688API技术解析,实现按图搜索1688商品(拍立淘)
  • 【面试经典150题】买卖股票的最佳时机
  • selenium可以编写自动化测试脚本吗?
  • CXL.mem M2S Message 释义
  • 使用boost::geometry::union_ 合并边界(内、外):方案二
  • ICCV 2023 | 小鹏汽车纽约石溪:局部上下文感知主动域自适应LADA
  • stable diffusion实践操作-黑白稿线稿上色
  • Python学习教程:集合操作的详细教程
  • 球球的排列
  • 1783_CMD启动MATLAB同时执行一个脚本
  • C语言中内存分配的几种方式
  • 组相联cache如何快速实现cache line eviction并使用PMU events验证
  • 【Stable Diffusion安装】支持python3.11 window版
  • Anycloud37D平台移植wirelesstools
  • 海康机器人工业相机 Win10+Qt+Cmake 开发环境搭建
  • 使用MDK5的一些偏僻使用方法和谋个功能的作用
  • 【实战】十一、看板页面及任务组页面开发(六) —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(二十八)
  • 在 Amazon 搭建无代码可视化的数据分析和建模平台
  • Pinely Round 2 (Div. 1 + Div. 2) G. Swaps(组合计数)
  • elasticSearch+kibana+logstash+filebeat集群改成https认证
  • GPT带我学-设计模式-迭代器模式
  • 数学建模--层次分析法(AHP)的Python实现
  • 机器学习笔记之最优化理论与方法(三)凸集的简单认识(下)
  • Apipost:API文档、调试、Mock与测试的一体化协作平台