1. 程式人生 > >POJ 3126 Prime Path 素數+最短路

POJ 3126 Prime Path 素數+最短路

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

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

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
int isprime( int n){
    int i,t;
    t=sqrt(n+0.0);
    for (i=2;i<=t;i++){
        if (n%i==0) return -2;
    }
    return -1;
}//prime:-1; not prime:-2; arrived:>=0;
const int S=10050;
int prime[S],f[S],w[4];
int main(){
    int i,T,x,y,tmp,a,b,c,d,t;
    for (i=2;i<S;i++) prime[i]=isprime(i);
    cin>>T;
    while (T--){
        cin>>x>>y;
        memcpy(f,prime,S*sizeof(int));
        f[x]=0;
        queue<int> q;
        q.push(x);
        while (!q.empty()&&(f[y]==-1))
        {
            t=q.front();
            tmp=t;
            q.pop();

            for (i=0;i<=3;i++) {w[i]=tmp%10;tmp/=10;}
            //ge_wei
            for (i=0;i<=9;i++)
            {
                tmp=i+10*w[1]+100*w[2]+1000*w[3];
                if (f[tmp]==-1) {
                    f[tmp]=f[t]+1;
                    q.push(tmp);
                }
            }
            //shi_wei
            for (i=0;i<=9;i++)
            {
                tmp=w[0]+10*i+100*w[2]+1000*w[3];
                if (f[tmp]==-1) {
                    f[tmp]=f[t]+1;
                    q.push(tmp);
                }
            }
            for (i=0;i<=9;i++)
            {
                tmp=w[0]+10*w[1]+100*i+1000*w[3];
                if (f[tmp]==-1) {
                    f[tmp]=f[t]+1;
                    q.push(tmp);
                }
            }
            for (i=1;i<=9;i++)
            {
                tmp=w[0]+10*w[1]+100*w[2]+1000*i;
                if (f[tmp]==-1) {
                    f[tmp]=f[t]+1;
                    q.push(tmp);
                }
            }

        }
        if (f[y]!=-1) cout<<f[y]<<endl;else cout<<"Impossible\n";
    }



    return 0;
}

相關推薦

POJ 3126 Prime Path 素數+短路

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

POJ 3126 Prime Path素數,BFS短路

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

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

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

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【bfs】【素數線性篩】

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

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

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】

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

[數論] POJ 3126 Prime Path

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

哈理工練習賽 杭電 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

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

POJ 3518 Prime Gap(素數)

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

POJ - 1062 昂貴的聘禮(短路Dijkstra)

方案 ref pst 思路 記錄 ron inpu 一個點 != 昂貴的聘禮 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Subm

POJ - 2253 Frogger(Floyd短路+預處理)

最短路 pri str 之間 col ace blank scanf oid 題目鏈接:http://poj.org/problem?id=2253 題意:青蛙要從點1到點2,給出各點的坐標,如果點A到點B可以通過A->C,C->B,A到B的距離可以用A-&g

POJ-1860 Currency Exchange (短路

條件 n) fin inline AS 我們 fde 思想 %d https://vjudge.net/problem/POJ-1860 題意 有多種匯幣,匯幣之間可以交換,這需要手續費,當你用100A幣交換B幣時,A到B的匯率是29.75,手續費是0.39,那麽你可以

POJ 3660 Cow Contest(Floyd短路)

Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10780 Accepted: 6003 Description N (1 ≤ N ≤ 100) cows,

Til the Cows Come Home ( POJ 2387) (簡單短路 Dijkstra)

problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

Aizu - GRL_1_C Shortest Path - All Pairs Shortest Path短路 floyd)

題目連結 題意:給出n個點m條路,求是否存在負環;不存在的輸出點之間的距離; 注意:會超int,要用long long 型; 判斷負環:floyd迴圈完一遍,如果存在dis[i][i]<0,證明存在負環;     #include <iostr