Lightoj 1141 BFS
来源:互联网 发布:淘宝助理5.8 编辑:程序博客网 时间:2024/05/16 15:26
注意是质素因子。。。。。
AC代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;#define MAX 0x3f3f3f3ftypedef struct{int step;int now;}Node;int s, t;bool prime[1010];int marks[1001];int BFS(){queue<Node> q;Node start;start.now = s;start.step = 0;q.push( start );memset( marks, 0x3f, sizeof( marks ) );int ans = MAX;while( !q.empty() ){Node n = q.front();q.pop();if( n.now == t ){ans = min( ans, n.step );continue;}for( int i = 2; i < n.now; i++ ){if( n.now % i != 0 || !prime[i] ){continue;}if( n.now + i > t ){continue;}if( marks[n.now+i] > n.step + 1 ){Node temp;temp.now = n.now + i;temp.step = n.step + 1;marks[n.now+i] = temp.step;q.push( temp );}}}if( ans < MAX ){return ans;}return -1;}int main(){int T, Case = 1;memset( prime, true, sizeof( prime ) );for( int i = 2; i <= 1000; i++ ){for( int j = 2; i * j <= 1000; j++ ){prime[i*j] = false;}}cin >> T;while( T-- ){cin >> s >> t;cout << "Case " << Case++ << ": " << BFS() << endl;}return 0;}
- Lightoj 1141 BFS
- lightoj-1141【bfs】
- LightOJ 1141 Number Transformation bfs
- Lightoj 1009 BFS
- LightOJ 1174--Commandos (BFS)
- LightOJ 1175 (BFS)
- LightOJ-1046-BFS
- lightoj 1099【dijkstra/BFS】
- LightOJ-1094-图论,bfs
- LightOJ-1066-bfs
- LightOJ 1012(基础图bfs)
- LightOJ - 1039 A Toy Company(BFS)
- LightOJ 1238 Power Puff Girls(BFS)
- Lightoj 1066 Gathering Food(BFS)
- 【LightOJ】1122 - Digit Count(bfs)
- LightOJ 1111 Best Picnic Ever 简单bfs
- 【lightoj】1141
- 【LightOJ 1141
- 自己
- Hashmap 与 HashTable 的区别
- 自学FreeCAD 第一篇 Python函数
- 四通道图像中的alpha通道
- 简单计算器
- Lightoj 1141 BFS
- 找新朋友 //找两个数是否有共同的公约数
- 隐含马尔可夫模型在语言处理中的应用
- 防尘塞卡手机里了怎么办?
- HNOI-排队
- UVa 10763 Foreign Exchange (map的应用)
- Memcached Client 使用手册
- 最近用到的开源项目 Android ExpandableListView
- DirectX如何构建一个太阳系