zoukankan      html  css  js  c++  java
  • POJ-3126

    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

    AC代码为:

     1 #include <iostream> 
     2 #include <cstdio> 
     3 #include <cstring> 
     4 #include <algorithm> 
     5 #include <cmath> 
     6 #include <string>
     7  #include <queue>
     8  using namespace std; 
     9 #define maxn 10005 
    10 bool visit[maxn];
    11  int m, n, a, b, c, d;
    12  struct Node 
    13 { 
    14 int p[4]; int step;
    15  }; 
    16 bool is_prime(int x)
    17  { 
    18 int sum = 0; 
    19 if (x == 1) return false;
    20  if (x == 2) return true;
    21  for (int i = 2; i <= sqrt(x); i++) 
    22 { 
    23 if (x%i == 0) sum++; 
    24 } 
    25 if (sum) return false; else return true; } 
    26 int BFS()
    27  { 
    28 Node st, now; memset(visit, false, sizeof(visit));
    29  queue<Node>Q; 
    30 while (!Q.empty()) Q.pop(); 
    31 visit[m] = true; st.p[0] = m / 1000; st.p[1] = (m / 100) % 10; st.p[2] = (m / 10) % 10; st.p[3] = m % 10; st.step = 0; Q.push(st); 
    32 while (!Q.empty()) 
    33 { 
    34 st = Q.front(); Q.pop();
    35  if (st.p[0] == a && st.p[1] == b && st.p[2] == c && st.p[3] == d) { return st.step; }
    36  for (int i = 0; i <= 3; i++)
    37  { 
    38     for (int j = 0; j<10; j++) 
    39     { 
    40         if (st.p[i] == j) continue;
    41          if (i == 0 && j == 0) continue; now.p[0] = st.p[0]; now.p[1] = st.p[1]; now.p[2] = st.p[2]; now.p[3] = st.p[3]; now.p[i] = j; int x = now.p[0] * 1000 + now.p[1] * 100 + now.p[2] * 10 + now.p[3]; if (is_prime(x) && !visit[x]) { visit[x] = true; now.step = st.step + 1; Q.push(now); } } } } 
    42 return -1; 
    43 } 
    44 int main() 
    45 { 
    46 int N; scanf("%d", &N);
    47  while (N--) 
    48 { 
    49     scanf("%d%d", &m, &n); a = n / 1000; b = (n / 100) % 10; c = (n / 10) % 10; d = n % 10; int ans = BFS(); 
    50     if (ans == -1) printf("Impossible
    "); else printf("%d
    ", ans); } 
    51 return 0;
    52  }            
    View Code
  • 相关阅读:
    POJ1486 Sorting Slides 二分图or贪心
    POJ2060 Taxi Cab Scheme 最小路径覆盖
    POJ3083 Children of the Candy Corn 解题报告
    以前的文章
    POJ2449 Remmarguts' Date K短路经典题
    这一年的acm路
    POJ3014 Asteroids 最小点覆盖
    POJ2594 Treasure Exploration 最小路径覆盖
    POJ3009 Curling 2.0 解题报告
    POJ2226 Muddy Fields 最小点集覆盖
  • 原文地址:https://www.cnblogs.com/csushl/p/9386589.html
Copyright © 2011-2022 走看看