1. 程式人生 > >[數論] POJ 3126 Prime Path

[數論] POJ 3126 Prime Path

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10897 Accepted: 6199

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


解題報告:

此題題意不難理解,就是求從一個素數A變到另一個素數B的路程長度,要求每個走過的數必須是素數且路程最短。

首先想到的是篩選出10000以內所有的素數,然後搜尋。

由於需要最短的路程,所以使用BFS,然後列舉每一個數即可

程式碼如下:

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

bool prime[10010];
bool visit[10010];
struct node
{
	int step;
	int prime;
}queue[10000];
int bfs(int a,int b){
	int head=0,tail=0;
	queue[tail].prime=a;
	queue[tail++].step=0;
	visit[a]=true;
	while(head<tail){
		node x=queue[head++];
		if(x.prime==b){
			return x.step;
		}
		for(int i=1;i<=9;i+=2){   //個位
			int y=(x.prime/10)*10+i;
			if(y!=x.prime && !visit[y] && !prime[y]){
				visit[y]=true;
				queue[tail].prime=y;
				queue[tail++].step=x.step+1;
			}
		}

		for(int i=0;i<=9;i++){  //十位
			int y=(x.prime/100)*100+i*10+x.prime%10;
			if(y!=x.prime && !visit[y] && !prime[y]){
				visit[y]=true;
				queue[tail].prime=y;
				queue[tail++].step=x.step+1;
			}
		}

		for(int i=0;i<=9;i++){  //百位
			int y=(x.prime/1000)*1000+i*100+((x.prime/10)%10)*10+x.prime%10;
			if(y!=x.prime && !visit[y] && !prime[y]){
				visit[y]=true;
				queue[tail].prime=y;
				queue[tail++].step=x.step+1;
			}
		}

		for(int i=1;i<=9;i++){  //千位
			int y=x.prime%1000+i*1000;
			if(y!=x.prime && !visit[y] && !prime[y]){
				visit[y]=true;
				queue[tail].prime=y;
				queue[tail++].step=x.step+1;
			}
		}
	}
	return -1;
}
int main(){
	memset(prime,0,sizeof(prime));
	prime[0]=1;prime[1]=1;
	for(int i=2;i*i<=10000;i++)
		if(!prime[i])
			for(int j=i+i;j<=10000;j+=i)
				prime[j]=1;
	int t;
	scanf("%d",&t);
	while(t--){
		memset(visit,false,sizeof(visit));
		int a,b;
		scanf("%d%d",&a,&b);
		int ans = bfs(a,b);
		if(ans==-1)
			printf("Impossible\n");
		else
			printf("%d\n",ans);
	}
}


相關推薦

[數論] POJ 3126 Prime Path

Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10897 Accepted: 6199 Description The ministers of the cabi

POJ 3126 Prime Path SPFA

queue mes primer memset 改變 data- sprint ron ble http://poj.org/problem?id=3126 題目大意: 給你兩個四位的素數s和t,要求每次改變一個數字。使得改變後的數字也為素數,求s變化到t的最少變化次

POJ 3126 Prime Path【從一個素數變為另一個素數的最少步數/BFS】

lan mem 奇數 offices ring finance primes iostream int Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted:

POJ 3126 Prime Path

ble pop rip tor 一次 門牌號 下一個 入隊 oid 題目鏈接:POJ 3126 Description 首相們對安全局長帶來的消息感到非常不安,他們說他們將為了安全考慮改變首相房間門牌號上面的門牌號碼。 ——要不時地改變門牌號,這樣可以使敵人處於迷惑之中。

POJ-3126 Prime Path 【BFS+剪枝】

題目傳送門 題目:給定兩個四位數n,m,每次改變n中的一位數並且保證改變後的n仍然是素數,問n最少要經過多少次改變才能變成m。若不論經過多少次改變都不能變成m那麼輸出Impossible。 題解:BFS,列舉改變的數。 注意:1.最後一位數一定不能是偶數,中間兩位數可以是0~9的任意

POJ 3126 Prime Path(素數,BFS最短路)

【連結】http://poj.org/problem?id=3126 【題意】給個字串,求滿足形如“E...E...E”這種鬼樣子的E串最大長度,其中“...”可以是任意個數的任意字元(沒有也ok),但是三個E之間不能重疊 【思路】  ① 掏出素數篩的模板,找到1W以內素數

poj 3126 Prime Path【bfs】【素數線性篩】

Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28338 Accepted: 15457 Description The ministers

POJ-3126 Prime Path 【BFS】

題目傳送門 題目:給定兩個四位數n,m,每次改變n中的一位數並且保證改變後的n仍然是素數,問n最少要經過多少次改變才能變成m。若不論經過多少次改變都不能變成m那麼輸出Impossible。 題解:BFS,列舉改變的數。 注意:1.最後一位數一定不能是偶數,中間兩位數可以

POJ 3126 Prime Path 素數+最短路

題目大意是讓你求素數x到素數y的最短路徑距離,每次只能對素數中的一位數進行改變,每次改變1英鎊,求最小花費。 則素數a-b相連當且僅當a與b有一位數字不同。從起點開始bfs,若終點可達,即得到答案,不可達則輸出Impossible。 Prime Path Time Li

哈理工練習賽 杭電 HDU Prime Path 1973 poj 3126 Prime Path

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 num

(BFS11.1.1)POJ 3126 Prime Path(計算從初始素數到目標素數的最短路徑長度)

/* * POJ_3126.cpp * * Created on: 2013年11月7日 * Author: Administrator */ #include <ios

poj 3126 Prime Path (線性素數篩 + bfs)

Prime Path Time Limit: 1000MS Memory Limit: 65536K Description The ministers of the cabinet were quite upset by the mes

POJ 3216 Prime Path(寬搜+篩素數)

POJ 3216 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they woul

Prime Path (打表+BFS) POJ-3126

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

Prime Path POJ - 3126 bfs 暴力

這題求最短路 ,所有的狀態轉移為 9999-999, 不超過10000,最多100個test case, 最壞的時間複雜度為 10e6,絕對不會超時的。 因為求最短路,要麼列舉所有 dfs路徑, 要麼使用 bfs ,不過我同學說求最短路一般bfs要比純dfs快,但是bfs 會消耗更多的記

Prime Path POJ - 3126 bfs

剛開始在執行函式中間不小心打錯成了que[rear].s 修正了以後發現各種輸出0emmm 要用rear-1啊 簡單的廣搜求最短,列舉素數 #include<stdio.h> #include<string.h> int pd[10005]; int b

poj 3126Prime Path 題意&題解&程式碼(C++)

變換的過程要保證 每次變換出來的數都是一個 四位素數,而且當前這步的變換所得的素數 與 前一步得到的素數 只能有一個位不同,而且每步得到的素數都不能重複。 求從a到b最少需要的變換次數。無

【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宮問題】

POJ3126Prime Path 給定兩個四位素數a  b,要求把a變換到b 變換的過程要 每次變換出來的數都是一個 四位素數,而且當前這步的變換所得的素數  與  前一步得到的素數  只能有一個位不同,而且每步得到的素數都不能重複。  

POJ 2689 Prime Distance [篩法選取素數]【數論

題目連結:http://poj.org/problem?id=2689 ————————-. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1

POJ 3518 Prime Gap(素數)

for org 篩選法求素數 lan article sizeof tar eof rim POJ 3518 Prime Gap(素數) http://poj.org/problem?id=3518 題意: 給你一個數。假設該數是素數就輸出0. 否則輸出比