poj 3126 Prime Path(bfs)

Prime Path
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16148   Accepted: 9113

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

Java AC 代码

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    
    static Queue<Integer> queue; 
    
    static boolean marked[];
    
    static int steps[];
    
    static int original, goal;
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testNumber = sc.nextInt();
        for(int i = 1; i <= testNumber; i++) {
            original = sc.nextInt();
            goal = sc.nextInt();
            marked = new boolean[10000];
            steps = new int[10000];
            queue = new LinkedList<Integer>();
            boolean flag = bfs();
            if(flag)
                System.out.println(steps[goal]);
            else
                System.out.println("Impossible");
        }
    }
    
    public static boolean bfs() {
        
        queue.add(original);
        steps[original] = 0;
        marked[original] = true;
        
        while(!queue.isEmpty()) {
            
            int head = queue.poll();
            int unit = head % 10;      //获取个位
            int deca = (head % 100) / 10; //获取十位
            
            for(int i = 1; i <= 9; i += 2) {  //更改个位数,跳过偶数
                int temp = (head / 10) * 10 + i;
                if(!marked[temp] && isPrime(temp)) {
                    marked[temp] = true;
                    steps[temp] = steps[head] + 1;
                    queue.add(temp);
                }
            }
            
            for(int i = 0; i <= 9; i++) { //更改十位数
                int temp = (head / 100) * 100 + i * 10 + unit;
                if(!marked[temp] && isPrime(temp)) {
                    marked[temp] = true;
                    steps[temp] = steps[head] + 1;
                    queue.add(temp);
                } 
            }
            
            for(int i = 0; i <= 9; i++) { //更改百位数
                int temp = (head / 1000) * 1000 + i * 100 + deca * 10 + unit;
                if(!marked[temp] && isPrime(temp)) {
                    marked[temp] = true;
                    steps[temp] = steps[head] + 1;
                    queue.add(temp);
                } 
            }
            
            for(int i = 1; i <= 9; i++) { //更改千位数,不包括0
                int temp = head % 1000 + i * 1000;
                if(!marked[temp] && isPrime(temp)) {
                    marked[temp] = true;
                    steps[temp] = steps[head] + 1;
                    queue.add(temp);
                } 
            }
            
            if(marked[goal])
                return true;
        }    
        return false;
    }
    
    public static boolean isPrime(int n) { //判断是否是素数
        for(int i = 2; i * i <= n; i++) {
            if(n % i == 0)
                return false;
        }
        return true;
    }
    
    
}

 

posted @ 2016-05-29 10:11  kk_kk  阅读(176)  评论(0编辑  收藏  举报