POJ 3126 math(BFS)

Prime Path

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21581   Accepted: 11986

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.

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

Source

Northwestern Europe 2006

思路:对每位数字替换,直到成为目标数字。

代码:

 1 #include "cstdio"
 2 #include "iostream"
 3 #include "algorithm"
 4 #include "string"
 5 #include "cstring"
 6 #include "queue"
 7 #include "cmath"
 8 #include "vector"
 9 #include "map"
10 #include "stdlib.h"
11 #include "set"
12 #define mj
13 #define db double
14 #define ll long long
15 using namespace std;
16 const int N=1e4+5;
17 const int mod=1e9+7;
18 const ll inf=1e16+10;
19 bool pri[N];
20 bool u[N],v[N];
21 int c[N];//统计步数
22 void init(){
23     int i,j;
24     for(i=1000;i<=N;i++){
25         for(j=2;j<i;j++)
26             if(i%j==0){
27                 pri[i]=0;
28                 break;
29             }
30         if(j==i) pri[i]=1;
31     }
32 }
33 int bfs(int s,int e){
34     queue<int >q;
35     memset(v,0, sizeof(v));
36     memset(c,0, sizeof(c));
37     int tmp,a[4],ans=s;
38     q.push(s);
39     v[s]=1;
40     while(q.size()){
41         int k=q.front();
42         q.pop();
43         a[0]=k/1000,a[1]=k/100%10,a[2]=k/10%10,a[3]=k%10;
44         for(int i=0;i<4;i++){
45             tmp=a[i];
46             for(int j=0;j<10;j++){
47                 if(tmp!=j){
48                     a[i]=j;
49                     ans=a[0]*1000+a[1]*100+a[2]*10+a[3];
50                     if(pri[ans]&&!v[ans]){//为素数且未使用过
51                         c[ans]=c[k]+1;
52                         v[ans]=1;
53                         q.push(ans);
54                     }
55                     if(ans==e) {
56                         printf("%d\n",c[ans]);
57                         return 0;
58                     }
59                 }
60                 a[i]=tmp;
61             }
62         }
63         if(k==e) {
64             printf("%d\n",c[k]);
65             return 0;
66         }
67     }
68     printf("Impossible\n");
69     return 0;
70 }
71 int main()
72 {
73     int n;
74     int x,y;
75     scanf("%d",&n);
76     init();
77     for(int i=0;i<n;i++){
78         scanf("%d%d",&x,&y);
79         bfs(x,y);
80     }
81     return 0;
82 }
时间: 2024-11-10 01:11:57

POJ 3126 math(BFS)的相关文章

POJ 1562-Oil Deposits(BFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 6888 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

poj 4105 拯救公主(bfs)

原题网址:http://bailian.openjudge.cn/practice/4105/ 思路: 每个位置包括的状态:所在的位置,获得的宝石. 广搜:用队列存储达到某个位置时,获得的宝石完全相同的最少用时. 传送门另外考虑即可. 详细代码: 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 const int INF = 0x44444444

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

poj 2309 BST(数学题)

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8440   Accepted: 5093 Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we c

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

poj 1724 ROADS(dfs)

http://poj.org/problem?id=1724 大致题意:N个城市由R条单向路连通,每条路(S,D)之间有两个因素:路的长度L和路的花费T.现要从城市1到达城市N,求花费在K以内的最短路程. 思路:很明显的dfs(他们都说很明显的spfa...).不过dfs有几点注意的地方: 建立邻接表不能用vector存,要用链表的形式,采用头插法. dfs的时候,在递归节点v之前,要先预判断一下到达v之后总花费是否大于k,若大于K就跳过,不必再调用v节点,这样会省很多时间.对于路程的处理也同样

Z1. 广度优先搜索(BFS)解题思路

/** BFS 解题思路 特点:从某些特定的节点开始,感染相邻的节点; 被感染的节点,再感染其相邻的节点,以此类推. 题目常见于数据结构包括 二维数组.树.图 **/ /** 1). 二维数组特定节点感染相邻的节点,即上下左右四个方向,可设定变化数组如下 int[] dr = new int[]{-1, 0, 1, 0}; int[] dc = new int[]{0, -1, 0, 1}; 2). 二维数组特定节点感染包围它的节点,即八个方法, 可设定变化数组如下: int[] dr = ne