View Code

POJ 3126 Prime Path( 广搜 )

Prime Path

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12974   Accepted: 7342

Description

图片 1The
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

题目大意:给两个素数a,b(是4位数),问a是否能通过变换,变成b,变换原则:一次只能改变a的其中一位数字,并且转换后的数字必须也是素数,如1033能变成1733,但不能变成1233(因为1233不是素数),也不能变成3733(因为从1033到3733一次变换了2位数),若能,输出最少的变换次数,否则输出Impossible。
广搜,判断是否是素数可以先打表。

#include 
#include 
#include 
using namespace std;
#define HUR 100
#define THO 1000
#define TEN 10
const int maxn=10000;
bool prime[maxn+5];
int vis[maxn],s,e;

void prime_table(){
 int i,j;
 memset(prime,0,sizeof(prime));
 for(i=2;i que;
 que.push(s);
 while(!que.empty()){
  int t=que.front();
  que.pop();
  int d=t;
  d%=1000;
  for(i=1;i<10;i++){
   int tt=d+i*THO;  //变换千位
   if(prime[tt]==0 && vis[tt]==0){
    if(tt==e) return vis[t];
    que.push(tt);vis[tt]=vis[t]+1;
   }
  }
  d=t%100+(t/1000*1000);
  for(i=0;i<10;i++){
   int tt=d+i*HUR;  //变换百位
   if(prime[tt]==0 && vis[tt]==0){
    if(tt==e) return vis[t];
    que.push(tt);vis[tt]=vis[t]+1;
   }
  }
  d=t%10+t/100*100;
  for(i=0;i<10;i++){
   int tt=d+i*TEN;  //变换十位
   if(prime[tt]==0 && vis[tt]==0){
    if(tt==e) return vis[t];
    que.push(tt);vis[tt]=vis[t]+1;
   }
  }
  d=t/10*10;
  for(i=0;i<10;i++){
   int tt=d+i;   //变换个位
   if(prime[tt]==0 && vis[tt]==0){
    if(tt==e) return vis[t];
    que.push(tt);vis[tt]=vis[t]+1;
   }
  }
 }
 return 0;
}

int main()
{
 int T,res;
 prime_table();
 scanf("%d",&T);
 while(T--){
  scanf("%d%d",&s,&e);
  if(s==e){
   printf("0\n");
   continue;
  }
  res=bfs();
  if(res==0)
   printf("Impossible\n");
  else
   printf("%d\n",res);
 }
 return 0;
}

http://www.bkjia.com/cjjc/994494.htmlwww.bkjia.comtruehttp://www.bkjia.com/cjjc/994494.htmlTechArticlePOJ 3126 Prime Path( 广搜 ) Prime Path Time
Limit: 1000MS Memory Limit: 65536K Total Submissions: 12974 Accepted:
7342 Description The ministers of the cabinet were quite upset
b…

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <cmath>
 4 #include <cstring>
 5 using namespace std;
 6 int visited[10000];
 7 queue <int> vi;
 8 int q;
 9 int step[10000];
10 bool isprim(int n)//判断是否素数;
11 {
12     for (int i = 2; i <= sqrt(n*1.0); ++i)
13     {
14     if (n % i == 0)
15     return false;
16     }
17     return true;
18 }
19 int main()
20 {
21     int t,m,n,first,next,temp;
22     cin>>t;
23     while(t--)
24     {
25         memset(visited,0,sizeof(visited));
26         memset(step,0,sizeof(step));
27         cin>>m>>n;
28         vi.push(m);
29         step[m]=0;
30         visited[m]=1;
31         while(!vi.empty())
32         {
33             first = vi.front();
34             vi.pop();
35             // if(first == n)break;(由于多了这一条,错了很久,头疼死了)
36             for(int i = 0; i <= 9; i ++)//对数据变动,由1到9循环;
37             {
38                 for(int j = 1; j <=4; j ++)//个十百千位分别各自变动;
39                 {
40                     if(j == 1 && i!= 0)//千位不能为0
41                     {
42                         next = i*1000+(first -first/1000*1000);//这个式子我想不出来是怎么写出来的
43                         if(isprim(next) && !visited[next])//判断千位变动是否为素数,是否出现过,如果是素数且未出现过,加入队列,标记;以下同这一样;
44                         {
45                             vi.push(next);
46                             visited[next]=1;
47                             step[next]=step[first]+1;
48                         }
49                     }
50                     if(j == 2)
51                     {
52                         next = first/1000*1000+i*100+(first - first/100*100);
53                         if(isprim(next) && !visited[next])
54                         {
55                             vi.push(next);
56                             visited[next]=1;
57                             step[next]=step[first]+1;
58                         }
59                     }
60                     if(j ==3)
61                     {
62                         next = first/100*100+i*10+(first-first/10*10);
63                         if(isprim(next) && !visited[next])
64                         {
65                             vi.push(next);
66                             visited[next]=1;
67                             step[next]=step[first]+1;
68                         }
69                     }
70                     if(j == 4)
71                     {
72                         next = first/10*10+i;
73                         if(isprim(next)&& !visited[next])
74                         {
75                             vi.push(next);
76                             visited[next]=1;
77                             step[next]=step[first]+1;
78                         }
79                     }
80                 }
81             }
82                 if(visited[n])//判断n出现时在第几步;即访问了的时候(visited[n]==1)
83                 temp = step[n];
84             }
85             cout<<temp<<endl;
86         }
87 }

图片 2图片 3

 

这个题意是计算出从输入的第一个数开始到第二个数结束之间的素数(每一个素数都只改变一个数字),每改变一次就花掉1pound,输出总共要花的pounds。使最少的步骤成为第二个数据。要解决的主要问题就在于怎样确定那个要改变的素数与上一个数只有一个数字不同。

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注