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;
        }
        
        
    }
  • 相关阅读:
    SQL Server 锁的 8 种类型
    MYSQL 提取时间中的信息的 4 方法
    MYSQL 时间计算的 3 种函数
    MYSQL 的 6 个返回时间日期函数
    SQL Server 错误18456
    MYSQL <=>运算符
    django比较相等或者不相等的模板语法ifequal / ifnotequal
    django模板语言的注释
    ECharts修改坐标轴,坐标轴字体,坐标轴网格样式以及控制坐标轴是否显示
    http://www.cnblogs.com/linxiyue/p/8244724.html
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5538966.html
Copyright © 2011-2022 走看看