您的位置:首页 > 其它

Prime Path - POJ 3126 BFS宽度(广度)优先搜索

2014-04-15 18:51 549 查看
Prime Path

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 10483Accepted: 5992
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



题意为给你t组数据,然后每组给你a,b两个数,问让你从a改到b需要多少次,要求每次只能改动1个数字,且每次改成的数必须为素数。

本题运用BFS宽度(广度)优先搜索的方法。用一个queue的栈,然后每次从栈中提取出最前面的数,再尝试这个数可以改成什么样的素数,并记录改成这个素数需要多少次(即提出的那个数的改动次数+1),搜索完后删除栈中最前面的那个数。重复上述过程直到搜索到了b数。过程中用数组p[]记录改变到这个素数需要多少次,visited[]用于记录已经搜索过的素数。

代码如下。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int f[10010],p[10010],visited[10010];
//f[]用于快速求素数,p[]记录改变到这个素数需要多少次,visited[]用于记录已经搜索过的素数
int BFS(int a,int b)
{ queue<int> q;
  q.push(a);
  p[a]=0;
  visited[a]=1;
  while(!q.empty())
  { int temp=q.front();        //得到qu最前方的素数
    q.pop();
    for(int i=0;i<=9;i++)
    { int y1=(temp/10)*10+i;
      //个位的改变
      if(f[y1]==0 && visited[y1]==0)//判断这个数是否是素数且是否搜索过
      { q.push(y1);                 //是的话将其放入qu的最后
        p[y1]=p[temp]+1;            //改成这个素数的需要的次数是上一个素数的次数+1
        visited[y1]=1;              //记录改数搜索已过
      }
      //十位的改变
      int y2=(temp/100)*100+temp%10+i*10;
      if(f[y2]==0 && visited[y2]==0)
      { q.push(y2);
        p[y2]=p[temp]+1;
        visited[y2]=1;
      }
      //百位的改变
      int y3=(temp/1000)*1000+temp%100+i*100;
      if(f[y3]==0 && visited[y3]==0)
      { q.push(y3);
        p[y3]=p[temp]+1;
        visited[y3]=1;
      }
      //千位的改变
      if(i!=0)
      { int y4=temp%1000+i*1000;
        if(f[y4]==0 && visited[y4]==0)
        { q.push(y4);
          p[y4]=p[temp]+1;
          visited[y4]=1;
        }
      }
    if(visited[b])   //如果搜素到想要改成的那个数的话,就返回需要的次数
     return p[b];
    }
  }
  return 0;
}
int main()
{ int i,j,k,n,a,b;
  //快速求素数
  for(i=2;i<=10010;i++)
  { if(!f[i])
    for(k=2;k*i<10010;k++)
     f[k*i]=1;
  }
  scanf("%d",&n);
  while(n--)
  { memset(p,0,sizeof(p));
    memset(visited,0,sizeof(visited));
    scanf("%d%d",&a,&b);
    printf("%d\n",BFS(a,b));
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: