【dfs/bfs+记录路径】Transformation: from A to B
来源:互联网 发布:淘宝店铺怎么推广宣传 编辑:程序博客网 时间:2024/05/23 16:54
Think:
1知识点:dfs/bfs+记录路径
2题意:输入两个数a,b,询问通过两种操作是否可以由a得到b
操作1:a = a*2
操作2:a = a*10 + 1
vjudge题目链接
以下为Accepted代码——bfs+记录路径
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;struct Node{ LL num; int size;}link[104014];int op, tp;int pre[104014];void bfs(LL a, LL b);void pri_path(int id);int main(){ LL a, b; while(~scanf("%lld %lld", &a, &b)){ if(a == b){ printf("YES\n"); printf("1\n"); printf("%lld\n", a); } else bfs(a, b); } return 0;}void bfs(LL a, LL b){ int id = -1; Node t, tmp; op = tp = 0; memset(pre, -1, sizeof(pre)); link[tp].num = a, link[tp].size = 1; tp++; while(op < tp){ t = link[op++]; tmp.num = t.num << 1; tmp.size = t.size + 1; if(tmp.num < b){ link[tp++] = tmp; pre[tp-1] = op-1; } else if(tmp.num == b){ link[tp++] = tmp; pre[tp-1] = op-1; id = tp-1; break; } tmp.num = t.num * (LL)10 + (LL)1; tmp.size = t.size + 1; if(tmp.num < b){ link[tp++] = tmp; pre[tp-1] = op-1; } else if(tmp.num == b){ link[tp++] = tmp; pre[tp-1] = op-1; id = tp-1; break; } } if(id == -1) { printf("NO\n"); } else { printf("YES\n"); printf("%d\n", link[id].size); pri_path(id); printf("\n"); } return;}void pri_path(int id){ if(pre[id] != -1) pri_path(pre[id]); if(pre[id] == -1) printf("%lld", link[id].num); else printf(" %lld", link[id].num);}
以下为Accepted代码——dfs
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;bool flag;LL a, b;int tp;LL link[1014];void dfs(LL x);int main(){ while(~scanf("%lld %lld", &a, &b)){ tp = 0, flag = false; link[tp++] = a; dfs(a); if(flag) { printf("YES\n"); printf("%d\n", tp); for(int i = 0; i < tp; i++) printf("%lld%c", link[i], i == tp-1? '\n': ' '); } else { printf("NO\n"); } } return 0;}void dfs(LL x){ if(flag || x > b) return; link[tp++] = x<<1; if(link[tp-1] == b){ flag = true; return; } dfs(link[tp-1]); if(flag) return; else tp--; link[tp++] = x*(LL)10 + (LL)1; if(link[tp-1] == b){ flag = true; return; } dfs(link[tp-1]); if(flag) return; else tp--; return;}
阅读全文
1 0
- 【dfs/bfs+记录路径】Transformation: from A to B
- codeforces Transformation: from A to B dfs+bfs
- Transformation: from A to B 暴力加dfs
- codeforces Transformation: from A to B (水题)
- 【35.56%】【727A】Transformation: from A to B
- 【codeforces 727A】Transformation: from A to B 题解
- [codeforces Technocup 2017 727A]Transformation: from A to B
- codeforces A. Transformation: from A to B(水题)
- CodeForces 727A Transformation: from A to B 暴力
- Technocup 2017 - Elimination Round 1: Transformation: from A to B
- DFS BFS 路径记录的总结
- POJ 2488 DFS+记录路径 TOJ 3520 BFS+记录路径
- 【记录】批处理实现dfs,bfs,双向bfs,A*寻路
- poj 迷宫问题(路径记录)(DFS,BFS)
- hdu1026 BFS+记录路径
- bfs+路径记录
- BFS如何记录路径
- BFS-记录路径
- eclipse解决打开eclipse markelplace速度慢的办法
- centos(6)-目录和文件
- Spark内存管理及钨丝计划
- Python资料下载网址
- 学习何凯明在图像中的降噪技术和识别率问题
- 【dfs/bfs+记录路径】Transformation: from A to B
- ue4 输出360全景图片及视频的几个方式对比及注意点
- JavaScript学习笔记整理(一)
- struts多个action使用一个配置
- 网络协议参考模型
- java求最大值的两种方法
- 爱普生打印机无法正常工作的解决方法
- 执行mapreduce程序时直接退出,没有报错
- eclipse运行AVD慢或者运行不了怎么办