poj 3126 Prime Path
Description

— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
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
1A~~爽
题意:就是给你两个素数只有四位的a,b,问a如何转换能到达b,转换规则是只能转换一个数字的一位,且每次转换后该数依旧是素数
思路:将1000-10000的素数打表,然后BFS,一个一个的枚举。
#include<stdio.h> #include<string.h> #include<queue> using namespace std; const int MAXN=10000; int vis1[MAXN],vis2[MAXN]; void init() { int i,j; memset(vis1,0,sizeof(vis1)); for(i=2;i<MAXN;i++) { if(!vis1[i]) { for(j=i*2;j<MAXN;j+=i) vis1[j]=1; } } } int BFS(int a,int b) { int step[MAXN]; int s,head,next; memset(step,0,sizeof(step)); memset(vis2,0,sizeof(vis2)); queue<int>Q; Q.push(a); vis2[a]=1; while(!Q.empty()) { head=Q.front(); Q.pop(); if(head==b) return step[head]; for(int i=1;i<=4;i++) { if(i==1) s=1; else s=0; for(int j=s;j<=9;j++) { if(i==1) next=j*1000+head%1000; else if(i==2) next=head/1000*1000+j*100+head%100; else if(i==3) next=head/100*100+j*10+head%10; else next=head/10*10+j; if(vis1[next]==1) continue; if(!vis2[next]) { step[next]=step[head]+1; if(next==b) return step[next]; vis2[next]=1; Q.push(next); } } } } return -1; } int main() { int T,a,b; init(); scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); int ans=BFS(a,b); if(ans>=0) printf("%d\n",ans); else printf("Impossible\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异