Poj 3126 Prime Number + extensive search

Source: Internet
Author: User
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 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.