POJ-3126-Prime Path【BFS】
2016-03-23 08:30
344 查看
3126-Prime Path
DescriptionThe 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.
1033
1733
3733
3739
3779
8779
8179
The 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.
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
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
题目链接:POJ-3126
题目大意:
条件:四位数(没有前导0)且为素数
给出一个初始的数字a,每次能改变一位数字,使得这个新数字也满足条件,问最少需要几次到达b这个数字
题目思路:直接BFS就可以了,4位数字分别改变的情况(0 ~ 9)搜。需要注意中间数字也是要满足条件。
以下是代码:
// // F.cpp // 搜索 // // Created by pro on 16/3/22. // Copyright (c) 2016年 pro. All rights reserved. // #include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> #include <queue> using namespace std; int vis[10000]; struct node { int val,cnt; }; int prm[100000]; //记录各个素数 const int MAXV = 1e5; bool isPrime[MAXV+1]; //判断该数字是不是素数 int size=0; //表示prm数组的大小 void getPrime() { memset(isPrime, true, sizeof(isPrime)); int sq = sqrt((double)MAXV) + 1; int i,j,k; for(i = 2;i <= sq; i++) if(isPrime[i]) for(j = 2,k = MAXV/i+1;j < k;j++) isPrime[i*j] = false; for( i = 2 ; i <= MAXV; i++) if(isPrime[i]) prm[size++] = i; isPrime[0] = isPrime[1] = false; } int getNum (int num,int i) { if (i == 1) return num % 10; else if (i == 2) return num / 10 % 10; else if (i == 3) return num / 100 % 10; else return num / 1000; } int bfs(int begin,int end) { queue<node> que; node zero; zero.val = begin; zero.cnt = 0; que.push(zero); vis[begin] = 1; while(!que.empty()) { node front = que.front(); que.pop(); if (front.val == end) return front.cnt; for (int i = 1; i <= 4; i++) { int num = getNum(front.val,i); for (int j = 0; j < 10; j++) { if (j != num) { int new_num = front.val + (j - num) * pow(10,i - 1); if (new_num < 1000) continue; if (isPrime[new_num] && !vis[new_num]) { vis[new_num] = 1; node next; next.val = new_num; next.cnt = front.cnt + 1; que.push(next); } } } } } return 0; } int main() { int t; cin >> t; getPrime(); while(t--) { memset(vis,0,sizeof(vis)); int begin,end; cin >> begin >> end; cout << bfs(begin,end) << endl; } }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- Surrounded Regions