笔试强训 Day 34:ISBN 号码、kotori 和迷宫、矩阵最长递增路径

Day 34

ISBN 号码

解题思路:

  • 模拟,注意 char 和 int 的转换涉及 ASCII 码的转换,需要进行 ± '0'

代码实现:

java 复制代码
import java.util.*;

public class Main {

    private static int MOD = 11;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] s = in.next().toCharArray();
        int n = s.length;
        int sum = 0;
        int idx = 1;
        int tail = 0;
        for(int i = 0; i < n; i++){
            if(s[i] == '-') continue;
            if(i == n-1){
                int num = sum % 11;
                int tmp = s[i] == 'X' ? 10 : s[i] - '0';
                if(num == tmp){
                    System.out.println("Right");
                    return;
                }else{
                    if(num == 10) s[i] = 'X';
                    else s[i] = (char) (num + '0');
                }
            }else{
                sum += (s[i] - '0') * idx++;
            }
        }
        System.out.println(String.valueOf(s));
    }
}

kotori 和迷宫

解题思路:

  • bfs,注意需要记录入口到出口的最短路径,因此除了往队列传坐标外,还可以传每个坐标距离起点的距离;

代码实现:

java 复制代码
import java.io.*;
import java.util.*;

public class Main{
    
    private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    private static Read in = new Read();
    
    public static void main(String[] args) throws IOException{
        int n = in.nextInt(), m = in.nextInt();
        int starX = 0, starY = 0;
        char[][] grid = new char[n + 1][m + 1];
        for(int i = 1; i <= n; i++){
            String str = in.next();
            for(int j = 1; j <= m; j++){
                grid[i][j] = str.charAt(j-1);
                if(grid[i][j] == 'k'){
                    starX = i;
                    starY = j;
                }
            }
        }
        boolean[][] visit = new boolean[n+1][m+1];
        int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        Queue<int[]> queue = new LinkedList<>();
        
        // 初始化, 第三个元素表示和入口的距离
        queue.add(new int[]{starX, starY, 0});
        visit[starX][starY] = true;
        
        // bfs
        int cntE = 0, min = Integer.MAX_VALUE;
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            for(int[] dir : dirs){
                int x = cur[0] + dir[0];
                int y = cur[1] + dir[1];
                // 更新和入口的距离
                int distance = cur[2] + 1;
                if(x < 1 || x > n || y < 1 || y > m) continue;
                if(visit[x][y]) continue;
                if(grid[x][y] == '*') continue;
                else if(grid[x][y] == 'e'){
                    // 到达出口一定出迷宫, 坐标不入队列
                    cntE++;
                    min = Math.min(min, distance);
                    // 出口也要标记
                    visit[x][y] = true;
                }else{
                    visit[x][y] = true;
                    queue.offer(new int[]{x, y, distance});
                }
            }
        }
        // 出口为 0, 输出 -1
        if(cntE == 0) out.println(-1);
        else out.println(cntE + " " + min);
        out.close();
    }
}

class Read{
    
    StringTokenizer st = new StringTokenizer("");
    
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    
    String next() throws IOException{
        if(!st.hasMoreTokens()){
            String line = bf.readLine();
            if(line == null) return null;
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }
    
    int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
}

矩阵最长递增路径

解题思路:

  • 记忆化搜索 + 递归

代码实现:

java 复制代码
import java.util.*;


public class Solution {
    private int[][] matrix;
    private int m, n;
    private int ret = 0;
    private boolean[][] visit;
    private int[][] dirs = new int[][]{{-1, 0},{1, 0}, {0, -1}, {0, 1}};

    public int solve (int[][] _matrix) {
        matrix = _matrix;
        m = matrix.length;
        n = matrix[0].length;
        visit = new boolean[m][n];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                dfs(i, j, 1);
            }
        }
        return ret;
    }

    private void dfs(int i, int j, int len){

        if(!check(i, j)) return;
        if(visit[i][j]) return;
        visit[i][j] = true;
        ret = Math.max(ret, len);
        for(int[] dir : dirs){
            int x = i + dir[0];
            int y = j + dir[1];
            if(!check(x, y)) continue;
            if(matrix[i][j] < matrix[x][y]) dfs(x, y, len+1);
        }
        visit[i][j] = false;
    }

    private boolean check(int i, int j){
        if(i < 0 || i >= m || j < 0 || j >= n) return false;
        return true;
    }
}
相关推荐
步行cgn4 小时前
Spring c 命名空间注入详解
java·后端·spring
明月_清风4 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
aramae5 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路5 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
荆棘鸟智能5 小时前
城市感知设备怎么统一接入?从多协议网关到设备模型的中间件架构设计
人工智能·算法·边缘计算
Wang's Blog6 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog6 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis
Ivanqhz6 小时前
SVD++算法
java·服务器·网络·深度学习·神经网络
qq_2518364577 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
caoerzhong7 小时前
JeeWMS 开源仓库管理系统全景解读:一套 Java WMS 如何把 WMS/OMS/BMS/TMS 装进同一个系统
java·开源