Light OJ 1141--BFS--(隐蔽的BFS)
来源:互联网 发布:linux jenkins svn 编辑:程序博客网 时间:2024/06/08 07:12
题意:求数字A变换到B 的最小步数。变换方法是每次加A的素因数
分析:BFS
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int s,t,n,vis[1009],prim[1009],fac[1009];struct h{int x,tot;};queue<h> q;int cnt;void check()//素数筛{cnt=0;memset(vis,0,sizeof(vis));for(int i=2;i<1000;i++){if(!vis[i]){prim[cnt++]=i;for(int j=2;j*i<1000;j++) vis[j*i]=1;}}}int bfs(){while(!q.empty()){int x=q.front().x;int tot=q.front().tot;q.pop();cnt=0;for(int i=0;prim[i]<x;i++){if(x%prim[i]==0) fac[cnt++]=prim[i];}for(int i=0;i<cnt;i++){h tmp;tmp.x=x+fac[i];if(tmp.x>0&&tmp.x<=t&&!vis[tmp.x]){if(tmp.x==t) return tot+1;tmp.tot=tot+1;vis[tmp.x]=1;q.push(tmp);}}}return -1;}int main(){check();cin>>n;for(int i=1;i<=n;i++){while(!q.empty()) q.pop();memset(vis,0,sizeof(vis));cin>>s>>t;if(s==t) cout<<"Case "<<i<<": 0"<<endl;else{h tmp;tmp.x=s,tmp.tot=0;vis[s]=1;q.push(tmp);int ans=bfs();cout<<"Case "<<i<<": "<<ans<<endl; }}}
0 0
- Light OJ 1141--BFS--(隐蔽的BFS)
- Light OJ 1055 BFS
- Light OJ 1238--BFS(第一道自己做的)
- !POJ 3278 Catch That Cow--BFS(隐蔽的BFS)
- !Light oj 1066--BFS--(队列溢出)
- Light oj 1046 - Rider (BFS)
- Light OJ 1066(bfs+坑)
- Light OJ:1141 Number Transformation(BFS+素因子)
- Light OJ 1377 - Blade and Sword (BFS)
- Light OJ 1094 - Farthest Nodes in a Tree【树的直径 两次bfs】
- light oj 1066Gathering Food (bfs 稍微有点小坑)
- Hust oj 1943 马是走“日”的(BFS)
- 湖北民族学院oj 1780 (BFS) 之 马的移动
- 百练OJ 晶矿的个数(基础BFS)
- SDUT OJ 2141 基于邻接矩阵的BFS
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
- csu oj 1010 Water Drinking(BFS)
- Hust oj 1367 King(BFS)
- 对象作为方法的参数
- 杭电 HDU ACM 1408 盐水的故事
- php <<<End....End heredoc的使用方法
- hdoj 1078 fatmouse and cheese 记忆化搜索
- iOS项目完成后上传到apple Store的流程以及注意事项
- Light OJ 1141--BFS--(隐蔽的BFS)
- 第十一讲_项目一_分离三位数
- 【leetcode】Minimum Path Sum
- 20150405_链表
- C++ vector 使用
- android studio最基本的知识一
- LeetCode66/169/79 Plus One/Majority Element /Word Search
- linux ls和 ll 命令
- HTML5-canvas 绘图