[kuangbin带你飞]专题一 简单搜索-F - Prime Path
2016-05-01 12:02
549 查看
Prime Path
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
One 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
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
Sample Output
分析:一个数字枚举出所有的状态,就是bfs嘛。水题,双向bfs如何实现是个问题。
code:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15921 | Accepted: 8987 |
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
One 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
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
分析:一个数字枚举出所有的状态,就是bfs嘛。水题,双向bfs如何实现是个问题。
code:
#include <cstdio> #include <cstring> #include <queue> #include <iostream> using namespace std; typedef long long ll; typedef struct{ int number[4]; int wal; }data; data star,en; int n,m; int is_prim[10005],vis[10005]; int dir[]={1,10,100,1000}; void init() { cin>>n>>m; memset(vis,0,sizeof vis); star.number[0]=n/1000; star.number[1]=n/100%10; star.number[2]=n/10%10; star.number[3]=n%10; en.number[0]=m/1000; en.number[1]=m/100%10; en.number[2]=m/10%10; en.number[3]=m%10; star.wal=0; en.wal=0; } void prim_() { memset(is_prim,0,sizeof is_prim); for(int i=2;i<=9999;i++) { if(is_prim[i]==1) continue; for(int j=i*2;j<=9999;j+=i) { is_prim[j]=1; } } } void bfs() { queue <data> qf; queue <data> qb; vis =1; qf.push(star); qb.push(en); while(qf.size()) { data nos,noe; nos=qf.front(); noe=qb.front(); int stemp=0,etemp=0; for(int i=0;i<4;i++) { stemp=stemp*10+nos.number[i]; etemp=etemp*10+noe.number[i]; } qf.pop(); qb.pop(); if(stemp==m) { cout<<nos.wal<<endl; return ; } //r printf("%d %d\n",stemp,etemp); data ne1,ne2; ne1=nos,ne2=noe; ne1.wal=nos.wal+1; ne2.wal=noe.wal+1; for(int i=0;i<4;i++) { for(int j=0;j<=9;j++) { ne1=nos; ne2=noe; if(i==0&&j==0) continue; ne1.number[i]=j; ne2.number[i]=j; stemp=0,etemp=0; for(int q=0;q<4;q++) { stemp=stemp*10+ne1.number[q]; etemp=etemp*10+ne2.number[q]; } if(1000<=stemp&&etemp<=9999) { if(!vis[stemp]&&!is_prim[stemp]) { vis[stemp]=1; ne1.wal=nos.wal+1; qf.push(ne1); } } if(1000<=etemp&&etemp<=9999) { if(!vis[etemp]&&is_prim[etemp]) { vis[etemp]=1; ne2.wal=noe.wal+1; qb.push(ne2); } } } } } } int main(void) { prim_(); int t; cin>>t; while(t--) { init(); bfs(); } }
相关文章推荐
- 强大的异或运算-深度好文
- 第 27 章 CSS 传统布局[上]
- Smart210学习记录-----linux定时器
- Eclipse SDK中一些插件的安装(web开发插件)
- Centos 6,5 安装Mysql 5.7.12
- 调戏微信朋友圈定位算法,巧用图片位置信息更改朋友圈定位信息
- 20145320《Java程序设计》第四次实验报告
- 第九周学习进度条
- 关于登录注册输入框底线线条被挡住的完美解决方案
- 从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0
- 首次安装Pycharm出现No Python interpreter selected解决方法
- [Java视频笔记]day10(待完善)
- WinEdt && LaTex(二)—— 空心中括号
- 设计模式——单例模式
- Strong Components
- leetcode——114—— Flatten Binary Tree to Linked List
- hdu5676 ztr loves lucky numbers DFS+二分
- Ubuntu16.04 卸载VMware12
- wince工业屏下QT开发环境搭建
- Linux之玩转xargs