48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径

ISBN号

ISBN号码_牛客题霸_牛客网

算法原理

模拟,根据题意模拟就可以了,注意一下余数为10的时候要特别判断一下是不是X就行了

代码

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        char[] s = scan.next().toCharArray();
        int sum = 0, count = 0;
        for(int i = 0; i < s.length - 1; i++) {
            if(i == 1 || i == 5 || i == 11) continue;
            count++;
            sum += (s[i] - '0') * count;
        }

        boolean flg = false;
        if(s[s.length - 1] == 'X') flg = (sum % 11 == 10);  //特别判断一下为10的情况
        else flg = (sum % 11 == s[s.length - 1] - '0');

        if(flg) System.out.println("Right");
        else {
            if(sum % 11 == 10) s[s.length - 1] = 'X';
            else s[s.length - 1] = Character.forDigit(sum % 11, 10);
            StringBuffer sb = new StringBuffer();
            for(char x: s) sb.append(x);
            System.out.println(sb.toString());
        }
    }
}

kotori和迷宫

kotori和迷宫

算法原理

迷宫类最短路径dfs问题的扩展,原理都差不多,就是最后输出的时候单独要多记录一下出口数量而已

代码

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int N = 35;
    public static int x1, y1;  //记录起点位置
    public static int n, m;
    public static char[][] arr = new char[N][N];
    public static int[][] dist = new int[N][N];

    public static int[] dx = {0, 0, -1, 1};
    public static int[] dy = {1, -1, 0, 0};

    public static void bfs() {
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                dist[i][j] = -1;
        
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x1, y1});
        dist[x1][y1] = 0;

        while(!q.isEmpty()) {
            int[] tmp = q.poll();
            int a = tmp[0], b = tmp[1];
            for(int i = 0; i < 4; i++) {
                //遍历该点的上下左右的四个点
                int x = a + dx[i], y = b + dy[i];
                //坐标是合法的,该点没有记录过,且不是墙
                if(x >= 0 && x < m && y >= 0 && y < n && dist[x][y] == -1 && arr[x][y] != '*') {
                    dist[x][y] = dist[a][b] + 1;
                    if(arr[x][y] != 'e') {
                        q.add(new int[]{x, y});
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        m = scan.nextInt(); n = scan.nextInt();
        for(int i = 0; i < m; i++) {
            char[] tmp = scan.next().toCharArray();
            for(int j = 0; j < n; j++) {
                arr[i][j] = tmp[j];
                if(arr[i][j] == 'k') {
                    x1 = i; y1 = j;
                }
            }
        }

        bfs();

        int count = 0, res = 1000;    //count记录出口个数
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++) {
                if(arr[i][j] == 'e' && dist[i][j] != -1) {    //为-1说明无法到达
                    count++;
                    res = Math.min(res, dist[i][j]);
                }
            }
        if(count == 0) System.out.println("-1");
        else System.out.println(count + " " + res);
    }
}

矩阵最长递增路径

矩阵最长递增路径_牛客题霸_牛客网

算法原理

递归 + 记忆搜索

代码

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


public class Solution {
    public static int m, n;
    public static int[] dx = {0, 0, -1, 1};
    public static int[] dy = {1, -1, 0, 0};
    public static int[][] memo = new int[1010][1010];

    public int dfs(int[][] matrix, int i, int j) {
        if (memo[i][j] != -1) return memo[i][j];

        int len = 1;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j])
                len = Math.max(len, 1 + dfs(matrix, x, y));
        }

        memo[i][j] = len;
        return len;
    }
    public int solve (int[][] matrix) {
        // write code here
        m = matrix.length;
        n = matrix[0].length;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                memo[i][j] = -1;
            }

        int res = 1;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                res = Math.max(res, dfs(matrix, i, j));
            }
        return res;
    }
}
相关推荐
月夕·花晨几秒前
Gateway -网关
java·服务器·分布式·后端·spring cloud·微服务·gateway
失散13几秒前
分布式专题——6 Redis缓存设计与性能优化
java·redis·分布式·缓存·架构
杏花春雨江南1 分钟前
Spring Cloud Gateway 作为一个独立的服务进行部署吗
java·开发语言
GSDjisidi2 分钟前
东京本社招聘 | 财务负责人 & 多个日本IT岗位(Java/C++/Python/AWS 等),IT营业同步招募
java·开发语言·aws
叫我阿柒啊13 分钟前
Java全栈开发面试实战:从基础到微服务的完整技术栈解析
java·spring boot·微服务·前端框架·vue·jwt·全栈开发
前行的小黑炭14 分钟前
Android:在项目当中可能会遇到的ANR,应该如何解决?
android·java·kotlin
索迪迈科技1 小时前
Flink Task线程处理模型:Mailbox
java·大数据·开发语言·数据结构·算法·flink
元亓亓亓2 小时前
LeetCode热题100--230. 二叉搜索树中第 K 小的元素--中等
算法·leetcode·职场和发展
草莓熊Lotso2 小时前
《算法闯关指南:优选算法-双指针》--01移动零,02复写零
c语言·c++·经验分享·算法·leetcode