poj1741,POJ:3126-Prime Path

 2023-12-25 阅读 32 评论 0

摘要:題目鏈接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25215 Accepted: 13889 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating tha

題目鏈接:http://poj.org/problem?id=3126


Prime Path

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25215 Accepted: 13889

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.

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

poj2352、3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0


解題心得:

  1. 題意就是一個長官的房間號是n(四位數),你每次可以改變n中的一個數,要你改變次數最少將n變成m,并且在改變工程中所有的數字都必須是一個素數,并且都是沒有前導零的四位數,如果不能通過這樣的改變達到m則輸出-1;
  2. 就是一個搜索加素數判斷,直接bfs,只不過在入隊的時候判斷一下是否是一個素數就行了。

#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1e4+100;
bool prim[maxn];
int n,m;struct NODE {int va,step;NODE() {va = 0,step = 0;}
}now2,Next2;void get_prim() {prim[0] = prim[1] = true;for(int i=2;i<maxn;i++) {if(prim[i])continue;for(int j=i*2;j<maxn;j+=i) {prim[j] = true;}}
}int bfs() {bool vis[maxn];int now[6],Next[6];memset(vis,0, sizeof(vis));vis[n] = true;queue <NODE> qu;now2.va = n,now2.step = 0;qu.push(now2);while(!qu.empty()) {int temp = qu.front().va;int step = qu.front().step;qu.pop();if(temp == m)return step;int k = 0;while(k < 4) {now[k] = temp%10;temp /= 10;k++;}for(int i=0;i<4;i++) {for(int j=1;j<=9;j++) {for(int k=0;k<4;k++)Next[k] = now[k];Next[i] = (now[i] + j) % 10;int num = 0;for(int k=3;k>=0;k--) {num = num*10 + Next[k];}if(!prim[num] && !vis[num] && num >= 1000) {Next2.step = step+1;Next2.va = num;qu.push(Next2);vis[num] = true;}}}}return -1;
}int main() {int t;scanf("%d",&t);get_prim();while(t--) {scanf("%d%d",&n,&m);int ans = bfs();if(ans != -1)printf("%d\n",ans);elseprintf("Impossible\n");}return 0;
}

轉載于:https://www.cnblogs.com/GoldenFingers/p/9107133.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/194969.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息