四、最大得分的路径数目(Biweekly16)

题目描述:
给你一个正方形字符数组 board ,你从数组最右下方的字符 ‘S’ 出发。

你的目标是到达数组最左上角的字符 ‘E’ ,数组剩余的部分为数字字符 1, 2, …, 9 或者障碍 ‘X’。在每一步移动中,你可以向上、向左或者左上方移动,可以移动的前提是到达的格子没有障碍。

一条路径的 「得分」 定义为:路径上所有数字的和。

请你返回一个列表,包含两个整数:第一个整数是 「得分」 的最大值,第二个整数是得到最大得分的方案数,请把结果对 10^9 + 7 取余。

如果没有任何路径可以到达终点,请返回 [0, 0] 。

示例 1:

输入:board = [“E23”,“2X2”,“12S”]
输出:[7,1]
示例 2:

输入:board = [“E12”,“1X1”,“21S”]
输出:[4,2]
示例 3:

输入:board = [“E11”,“XXX”,“11S”]
输出:[0,0]

提示:

2 <= board.length == board[i].length <= 100

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-paths-with-max-score
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路:
动态规划:保存最大路径数即可

class Solution {
     int mod = 1000000000 + 7;
    public int[] pathsWithMaxScore(List<String> board) {
        int len = board.size();
        int[][] dp = new int[len][len];
        char[][] chars = new char[len][len];
//        记录到每个点最大的方案数
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < chars.length; i++) {
            chars[i] = board.get(i).toCharArray();
        }
//        初始化最后一列和最后 一行
        int x = len - 2;
        int y = len - 1;
        while (x >= 0) {
            if (chars[x][y] == 'X') {
                break;
            }
            if (x == len - 2) {
                dp[x][y] = Integer.valueOf(chars[x][y] - '0');
                map.put(x * len + y, 1);
                x--;
                continue;
            } else {
                dp[x][y] = Integer.valueOf(chars[x][y] - '0') + dp[x + 1][y];
                map.put(x * len + y, 1);
            }
            x--;
        }
        x = len - 1;
        y = len - 2;
        while (y >= 0) {
            if (chars[x][y] == 'X') {
                break;
            }
            if (y == len - 1) {
                dp[x][y] = Integer.valueOf(chars[x][y] - '0');
                dp[x][y] %= mod;
                map.put(x * len + y, 1);
                x--;
                continue;
            } else {
                dp[x][y] = Integer.valueOf(chars[x][y] - '0') + dp[x][y + 1];
                dp[x][y] %= mod;
                map.put(x * len + y, 1);
            }
            y--;
        }


        for (int i = len - 2; i >= 0; i--) {
            for (int j = len - 2; j >= 0; j--) {
                if (chars[i][j] == 'X') {
                    continue;
                }
                ad(dp,i,j,map,chars);
            }
        }
        
        if(map.getOrDefault(0,0) == 0){
            return new int[]{0,0};
        }
        return new int[]{dp[0][0] - 'E' + '0', map.get(0)};
    }

    
    public void ad(int[][] dp, int i, int j, Map<Integer, Integer> map, char[][] chars) {
        int down = dp[i + 1][j];
        int right = dp[i][j + 1];
//                斜着走
        int xie = dp[i + 1][j + 1];
        int cur = Integer.valueOf(chars[i][j] - '0');
        int downtime = map.getOrDefault((i + 1) * chars.length + j,0);
        int righttime = map.getOrDefault(i * chars.length + j + 1,0);
        int xietime = map.getOrDefault((i + 1) * chars.length  + j + 1,0);
        int max = Math.max(down,Math.max(right,xie));
        dp[i][j] = cur + max;
        dp[i][j] %= mod;
        if (down != 0 || right != 0 || xie != 0) {
//            三个都不等于0
           if(down == max && right == max && xie == max){
               map.put(i * chars.length + j, (downtime + righttime + xietime) % mod);
               return;
           }
           if(xie == max){
               if(right == max){
                   map.put(i * chars.length + j,( righttime + xietime) % mod);
                   return;
               }else{
                   map.put(i * chars.length + j, xietime);
                   return;
               }
           }else{
               if(right == max){
                   if(down == max){
                       map.put(i * chars.length + j, (righttime + downtime) % mod);
                       return;
                   }else{
                       map.put(i * chars.length + j, righttime);
                       return;
                   }
               }else{
                   map.put(i * chars.length + j, downtime );
                   return;
               }
           }
        }

//        三个都等于零
        if (i == chars.length - 2 && j == chars.length - 2){
            dp[i][j] = cur + xie;
            dp[i][j] %= mod;
            map.put(i * chars.length + j, 1);
            return;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中订阅ICS(iCalendar)日程,你可以使用一些开源的Java库,例如 iCal4j 或 Biweekly。这些库提供了解析和处理ICS文件的功能。 以下是使用 iCal4j 库来订阅ICS日程的简单示例: 1. 首先,你需要将 iCal4j 添加到你的项目依赖中。你可以在 Maven 或 Gradle 中添加以下依赖项: Maven: ```xml <dependency> <groupId>org.mnode.ical4j</groupId> <artifactId>ical4j</artifactId> <version>3.0.27</version> </dependency> ``` Gradle: ```groovy implementation 'org.mnode.ical4j:ical4j:3.0.27' ``` 2. 然后,你可以使用以下代码订阅ICS日程: ```java import net.fortuna.ical4j.data.CalendarBuilder; import net.fortuna.ical4j.model.Calendar; import net.fortuna.ical4j.model.Period; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; public class ICSReader { public static void main(String[] args) { try { // 从文件中读取ICS日程 FileInputStream fileInputStream = new FileInputStream("path/to/your/file.ics"); CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = builder.build(fileInputStream); // 或者从URL中读取ICS日程 URL url = new URL("http://example.com/your-calendar.ics"); Calendar calendar = builder.build(url.openStream()); // 解析并输出日程信息 for (Object component : calendar.getComponents()) { if (component instanceof Period) { Period period = (Period) component; System.out.println("Start: " + period.getStart()); System.out.println("End: " + period.getEnd()); // 其他属性的访问方式请参考 iCal4j 文档 } } } catch (IOException e) { e.printStackTrace(); } catch (net.fortuna.ical4j.data.ParserException e) { e.printStackTrace(); } } } ``` 请注意,以上示例仅演示了如何使用 iCal4j 库来读取并解析ICS文件中的日程信息。你可以根据需要进一步处理和使用这些信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值