poj3126 Prime Path(c语言)
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
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 ...
随机推荐
- 上下文管理器——with语句的实现
前言 with语句的使用给我们带来了很多的便利,最常用的可能就是关闭一个文件,释放一把锁. 既然with语句这么好用,那我也想让我自己写的代码也能够使用with语句,该怎么实现? 下面具体介绍怎样实现 ...
- 解决内存不能为read错误
解决方法 1. 命令解决方法:开始菜单,运行,输入cmd,回车,在命令提示符下输入(复制即可) :for %1 in (%windir%\system32\*.ocx) do regsv ...
- C# 连接Oracle时报错的问题
“/”应用程序中的服务器错误. 试图加载格式不正确的程序. (异常来自 HRESULT:0x8007000B) 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该 ...
- python3编写网络爬虫13-Ajax数据爬取
一.Ajax数据爬取 1. 简介:Ajax 全称Asynchronous JavaScript and XML 异步的Javascript和XML. 它不是一门编程语言,而是利用JavaScript在 ...
- vs2015添加管理员权限
在工作过程中,我可能可能遇到要求编译好的程序默认以管理员权限运行,我们可以这样操作 一.新建一个manifest文件: <?xml version="1.0" encodin ...
- Activity总结
1)Activity在app构建中的位置: 2)与window和视图体系.事件处理的关系: 3)生命周期: 4)启动方式: 5)activity管理: 6)加载管理:
- Java逻辑运算
逻辑运算是在关系运算基础之上的运算,能处理更加复杂的问题 逻辑运算的结果是 true 或 false 一.逻辑运算的种类: 在java的逻辑运算符中,有这么四类&&(短路与).& ...
- python在图片上写汉字
1.python opencv的putText只能画英文上去 2.借鉴这个https://blog.csdn.net/dcrmg/article/details/79108491 使用pil 首先,你 ...
- <数据结构与算法分析>读书笔记--运行时间计算
有几种方法估计一个程序的运行时间.前面的表是凭经验得到的(可以参考:<数据结构与算法分析>读书笔记--要分析的问题) 如果认为两个程序花费大致相同的时间,要确定哪个程序更快的最好方法很可能 ...
- MP实战系列(三)之实体类讲解
首先说一句,mybatis plus实在太好用了! mybaits plus的实体类: 以我博客的用户类作为讲解 package com.blog.entity; import com.baomido ...