当前位置: 代码迷 >> 综合 >> [kuangbin带你飞]专题一 简单搜索F - Prime Path POJ - 3126
  详细解决方案

[kuangbin带你飞]专题一 简单搜索F - Prime Path POJ - 3126

热度:19   发布时间:2024-02-05 16:35:05.0

题目描述

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
这道题背景叭叭叭的说了一大堆,其实内容很简单,就是给定两个四位数的数字,要找一条路径,可以从一个数字到另一个数字。路径上相邻两个数字必须只有一位不同,而且路径上的数字必须都是素数。没走一步,代价就是1,求最短路径,如果两个数字之间没有路径,则输出impossible。

题目分析

遇到求最短路径的问题,一般就把BFS往上怼,这道题每次添加节点的时候有39个方向,个位可以0-9十种,十位十种,百位十种,千位1-9九种。

AC代码

#include<iostream>
#include<string.h>
#include<queue>
#include<cmath>
using namespace std;
int start_num,end_num;
int vist[10000];
int step_count[10000];
bool is_prime(int x)
{for(int i=2;i<=sqrt(x*1.0);i++){if(x%i==0)return false;}return true;} 
int bfs()
{queue<int> que;int next_num,new_num;que.push(start_num);vist[start_num] = 1;while(!que.empty()){new_num = que.front();que.pop();if(new_num == end_num) return step_count[new_num];int a = new_num%10;int b = new_num/10%10;int c = new_num/100%10;int d = new_num/1000%10;for(int i = 0;i<=9;i++)//个位 {next_num = i+b*10+c*100+d*1000;if(vist[next_num]==0&&is_prime(next_num)){que.push(next_num);vist[next_num] = 1;step_count[next_num] = step_count[new_num]+1;} }for(int i = 0;i<=9;i++)//十位 {next_num = a+i*10+c*100+d*1000;if(vist[next_num]==0&&is_prime(next_num)){que.push(next_num);vist[next_num] = 1;step_count[next_num] = step_count[new_num]+1;} }for(int i = 0;i<=9;i++)//百位 {next_num = a+b*10+i*100+d*1000;if(vist[next_num]==0&&is_prime(next_num)){que.push(next_num);vist[next_num] = 1;step_count[next_num] = step_count[new_num]+1;} }for(int i = 1;i<=9;i++)//千位 {next_num = a+b*10+c*100+i*1000;if(vist[next_num]==0&&is_prime(next_num)){que.push(next_num);vist[next_num] = 1;step_count[next_num] = step_count[new_num]+1;} }}return -1;
}
int main()
{
// cout<<3240%10<<endl;
// cout<<3439/10%10<<endl;
// cout<<3739/100%10<<endl;
// cout<<1739/1000%10<<endl;int n,result;cin>>n;while(n){cin>>start_num>>end_num;memset(vist,0,sizeof(vist));memset(step_count,0,sizeof(step_count));result = bfs();if(result==-1) cout<<"Impossible"<<endl;else{cout<<result<<endl;}n--;}} 
``
  相关解决方案