POJ 3126 Prime Path

文章讲述了如何使用广度优先搜索(BFS)算法和素数筛选技术,在四位数素数间找到改变一位数字的最廉价路径,以帮助总理更换办公室门号,同时最小化成本。
摘要由CSDN通过智能技术生成

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 53655 Accepted: 27384
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

由于本人刚开始学习BFS算法,不太熟练,代码细节实现不充分以及等奇怪的原因导致这题写了整整2个小时。。。还是菜。。。

解题思路: 利用BFS宽度优先搜索(求最优解的时候常用)+ 素数筛。素数筛这里不详细讲了。由一个数字 可以通过变换每一位的数字得到8×9×9×9种情况(首位不取0).再选出其中的素数情况 代价+1 。为保证求的代价最小,我们开一个访问数组vis来判断这些数字已经走到了。(毕竟好马不吃回头草,写题时忘记这一步了 调试了好久。。。)然后由新得到的数字再进行扩散得到一批新的数字 代价+1。不难看出step[新数] = step[老数] + 1。 这里用到队列(FIFO)的思想将最开始的数字入队 ,扩散得到满足的数字逐个入队。最后将最开始的数字出队,逐个从队伍中取数字扩散,进行入队,出队操作。结束条件为已经走到目标数字或者队伍空了(目标数字不可能产生)。
注意: 题目包含多个数据,记得每个样例后要初始化 vis 和 step;

AC代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int MaxN = 10010;
int prime[MaxN + 1];
int isprime[MaxN + 1];
int x, y;
int step[MaxN + 1];
int vis[MaxN+1];
void aisi() {
	int cnt = 0;
	for (int i = 2; i <= MaxN; i++) {
		if (!isprime[i] && i <= 10000) {
			for (int j = i + i; j <= MaxN; j += i) {
				isprime[j] = 1;
			}
		}
	}
}
int queue[MaxN];
void BFS(int x, int y) {
	memset(vis,0,sizeof(vis));
	memset(step,0,sizeof(step));
	int num[4];
	int temp = x;
	num[0] = temp / 1000;
	temp %= 1000;
	num[1] = temp / 100;
	temp %= 100;
	num[2] = temp / 10;
	temp %= 10;
	num[3] = temp;
	int front = 0, rear = 1;
	queue[front] = x;
	int a = x;
	int flag = 1;
	while (rear - front) {
			temp = a;
				num[0] = temp / 1000;
				temp %= 1000;
				num[1] = temp / 100;
				temp %= 100;
				num[2] = temp / 10;
				temp %= 10;
				num[3] = temp;	
		//改变个位数字
		for (int i = 0; i <= 9; i++) {
			if (i == num[3])	continue;
			temp = a + i - num[3];
			if (!isprime[temp] && !vis[temp]) {
				vis[temp] = 1;
				queue[rear++] = temp;
				step[temp] = step[a] + 1;	
				if (temp == y) {
					printf("%d\n", step[temp]);
					flag = 0;
					break;
				}
			}
		}
		if (flag == 0) break;
		for (int i = 0; i <= 9; i++) {
			if (i == num[2])	continue;
			temp = a + (i - num[2]) * 10;
			if (!isprime[temp] && !vis[temp]) {
				vis[temp] = 1;
				queue[rear++] = temp;
				step[temp] = step[a] + 1;
				if (temp == y) {
					printf("%d\n", step[temp]);
					flag = 0;
					break;
				}

			}
		}
		if (flag == 0) break;
		for (int i = 0; i <= 9; i++) {
			if (i == num[1])	continue;
			temp = a + (i - num[1]) * 100;
			if (!isprime[temp] && !vis[temp]) {
				vis[temp] = 1;
				queue[rear++] = temp;
		
				step[temp] = step[a] + 1;
				if (temp == y) {
					printf("%d\n", step[temp]);
					flag = 0;
					break;
				}
			}
		}
		if (flag == 0) break;
		for (int i = 1; i <= 9; i++) {
			if (i == num[0])	continue;
			temp = a + (i - num[0]) * 1000;
			if (!isprime[temp] && !vis[temp]) {
				vis[temp] = 1;
				queue[rear++] = temp;
				step[temp] = step[a] + 1;
				if (temp == y) {
					printf("%d\n", step[temp]);
					flag = 0;
					break;
				}
			}
		}
		if (flag == 0) break;
		front++;
		a = queue[front];
	}
}
int main() {
		aisi();
		
	int n;
	scanf("%d", &n);
	while (n--) {
		scanf("%d %d", &x, &y);
		if (x == y) {
			printf("0\n");
			continue;
		}
		BFS(x, y);
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值