poj3126 Prime Path(c语言)
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.
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
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0 题意 将a转换到b,每次换一位,每次交换后的数都是素数,算最短换了多少次
我的思路
找到所有的四位数的素数,打表
用bfs 有四十个方向,找到答案
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int prime[],n,m,tail,head;
struct que
{
int x,time;
}que[];
int row(int x)
{
int i,s=;
for(i=;i<=x;i++)
s*=;
return s;
}
int bfs()
{
int i,j,x;
for(i=;i<=;i++)
{
for(j=;j<=i/;j++)
if(i%j==)break;
if(j==i/+)prime[i]=;
else prime[i]=;
}
prime[n]=;
tail=head=;
que[].x=n,que[].time=;
while(head<=tail)
{
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
x=que[head].x-(que[head].x/row(i-)%)*row(i-)+j*row(i-);
if(x>=&&x<=&&prime[x])
{
que[++tail].x=x;
que[tail].time=que[head].time+;
if(x==m)return que[tail].time;
else prime[x]=;
}
}
}
head++;
}
return ;
}
int main()
{
int i,j,key,g;
while(scanf("%d",&g)==)
{
for(i=;i<g;i++)
{
scanf("%d %d",&n,&m);
if(n==m)
{
printf("0\n");
continue;
}
key=bfs();
if(key)
printf("%d\n",key);
else
printf("Impossible\n");
}
}
return ;
}
poj3126 Prime Path(c语言)的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path
http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- POJ3126——Prime Path
非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
随机推荐
- python之字符串的常用操作(转)
1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + str2) >> ...
- January 02nd, 2018 Week 01st Tuesday
I dream my painting, and then I paint my dream. 我梦见我的画,然后我画我的梦. It was a long time after I had a goo ...
- Cannot uninstall 'pyserial'. It is a distutils installed project and thus we cannot a ccurately determine which files belong to it which would lead to only a partial uninstall. 解决方法
最近再升级 pyserial模块时,采用 pip install --upgrade pyserial,待模块下载完成准备卸载原版本时 提示:“Cannot uninstall 'pyserial'. ...
- node爬虫扒小说
Step 1: 万年不变的初始化项目,安装依赖 cnpm i express cheerio superagent superagent-charset async -S express 就不用多说 ...
- Sublime Text 3安装及常用插件安装
一.Sublime3下载 1.百度搜索Sublime3 download,选择进入下载页面 2.我选择下载Win64位安装程序 二.Sublime3安装 傻瓜式安装,一直点下一步即可. 三.Subli ...
- 关于RBAC(Role-Base Access Control)的理解(转)
基于角色的访问控制(Role-Base Access Control) 有两种正在实践中使用的RBAC访问控制方式:隐式(模糊)的方式和显示(明确)的方式. 今天依旧有大量的软件应用是使用隐式的访问控 ...
- Recurrences UVA - 10870 (斐波拉契的一般形式推广)
题意:f(n) = a1f(n−1) + a2f(n−2) + a3f(n−3) + ... + adf(n−d), 计算这个f(n) 最重要的是推出矩阵. #include<cstdio> ...
- Hue添加Spark notebook
参考自https://blogs.msdn.microsoft.com/pliu/2016/06/18/run-hue-spark-notebook-on-cloudera/ 说明 使用Clouder ...
- 小度之家SDK功能介绍
https://developer.dueros.baidu.com/doc/device-devkit/intro_markdown
- git fork之如何同步更新
1.打开git fork的开源项目(这里我以git fork vscode为例): 2.点击New pull request 3.选择你自己的 4.选择完后出现如图 5.调整状态,右边改为源fork地 ...