模拟队列:
运用两个整数(head、tail),分别做队首,队尾,来模拟queue中的入队,出队,取队首...
void bfs() { // 0 int q[maxn]; //模拟一个int类型的队列进行基本的bfs //queue: 空 // (head) int tail = 0, head = 0, x; // (tail) q[head] = x; // 0 1 tail++; //queue: x //此时队列 // (head) (tail) 此时队首是x,但是1对应的是空 while(tail > head) { //如果tail>head 则表示队列中有元素 int now = q[head]; // 0 1 head++; //模拟队首出列, 此时变为: //queue: 空 空 // (head) if(now满足答案) { // (tail) 输出,返回... } for(;;){ //进行运动 temp = now... if(temp满足条件) { ... q[tail] = temp; //模拟入队 此时: 0 1 2 tail++; //queue: temp 空 } // (head) (tail) } } return; }
Prime Path:
题意:(文末有原题)
给出两个素数a,b,每次只能改变一个数字,且该变后仍为素数,问操作几步之后可以使a变成b;
思路:
因为只是四位数,所以可以直接列表,然后再通过bfs对每一位操作,得出最短路径;
(注意如果直接使用queue会超时,用两个整数和数组来模拟队列)
代码:
#include <iostream> #include <cstring> using namespace std; //如果使用 队列 会超时, //引用大佬的原话: 你如果不了解STL的内部实现逻辑或者没看过代码,还是少用,因为使用不当就会拖慢程序效率的。STL的queue是在deque基础上封装的,而deque维护的是一个双端队列。换而言之,queue的数据结构决定了,其入队和出队操作的时间复杂度都是0(n),而不是你以为的O(1)。随着队列元素的增长,queue的出入队效率越差。 const int maxn = 10005; int a, b; bool isprime[maxn]; //保存所有素数,用于判断; bool vis[maxn]; //判断是否访问过 struct Node{ int prime, step; Node(int prime = 0, int step = 0) : prime(prime) , step(step) {} }; void make_tab() { //制表 memset(isprime, true, sizeof(isprime)); for(int i = 2; i * i < maxn; i++) { if(isprime[i]) { for(int j = i * i; j < maxn; j += i) { isprime[j] = false; } } } } void bfs(void) { int head, tail; //模拟队列; head = tail = 0; Node q[maxn]; q[tail].prime = a; q[tail].step = 0; tail++; vis[a] = true; while(tail > head) { //队列是否为空 Node now = q[head]; head++; //队首出列 if(now.prime == b) { cout << now.step << endl; return; } //保存个位与十位的数,因为当对个位操作时,只影响最后一位, 不需要提前对其他值储存; int ones = now.prime % 10; //对十位操作时,会影响个位,所以需要提前保存个位 int tens = (now.prime / 10) % 10; //同理,对百位操作会影响个位,十位,提前保存;而对千位的操作直接 %1000 就可,不会影响; for(int i = 1; i < 10; i += 2) { //对个位的操作,偶数一定是不是素数,所以直接跳过 Node test; test.prime = (now.prime / 10) * 10 + i; if(!isprime[test.prime] || vis[test.prime]) continue; test.step = now.step + 1; vis[test.prime] = true; q[tail].prime = test.prime; q[tail].step = test.step; tail++; //尾的值++ } for(int i = 0; i < 10; i++) { //对十位的操作; Node test; test.prime = (now.prime / 100) * 100 + 10 * i + ones; if(!isprime[test.prime] || vis[test.prime]) continue; test.step = now.step + 1; vis[test.prime] = true; q[tail].prime = test.prime; q[tail].step = test.step; tail++; } for(int i = 0; i < 10; i++) { //对百位的操作; Node test; test.prime = (now.prime / 1000) * 1000 + 100 * i + tens * 10 + ones; if(!isprime[test.prime] || vis[test.prime]) continue; test.step = now.step + 1; vis[test.prime] = true; q[tail].prime = test.prime; q[tail].step = test.step; tail++; } for(int i = 1; i < 10; i++) { //对千位的操作; Node test; test.prime = now.prime % 1000 + i * 1000; if(!isprime[test.prime] || vis[test.prime]) continue; test.step = now.step + 1; vis[test.prime] = true; q[tail].prime = test.prime; q[tail].step = test.step; tail++; } } cout << "Imopssible" << endl; return; } int main() { make_tab(); int t; cin >> t; while(t--) { cin >> a >> b; memset(vis, false, sizeof(vis)); bfs(); } return 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
8179The 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.
输入:
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).
输出:
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
样例:
Input: Output:
3
1033 8179 ----------------- 6
1373 8017 ----------------- 7
1033 1033 ----------------- 0