Prime Path

Prime Path

时间: 1ms        内存:128M

描述:

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.

输入:

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).

输出:

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

示例输入:

3
1033 8179
1373 8017
1033 1033

示例输出:

6
7
0

提示:

参考答案(内存最优[1268]):

#include<iostream>
#include<cstdio>
#include<queue>

#define INTMAX 2147483647

using namespace std;

void play(int num, int goal, int visited[], bool isPrime[]) {
	int x, y;
	char strNum[5];
	queue <int> que;
	que.push(num);
	visited[num] = 0;
	while(!que.empty()) {
		x = que.front();
		que.pop();
		if(x == goal) return;
		for(int i=0; i<4; i++) {
			for(int j=0; j<=9; j++) {
				sprintf(strNum, "%d", x);
				strNum[i] = j + '0';
				sscanf(strNum, "%d", &y);
				if(y < 1000) continue;
				if(isPrime[y] == false) continue;
				if(visited[x]+1 < visited[y]) { visited[y] = visited[x] + 1; que.push(y); }
			}
		}
	}
}

int main(void) {
	bool isPrime[10000];
	isPrime[0] = isPrime[1] = false;
	for(int i=2; i<10000; i++) isPrime[i] = true;
	for(int i=2; i<5000; i++) {
		if(isPrime[i] == false) continue;
		for(int j=2; i*j<10000; j++) isPrime[i*j] = false;
	}
	
	int tcase, num, goal, answer;
	int visited[10000];

	cin >> tcase;
	while(tcase--) {
		cin >> num >> goal;
		for(int i=0; i<10000; i++) visited[i] = INTMAX;
		play(num, goal, visited, isPrime);
		cout << visited[goal] << endl;
	}
	return 0;
}

参考答案(时间最优[5]):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <math.h>
using namespace std;
typedef long long ll;
const int MAXN = 10000;
int isprime[MAXN];

int Prime(int n)
{
    for(int i=2;i*i<=n;i++){
        if(n%i == 0)return 0;
    }
    return 1;
}
int bfs(int l,int r)
{
    int i,j,now;
    queue <int> q;
    int ans[MAXN]={0};//、用来打标记,记录操作次数的
    q.push(l);
    ans[l] = 1;
    while(!q.empty())
    {
        now = q.front(); q.pop();
        if(now == r) return ans[now]-1;

        int t = 1000;
        int s;
        for(i=0;i<4;i++){
            if(i == 0)    s = now%1000 + now/10000*10000;
            else if(i==1) s = now%100 +now/1000*1000;
            else if(i==2) s = now%10+now/100*100;
            else if(i==3) s = now/10*10;
            for(int k = 0;k<10;k++){
                j = s + t*k;
                if(isprime[j] == 1&&ans[j] == 0){
                    q.push(j);
                    ans[j] = ans[now] + 1;
                }
            }
            t /=10;
        }
    }
    return -1;
}

int main()
{
    int t ,l,r;
    scanf("%d",&t);
    for(int i = 1000;i<=9999;i++)//标记10000以内的素数
        isprime[i] = Prime(i);
    while(t--)
    {
        scanf("%d%d",&l,&r);
        int ans = bfs(l,r);
        if(ans == -1)  printf("Impossible\n");
        else printf("%d\n", ans);
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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