POJ - 3126 - Prime
先上题目
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9259 | Accepted: 5274 |
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
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
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0 题意比较简单,就是给你两个4位的素数,问你能不能经过有限的步骤将第一个素数转化为第二个素数,其中转化的要符合一下的要求:每一次只可以改变这个四位数的某一位,首位不能为0,每一次转化以后得到的数要还是素数。如果不可以转化得到目标素数,输出Impossible。
做法也是比较简单,先筛出10000以内的素数出来,然后枚举当前的这个素数可以转化的其他素数,然后进行bfs,当遇到目标素数的时候就输出步数;如果转化不了就会把可以转化的素数都转化了一遍,最后循环就会结束。这里为了能够判断是否能转化,需要标记它转化过哪些素数,转化过的素数如果再出现,就不需要再搜索这个数了。 上代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#include <map>
#define LL long long
#define MAX (10000+10)
using namespace std; bool f[MAX],M[MAX]; typedef struct
{
int l;
int st;
}S;
queue<S> q; void dedeal()
{
LL i,j,n;
n=MAX;
for(i=;i<=n;i++)
{
if(!f[i])
{
for(j=i*i;j<=n;j+=i) f[j]=;
}
}
} int check(int x,int y)
{
int t,i;
S d;
d.l=x;
d.st=;
memset(M,,sizeof(M));
while(!q.empty()) q.pop();
q.push(d);
M[d.l]=;
while(!q.empty())
{
d=q.front();
q.pop();
if(d.l==y) return d.st;
d.st++;
x=d.l;
t=x/*;
for(i=;i<;i++)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x/*+x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x/*+x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
t=x%;
for(i=;i<;i+=)
{
d.l=t+i;
if(!f[d.l] && d.l!=x && !M[d.l])
{
M[d.l]=;
q.push(d);
}
}
}
return -;
} int main()
{
int n,c,x,y;
//freopen("data.txt","r",stdin);
dedeal();
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&x,&y);
c=check(x,y);
if(c==-) printf("Impossible\n");
else printf("%d\n",c);
}
return ;
}
3126
POJ - 3126 - Prime的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
- BFS POJ 3126 Prime Path
题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...
- poj 3126 Prime Path bfs
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 3126 Prime Path 广度优先搜索 难度:0
http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...
随机推荐
- Spring:验证用户登录
利用 Spring IOC 技术实现用户登录的验证机制,对用户进行登录验证. 首先利用 Spring 的自动装配模式将 User 对象注入到控制器中,然后将用户输入的用户名和密码与系统中限定的合法用户 ...
- FFT模板——copied from hzwer
/* Welcome Hacking Wish You High Rating */ #include<iostream> #include<cstdio> #include& ...
- [python基础] Flasky-表单WTForms支持的html字段和内建函数
WTForms 支持的HTML 标准字段如表4-1 所示.表4-1 WTForms支持的HTML标准字段字段类型 说 明StringField 文本字段T ...
- bzoj2595 [Wc2008]游览计划——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2595 今天刚学了斯坦纳树,还不太会,写一道题练习一下: 参考了博客:http://www.c ...
- k-means 聚类前的数据分析
原始数据 Say you are given a data set where each observed example has a set of features, but has nolabel ...
- succ
- Java压缩技术(二) ZIP压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642298 去年整理了一篇ZLib算法Java实现(Java压缩技术(一) ZLib),一直惦记却没时间补充.今天得空,整理一下ZI ...
- 安装Windows包管理工具Chocolatey
1.开始菜单里面用PS的管理员模式打开,执行一下命令. Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object Syste ...
- Python 38 sql基础
数据库服务器中存放的是 库(文件加) .表(文件) .表里面是记录(一行数据) 增 删 改 查 1.库相关 创建------------------create databa ...
- mysql 年龄计算(根据生日字段)
mysql 年龄计算(根据生日字段) year( from_days( datediff( now( ), birthdate))) //获取年龄 now() 当前时间,精确到秒 datediff(b ...