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;
        }
        
        
    }
  • 相关阅读:
    Spring Boot (20) 拦截器
    Spring Boot (19) servlet、filter、listener
    Spring Boot (18) @Async异步
    Spring Boot (17) 发送邮件
    Spring Boot (16) logback和access日志
    Spring Boot (15) pom.xml设置
    Spring Boot (14) 数据源配置原理
    Spring Boot (13) druid监控
    Spring boot (12) tomcat jdbc连接池
    Spring Boot (11) mybatis 关联映射
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5538966.html
Copyright © 2011-2022 走看看