zoukankan      html  css  js  c++  java
  • 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;
        }
        
        
    }
  • 相关阅读:
    编译linux内核问题
    linux驱动路径
    plateform_driver_register和plateform_device_register区别
    linux总线、设备和设备驱动的关系
    linux设备驱动模型
    一堆Offer怎么选?这样做就不纠结了
    解决问题最简单的方法
    Android ScrollView嵌套GridView导致GridView只显示一行item
    84. Spring Boot集成MongoDB【从零开始学Spring Boot】
    接手别人的代码,死的心有吗?
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5538966.html
Copyright © 2011-2022 走看看