动态规划-576-出界路径

Description:

There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.


Example 1:
这里写图片描述


Example 2:
这里写图片描述


Note:

  1. Once you move the ball out of boundary, you cannot move it back.
  2. The length and height of the grid is in range [1,50].
  3. N is in range [0,50].

问题描述

在m * n 的棋盘上有一个初始位置为(i, j)的球,你可以将它向四个方向(上,下,左,右)移动,并且可以把它移出棋盘。然而你最多只能将球移动N次。返回将球移出棋盘的路径个数。数目可能会很大,先对 109+7 10 9 + 7 取余


问题分析

这道题与下面这道题类似
https://leetcode.com/problems/knight-probability-in-chessboard/description/
用DFS + Memorization做就可以了


解法1(dfs + memorization):

class Solution {
    private int M = 1000000007;
    private int[][][] memo;

    public int findPaths(int m, int n, int N, int i, int j) {
        memo = new int[m][n][N + 1];

        for(int[][] temp1 : memo){
            for(int[] temp2 : temp1) Arrays.fill(temp2,-1);
        }

        return find(m,n,N,i,j);
    }

    public int find(int m,int n,int N,int i,int j){
        if(i < 0 || j < 0 || i == m || j == n) return 1;
        if(N == 0) return 0;
        if(memo[i][j][N] >= 0) return memo[i][j][N];

        memo[i][j][N] = ((find(m,n,N - 1,i - 1,j) + find(m,n,N - 1,i,j - 1)) % M + (find(m,n,N - 1,i + 1,j) + find(m,n,N - 1,i,j + 1)) % M) % M;

        return memo[i][j][N];
    }
}

解法2(动态规划):

public class Solution {
    public int findPaths(int m, int n, int N, int x, int y) {
        int M = 1000000000 + 7;
        int dp[][] = new int[m][n];
        dp[x][y] = 1;
        int count = 0;

        for (int moves = 1; moves <= N; moves++) {
            int[][] temp = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (i == m - 1)
                        count = (count + dp[i][j]) % M;
                    if (j == n - 1)
                        count = (count + dp[i][j]) % M;
                    if (i == 0)
                        count = (count + dp[i][j]) % M;
                    if (j == 0)
                        count = (count + dp[i][j]) % M;
                    temp[i][j] = (((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M + ((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M) % M;
                }
            }
            dp = temp;
        }

        return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的AGV小车路径规划问题的遗传算法实现的代码: ```python import random # 遗传算法 POP_SIZE = 50 # 种群大小 MUTATION_RATE = 0.1 # 变异概率 CROSSOVER_RATE = 0.8 # 交叉概率 N_GENERATIONS = 20 # 迭代次 # AGV小车路径规划问题参 START_POINT = (0, 0) # 起点坐标 END_POINT = (10, 10) # 终点坐标 OBSTACLES = [(2, 2), (3, 3), (5, 7)] # 障碍物坐标列表 # 随机生成一个个体 def generate_individual(): return [(random.randint(-1, 1), random.randint(-1, 1)) for _ in range(10)] # 计算个体的适应度,即到达终点的距离 def calculate_fitness(individual): x, y = START_POINT for step in individual: x += step[0] y += step[1] if (x, y) in OBSTACLES or x < 0 or y < 0: return 0 # 遇到障碍物或出界,适应度为0 return (x - END_POINT[0]) ** 2 + (y - END_POINT[1]) ** 2 # 选择操作 def selection(population): fitness = [calculate_fitness(individual) for individual in population] total_fitness = sum(fitness) roulette_wheel = [sum(fitness[:i+1]) / total_fitness for i in range(len(population))] new_population = [] for i in range(len(population)): r = random.random() for j in range(len(population)): if roulette_wheel[j] >= r: new_population.append(population[j]) break return new_population # 交叉操作 def crossover(parent1, parent2): if random.random() < CROSSOVER_RATE: child1 = parent1[:5] + parent2[5:] child2 = parent2[:5] + parent1[5:] return child1, child2 else: return parent1, parent2 # 变异操作 def mutation(individual): new_individual = individual.copy() for i in range(len(new_individual)): if random.random() < MUTATION_RATE: new_individual[i] = (random.randint(-1, 1), random.randint(-1, 1)) return new_individual # 主函 def main(): population = [generate_individual() for _ in range(POP_SIZE)] for generation in range(N_GENERATIONS): population = selection(population) next_population = [] for i in range(POP_SIZE // 2): parent1 = random.choice(population) parent2 = random.choice(population) child1, child2 = crossover(parent1, parent2) child1 = mutation(child1) child2 = mutation(child2) next_population.extend([child1, child2]) population = next_population best_individual = max(population, key=calculate_fitness) print("Best individual:", best_individual) print("Fitness:", calculate_fitness(best_individual)) if __name__ == '__main__': main() ``` 该代码使用遗传算法来解决AGV小车路径规划问题,其中种群大小为50,变异概率为0.1,交叉概率为0.8,迭代次为20。在每一次迭代中,使用轮盘赌选择算子选出下一代个体,使用单点交叉算子和随机变异算子对选出的个体进行交叉和变异操作。最终输出最优个体及其适应度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值