POJ 3126 Prime Path(BFS)
来源:互联网 发布:java基础实例程电子书 编辑:程序博客网 时间:2024/05/16 09:36
Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12616 Accepted: 7145
Description
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
1733
3733
3739
3779
8779
8179
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
31033 81791373 80171033 1033
Sample Output
670
题目大意:
这道题目是说,给你一个l和r,并且l和r都是素数,问你如何通过最小的步数使得l变成r,并且每次变化只能变一个位置的数字,且每次变化完后都是素数
解题思路:
直接bfs就好,每次将个位,十位,百位,千位的变化后的数字压入队列,然后判断是不是使我们所要求的那个数字,然后记录步数就好了。
代码:
# include<cstdio># include<iostream># include<cstring># include<queue>using namespace std;# define MAX 10000int prime[MAX];int book[MAX];int path[MAX];int l,r;void init(){ for ( int i = 2;i < 10000;i++ ) { prime[i] = 1; } for ( int i = 2;i < 100;i++ ) { if ( prime[i] ) { for ( int j = 1;i*j < 10000;j++ ) { prime[i*j] = 0; } } }}void bfs(){ queue<int>q; q.push(l); book[l] = 1; while ( !q.empty() ) { int temp = q.front(); q.pop(); if ( temp == r ) break; for ( int t = 1;t <= 1000;t*=10 ) { int d = temp; d/=t; d%=10; int rec = temp-d*t; for ( int j = 0;j <= 9;j++ ) { if ( t == 1000&&j==0 ) { continue; } if ( j!=d ) { int cur = j*t+rec; if ( prime[cur]==1&&book[cur]==0 ) { q.push(cur); path[cur] = path[temp]+1; book[cur] = 1; } } } } }}int main(void){ int t;cin>>t; while ( t-- ) { memset(book,0,sizeof(book)); memset(path,0,sizeof(path)); cin>>l>>r; init(); bfs(); cout<<path[r]<<endl; } return 0;}
0 0
- poj poj 3126 Prime Path(BFS)
- poj 3126 Prime Path(bfs搜索)
- POJ 3126 Prime Path (BFS)
- poj 3126 Prime Path(BFS)
- poj 3126 Prime Path (bfs)
- POJ 3126 Prime Path(BFS算法)
- Prime Path (poj 3126 bfs)
- POJ 3126 Prime Path(BFS)
- POJ 3126 - Prime Path(BFS)
- poj 3126 Prime Path(BFS)
- POJ 3126 Prime Path(BFS)
- POJ 3126 Prime Path (bfs、埃氏筛法)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path(bfs)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path (BFS)
- POJ 3126 Prime Path(BFS)
- 每日一题3:判断单链表是否相交
- ModelSim仿真流程
- 【Visual C++】游戏编程学习笔记之九:回合制游戏demo(剑侠客VS巡游天神)
- nyoj 60 谁获得了最高奖学金
- 解译密码
- POJ 3126 Prime Path(BFS)
- 用指针处理的一道题
- Hibernate - DetachedCriteria 的完整用法
- PTVS的内存分配失败及处理
- 魔方阵
- [LeetCode]Rotate List
- (转)设计模式-----桥接模式(Bridge Pattern)-Java
- 杨辉三角形
- C++异常规范