Java Solutions for Blue Bridge Cup Algorithm Challenges

Secret Code Decoding

Recover Chinese character bitmaps from byte sequences and interpret hidden messages. Each character is represented by 32 bytes arranged in 16 rows of 2 bytes. Convert byte values to binary, replacing 0s with spaces to visualize the characters.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class BitmapDecoder {
    public static void main(String[] args) {
        int[] byteData = {4,0,4,0,4,0,4,32,-1,-16,4,32,4,32,4,32,4,32,4,32,8,32,8,32,16,34,16,34,32,30,-64,0,16,64,16,64,34,68,127,126,66,-124,67,4,66,4,66,-124,126,100,66,36,66,4,66,4,66,4,126,4,66,40,0,16,4,0,4,0,4,0,4,32,-1,-16,4,32,4,32,4,32,4,32,4,32,8,32,8,32,16,34,16,34,32,30,-64,0,0,-128,64,-128,48,-128,17,8,1,-4,2,8,8,80,16,64,32,64,-32,64,32,-96,32,-96,33,16,34,8,36,14,40,4,4,0,3,0,1,0,0,4,-1,-2,4,0,4,16,7,-8,4,16,4,16,4,16,8,16,8,16,16,16,32,-96,64,64,16,64,20,72,62,-4,73,32,5,16,1,0,63,-8,1,0,-1,-2,0,64,0,80,63,-8,8,64,4,64,1,64,0,-128,0,16,63,-8,1,0,1,0,1,0,1,4,-1,-2,1,0,1,0,1,0,1,0,1,0,1,0,1,0,5,0,2,0,2,0,2,0,7,-16,8,32,24,64,37,-128,2,-128,12,-128,113,-4,2,8,12,16,18,32,33,-64,1,0,14,0,112,0,1,0,1,0,1,0,9,32,9,16,17,12,17,4,33,16,65,16,1,32,1,64,0,-128,1,0,2,0,12,0,112,0,0,0,0,0,7,-16,24,24,48,12,56,12,0,56,0,-32,0,-64,0,-128,0,0,0,0,1,-128,3,-64,1,-128,0,0};
        String[] padding = {"       ","      ","     ","    ","   ","  "," ",""};
        
        for(int idx = 0; idx < byteData.length; idx++) {
            int current = byteData[idx];
            String binary;
            
            if(current < 0) {
                binary = Integer.toBinaryString(current).substring(24).replace("0", " ");
            } else {
                binary = Integer.toBinaryString(current).replace("0", " ");
                binary = padding[binary.length()-1] + binary;
            }
            
            if(idx % 2 == 1) {
                System.out.println(binary);
            } else {
                System.out.print(binary);
            }
        }
    }
}
</java>

Birthday Deduction

Find an 8-digit birthdate (YYYYMMDD) divisible by the current date (2012-03-12) where the person was born in June (06).

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class BirthdayFinder {
    public static void main(String[] args) {
        for(int date = 19000000; date <= 20000000; date++) {
            if(date % 2012 == 0 && date % 3 == 0 && date % 12 == 0) {
                String dateStr = String.valueOf(date);
                if(dateStr.substring(4,6).equals("06")) {
                    int day = Integer.parseInt(dateStr.substring(6,8));
                    if(day <= 30) {
                        System.out.println(date);
                        break;
                    }
                }
            }
        }
    }
}
</java>

Integer Decomposition

Count distinct ways to decompose 2019 into three distinct positive integers without digits 2 or 4. Order permutations are considered identical.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class TripletSum {
    public static void main(String[] args) {
        int count = 0;
        for(int x = 1; x < 2019/2; x++) {
            for(int y = x+1; y < 2019; y++) {
                int z = 2019 - x - y;
                if(y < z && validDigits(x) && validDigits(y) && validDigits(z)) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    private static boolean validDigits(int num) {
        while(num > 0) {
            int digit = num % 10;
            if(digit == 2 || digit == 4) return false;
            num /= 10;
        }
        return true;
    }
}
</java>

Maze Shortest Path

Find the lexicographically smallest shortest path in a 30x50 maze (0=passable, 1=blocked) using BFS with directional priorities (D<L<R<U).

Constraints

  • Max runtime: 1 second
  • Max memory: 256MB
<java>
import java.util.*;
class MazeSolver {
    static class Node {
        int x, y;
        String path;
        Node(int x, int y, String p) { 
            this.x = x; this.y = y; path = p; 
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[][] grid = new char[30][50];
        int[][] visited = new int[30][50];
        int[] dx = {1, 0, 0, -1}, dy = {0, -1, 1, 0};
        char[] dirs = {'D','L','R','U'};
        
        for(int i=0; i<30; i++) grid[i] = sc.nextLine().toCharArray();
        
        Queue<Node> q = new LinkedList<>();
        q.add(new Node(0,0,""));
        visited[0][0] = 1;
        
        while(!q.isEmpty()) {
            Node curr = q.poll();
            if(curr.x == 29 && curr.y == 49) {
                System.out.println(curr.path);
                return;
            }
            for(int i=0; i<4; i++) {
                int nx = curr.x + dx[i], ny = curr.y + dy[i];
                if(nx >=0 && nx<30 && ny>=0 && ny<50 && grid[nx][ny]=='0' && visited[nx][ny]==0) {
                    visited[nx][ny] = 1;
                    q.add(new Node(nx, ny, curr.path + dirs[i]));
                }
            }
        }
    }
}
</java>

Prime Number Identification

Find the 2019th prime number starting from 2.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class PrimeFinder {
    public static void main(String[] args) {
        int count = 1, num = 0;
        for(int i=3; count < 2019; i++) {
            if(isPrime(i)) {
                count++;
                if(count == 2019) num = i;
            }
        }
        System.out.println(num);
    }

    private static boolean isPrime(int n) {
        for(int i=2; i*i <= n; i++) 
            if(n % i == 0) return false;
        return true;
    }
}
</java>

Weight Distribution

Calculate maximum weight on electronic scales after distributing pyramid weights downward equally. Given base scale readings, compute the ratio to find the maximum weight.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
import java.util.*;
public class WeightPyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[][] weights = new double[30][30];
        for(int i=0; i<29; i++) 
            for(int j=0; j<=i; j++) 
                weights[i][j] = sc.nextInt();
                
        for(int i=1; i<30; i++) {
            for(int j=0; j<=i; j++) {
                if(j == 0) weights[i][j] += weights[i-1][j]/2;
                else if(j == i) weights[i][j] += weights[i-1][j-1]/2;
                else weights[i][j] += (weights[i-1][j-1] + weights[i-1][j])/2;
            }
        }
        Arrays.sort(weights[29]);
        double ratio = 2086458231 / weights[29][0];
        System.out.println((long)(ratio * weights[29][29]));
    }
}
</java>

Increasing Sequence Count

Count increasing letter pairs in a 30x50 matrix along rows, columns, or 45° diagonals. Letters are considered increasing when read left-to-right or top-to-bottom.

Constraints

  • Max runtime: 1 second
  • Max memory: 256MB
<java>
public class GridAnalyzer {
    static String letters = "VLPWJVVNNZSWFGHSFRBCOIJTPYNEURPIGKQGPSXUGNELGRVZAGSDLLOVGRTWEYZKKXNKIRWGZWXWRHKXFASATDWZAPZRNHTNNGQFZGUGXVQDQAEAHOQEADMWWXFBXECKAVIGPTKTTQFWSWPKRPSMGABDGMGYHAOPPRRHKYZCMFZEDELCALTBSWNTAODXYVHQNDASUFRLYVYWQZUTEPFSFXLTZBMBQETXGXFUEBHGMJKBPNIHMYOELYZIKHZYZHSLTCGNANNXTUJGBYKUOJMGOGRDPKEUGVHNZJZHDUNRERBUXFPTZKTPVQPJEMBHNTUBSMIYEGXNWQSBZMHMDRZZMJPZQTCWLRZNXOKBITTPSHEXWHZXFLWEMPZTBVNKNYSHCIQRIKQHFRAYWOPGMHJKFYYBQSDPOVJICWWGGCOZSBGLSOXOFDAADZYEOBKDDTMQPAVIDPIGELBYMEVQLASLQRUKMXSEWGHRSFVXOMHSJWWXHIBCGVIFGWRFRFLHAMYWYZOIQODBIHHRIIMWJWJGYPFAHZZWJKRGOISUJCEKQKKPNEYCBWOQHTYFHHQZRLFNDOVXTWASSQWXKBIVTKTUIASKPEKNJFIVBKOZUEPPHIWLUBFUDWPIDRJKAZVJKPBRHCRMGNMFWWCGZAXHXPDELTACGUWBXWNNZNDQYYCIQRJCULIEBQBLLMJEUSZPRWHHQMBIJWTQPUFNAESPZHAQARNIDUCRYQAZMNVRVZUJOZUDGSPFGAYBDEECHUXFUZIKAXYDFWJNSAOPJYWUIEJSCORRBVQHCHMRJNVIPVEMQSHCCAXMWEFSYIGFPIXNIDXOTXTNBCHSHUZGKXFECLYZBAIIOTWLREPZISBGJLQDALKZUKEQMKLDIPXJEPENEIPWFDLPHBQKWJFLSEXVILKYPNSWUZLDCRTAYUUPEITQJEITZRQMMAQNLNDQDJGOWMBFKAIGWEAJOISPFPLULIWVVALLIIHBGEZLGRHRCKGFLXYPCVPNUKSWCCGXEYTEBAWRLWDWNHHNNNWQNIIBUCGUJYMRYWCZDKISKUSBPFHVGSAVJBDMNPSDKFRXVVPLVAQUGVUJEXSZFGFQIYIJGISUANRAXTGQLAVFMQTICKQAHLEBGHAVOVVPEXIMLFWIYIZIIFSOPCMAWCBPKWZBUQPQLGSNIBFADUUJJHPAIUVVNWNWKDZBHGTEEIISFGIUEUOWXVTPJDVACYQYFQUCXOXOSSMXLZDQESHXKPFEBZHJAGIFGXSMRDKGONGELOALLSYDVILRWAPXXBPOOSWZNEASVJGMAOFLGYIFLJTEKDNIWHJAABCASFMAKIENSYIZZSLRSUIPCJBMQGMPDRCPGWKTPLOTAINXZAAJWCPUJHPOUYWNWHZAKCDMZDSRRRARTVHZYYCEDXJQNQAINQVDJCZCZLCQWQQIKUYMYMOVMNCBVYABTCRRUXVGYLZILFLOFYVWFFBZNFWDZOADRDCLIRFKBFBHMAXX";
    
    public static void main(String[] args) {
        char[][] matrix = new char[30][50];
        int count = 0;
        for(int i=0; i<30; i++) 
            for(int j=0; j<50; j++) 
                matrix[i][j] = letters.charAt(i*50+j);
        
        for(int i=0; i<30; i++) {
            for(int j=0; j<50; j++) {
                // Check right
                for(int k=1; j+k<50; k++) 
                    if(matrix[i][j] < matrix[i][j+k]) count++;
                // Check down
                for(int k=1; i+k<30; k++) 
                    if(matrix[i][j] < matrix[i+k][j]) count++;
                // Check diagonals
                for(int k=1; i+k<30 && j+k<50; k++) 
                    if(matrix[i][j] < matrix[i+k][j+k]) count++;
                for(int k=1; i-k>=0 && j+k<50; k++) 
                    if(matrix[i][j] < matrix[i-k][j+k]) count++;
                for(int k=1; i+k<30 && j-k>=0; k++) 
                    if(matrix[i][j] < matrix[i+k][j-k]) count++;
            }
        }
        System.out.println(count);
    }
}
</java>

Maximum Rainfall

Determine the minimal depth where node values sum to maximum in a complete binary tree.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class TreeAnalyzer {
    public static void main(String[] args) {
        System.out.println(34);
    }
}
</java>

Odd Digit Multiple

Find smallest integer multiple of 2019 containing only odd digits (1,3,5,7,9).

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class OddMultipleFinder {
    public static void main(String[] args) {
        for(long i=2019; ; i+=2019) {
            if(hasOnlyOddDigits(i)) {
                System.out.println(i);
                break;
            }
        }
    }
    
    private static boolean hasOnlyOddDigits(long n) {
        while(n > 0) {
            int d = (int)(n % 10);
            if(d % 2 == 0) return false;
            n /= 10;
        }
        return true;
    }
}
</java>

Optimal Path Length

Compute shortest path between nodes 1 and 2021 where edges exist between nodes with ≤21 difference, weighted by LCM of node values.

Constraints

  • Max runtime: 1 second
  • Max memory: 128MB
<java>
public class GraphTraversal {
    static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
    
    static int lcm(int a, int b) {
        return a * b / gcd(a, b);
    }
    
    public static void main(String[] args) {
        int n = 2021;
        int[] dist = new int[n+1];
        for(int i=2; i<=n; i++) dist[i] = Integer.MAX_VALUE;
        
        for(int i=1; i<=n; i++) {
            for(int j=i+1; j<=Math.min(i+21, n); j++) {
                int weight = lcm(i, j);
                dist[j] = Math.min(dist[j], dist[i] + weight);
            }
        }
        System.out.println(dist[n]);
    }
}
</java>

Arithmetic Sequence

Find minimal terms in shortest arithmetic sequence containing given integers.

Constraints

  • Max runtime: 1 second
  • Max memory: 256MB
<java>
import java.util.*;
public class SequenceOptimizer {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i=0; i<n; i++) nums[i] = sc.nextInt();
        Arrays.sort(nums);
        
        int minDiff = Integer.MAX_VALUE;
        for(int i=1; i<n; i++) 
            minDiff = Math.min(minDiff, nums[i]-nums[i-1]);
        
        if(minDiff == 0) {
            System.out.println(n);
            return;
        }
        System.out.println((nums[n-1]-nums[0])/minDiff + 1);
    }
}
</java>

Bottle Cap Exchange

Calculate total drinks obtained by exchanging every 3 bottle caps for 1 new drink.

Constraints

  • Max runtime: 1 second
  • Max memory: 256MB
<java>
public class BottleExchange {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int bottles = sc.nextInt();
        int total = bottles;
        while(bottles >= 3) {
            int newBottles = bottles / 3;
            total += newBottles;
            bottles = newBottles + bottles % 3;
        }
        System.out.println(total);
    }
}
</java>

Tags: java algorithms DataStructures GraphTheory NumberTheory

Posted on Fri, 15 May 2026 16:06:03 +0000 by Syphon