poj3126 Prime Path(c语言)
Description
![](http://poj.org/images/3126_1.jpg)
— 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 ...
随机推荐
- HDU ACM 1856 More is better(并查集)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1856 [解题思路]给的数据有点大,干脆少开点数组,直接上set存储有朋友的孩子的编号,同时根据编号初 ...
- 【转】handbrake使用教程
原文地址http://tieba.baidu.com/p/2399590151?pn=1 现在的很多压制教程基本都是使用megui或者mediacoder的,这两个软件使 ...
- [BUG]Appium1.9.1 这个问题竟然花了我5分钟进行定位
1.先上问题,知道是什么问题先 EE ====================================================================== ERROR: tes ...
- Sublime Text3如何快速预览html文件
Sublime Text3 步骤1:选择 Tools----> Build System ----> New Build System... 步骤2:输入以下内容 "cmd&qu ...
- span 文本内容超过宽度自动换行
span{word-break:normal; width:auto; display:block; white-space:pre-wrap;word-wrap : break-word ;over ...
- P2347 砝码称重 (01背包)
题目描述 设有 1g1g1g . 2g2g2g . 3g3g3g . 5g5g5g . 10g10g10g . 20g20g20g 的砝码各若干枚(其总重 ≤1000 \le 1000≤1000 ), ...
- Rman将数据文件恢复到不同的路径
RMAN> startup nomount connected to target database (not started)Oracle instance started Total Sys ...
- oracle(sql)基础篇系列(五)——PLSQL、游标、存储过程、触发器
PL/SQL PL/SQL 简介 每一种数据库都有这样的一种语言,PL/SQL 是在Oracle里面的一种编程语言,在Oracle内部使用的编程语言.我们知道SQL语言是没有分支和循环的,而PL语言是 ...
- Google的Flutter工具允许开发者开发跨平台应用
与大多数应用程序开发人员交谈,他们会告诉你,与iOS相比,制作Android应用程序要困难得多,也更复杂,也不那么有趣.实际上,如果你要求报价,这两种软件都将单独定价,因为它们都需要单独的开发时间和团 ...
- Tensorflow[LSTM]
0.背景 通过对<tensorflow machine learning cookbook>第9章第3节"implementing_lstm"进行阅读,发现如下形式可以 ...