Prime Path
poj3126:http://poj.org/problem?id=3126
题意:给你两个数n,k,两个数都是四位数的素数。现在让你改变n的一位数,让n变成另外一个素数。然后把这个素数在改变其中的以为又变成一个新的素数,问你最少有这样变换几步,才能使得使他变成k。
题解:求最短的问题,很自然的就想到了BFS,此外这一题还要处理10000以内的素数,可以采用素数筛法。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int prime[];//标记该数是不是素数
struct Node{
int x;
int step;
};
int counts[];
int n,k;
void DealPrime(){//素筛
memset(prime,,sizeof(prime));
for(int i=;i<=;i++){
if(prime[i])
for(int j=*i;j<=;j+=i)
prime[j]=;
}
}
int BFS(int x){
queue<Node>Q;
for(int i=;i<=;i++)
counts[i]=;
Node tt;
tt.x=x;
tt.step=;
Q.push(tt);
counts[x]=;
while(!Q.empty()){
Node temp=Q.front();
Q.pop();
int xx=temp.x;
int a=xx%;//取出个位
int b=xx/%;//十位
int c=xx/%;//百位
int d=xx/;//千位
int step=temp.step;
for(int i=;i<=;i++){//个位的变换
int xxx=d*+c*+b*+i;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//十位的变换
int xxx=d*+c*+i*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//百位的变换
int xxx=d*+i*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
for(int i=;i<=;i++){//千位的变换,注意千位不能为0
int xxx=i*+c*+b*+a;
if(prime[xxx]&&counts[xxx]>step+){
counts[xxx]=step+;
Node t;
t.x=xxx;
t.step=step+;
Q.push(t);
}
}
}
return counts[k];
}
int main(){
int cas;
scanf("%d",&cas);
DealPrime();
while(cas--){
scanf("%d%d",&n,&k);
int ans=BFS(n);
if(ans==)
printf("Impossible\n");
else
printf("%d\n",ans);
} }
Prime Path的更多相关文章
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- Prime Path(POJ 3126 BFS)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15325 Accepted: 8634 Descr ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- Prime Path (poj 3126 bfs)
Language: Default Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11703 Ac ...
随机推荐
- C. Tourist Problem
http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...
- android 03 TableLayout
MainActivity.java(默认的,什么都没有) package com.sxt.day02_02; import android.os.Bundle; import android.app. ...
- DIV------使用 <div> 元素的网页布局
<!DOCTYPE html> <html> <head> <style type="text/css"> div#containe ...
- 【转载】Shared Configuration
Introduction The Internet changes the ways in which companies handle their day-to-day business and h ...
- windows下apache+php+mysql配置
Apache 2.4.10(文件:httpd-2.4.10-win64-VC11.zip) php 5.6.26 (文件:php-5.6.25-Win32-VC11-x64.zip) mysql 5. ...
- object标签参考(转载)
<object> 元素可支持多种不同的媒介类型,比如: 图片 音频 视频 Other 对象 显示图片 你可以显示一幅图片: <object height="100%&quo ...
- JS中escape 方法和C#中的对应
在项目中遇到js中escape过的json字符串,需要在C#中对应模拟编码,记得原来遇到过这个问题,但是当时没记录下来方案, 于是又搜索了一番,发现别人说的都是HttpUtility.UrlEncod ...
- Oracle分区表学习
(1) 表空间及分区表的概念表空间: 是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间.分区表: 当表中的数据量不断增大,查询数据的速度就会变慢,应 ...
- http错误代码含义大全详解
http 错误代码表 所有 HTTP 状态代码及其定义. 代码 指示 2xx 成功 200 正常:请求已完成. 201 正常:紧接 POST 命令. 202 正常:已接受用于处理, ...
- TextView控件
1.手动创建(不建议): TextView tv = new TextView(this); tv.setContent("你好"); setContentView(tv); 2. ...