Question: Give you two four-digit prime numbers, which need to be transformed from N-> M several times... train of Thought: the prime number is used in table + wide search, because it has not been written for a long time, so it has been stuck for a long time before 1 has passed. The average pair column is used, and an array is used to record its steps, OK .. # include <iostream> # include <cstdio> # include <queue> using namespace STD; # define Manx 25000int n, m, POS [Manx], Mark [Manx], flag; bool s [Manx]; void prime () {memset (S, 0, sizeof (s); For (INT I = 2; I * I <Manx; I ++) {If (! S [I]) {for (Int J = 2; j * I <Manx; j ++) {s [I * j] = 1 ;}}}} void BFS () {memset (Pos, 0, sizeof (POS); memset (mark, 0, sizeof (Mark); queue <int> que; while (! Que. Empty () que. Pop (); Que. Push (n); Mark [N] = 1; while (! Que. empty () {If (que. front () = m) {flag = 1; printf ("% d \ n", POS [m]); break;} int temp = que. front (); que. pop (); For (INT I = 0; I <= 9; I ++) {If (! S [temp/10*10 + I] &! Mark [temp/10*10 + I]) {// process a single digit mark [temp/10*10 + I] = 1; que. push (temp/10*10 + I); POS [temp/10*10 + I] = POS [temp] + 1;} If (! S [temp/100*100 + TEMP % 10 + I * 10] &! Mark [temp/100*100 + TEMP % 10 + I * 10]) {// process ten Mark [temp/100*100 + TEMP % 10 + I * 10] = 1; que. push (temp/100*100 + TEMP % 10 + I * 10 ); pos [temp/100*100 + TEMP % 10 + I * 10] = POS [temp] + 1;} If (! S [temp/1000*1000 + TEMP % 100 + I * 100] &! Mark [temp/1000*1000 + TEMP % 100 + I * 100]) {// process 1000 bits mark [temp/1000*100 + TEMP % 100 + I *] = 1; que. push (temp/1000*1000 + TEMP % 100 + I * 100 ); pos [temp/1000*1000 + TEMP % 100 + I * 100] = POS [temp] + 1;} If (! S [TEMP % 1000 + I * 1000] &! Mark [TEMP % 1000 + I * 1000] & TEMP % 1000 + I * 1000> = 1000) {// handle thousands of Mark [TEMP % 1000 + I * 1000] = 1; que. push (TEMP % 1000 + I * 1000); POS [TEMP % 1000 + I * 1000] = POS [temp] + 1 ;}}} int main () {Prime (); int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); flag = 0; BFS (); If (! Flag) printf ("impossible \ n ");}}