45. Jump Game II

本文介绍了一个算法问题:如何在给定的非负整数数组中从第一个位置到达最后一个位置所需的最少跳跃次数。通过动态规划的方法,逆向遍历数组来计算每个位置达到终点所需的最少跳跃次数。
  • Given an array of non-negative integers, you are initially positioned
    at the first index of the array.

    Each element in the array represents your maximum jump length at that
    position.

    Your goal is to reach the last index in the minimum number of jumps.

    For example: Given array A = [2,3,1,1,4]

    The minimum number of jumps to reach the last index is 2. (Jump 1
    step from index 0 to 1, then 3 steps to the last index.)

    Note: You can assume that you can always reach the last index

int calculateJump(int* nums, int numsSize,int *jumps) {
    int i,j;

    if(numsSize <= 1 || nums == NULL || jumps == NULL) {
        return 0;
    }

    /*initial*/
    jumps[numsSize-1] = 0;

    for(i = numsSize-2; i >=0;i--){
         jumps[i] = 2147483647;
         if(nums[i] == 0){
            jumps[i] = -1;
         }else{
            for(j = 1;j<=nums[i];j++){
                if(jumps[i] > 0 && jumps[i+j] >= 0){
                    if((1 + jumps[i+j])<jumps[i]){
                        jumps[i] = 1 + jumps[i+j];
                    }
                }                
            } 
            if(jumps[i] == 2147483647){
                jumps[i] = -1;
            }
         }
    }

    return 0;
}

int jump(int* nums, int numsSize) {
    int *jumps = (int *)malloc(sizeof(int)*numsSize);      
    calculateJump(nums,numsSize,jumps);  
    return jumps[0];
}
import pygame import time class Fighter: def __init__(self, x, y, image_path, attack_image_path): self.x = x self.y = y self.speed = 2 self.image = pygame.image.load(image_path) self.attack_image = pygame.image.load(attack_image_path) self.is_jumping = False self.jump_height = 15 self.jump_count = self.jump_height self.health = 100 self.is_attacking = False self.last_attack_time = 0 self.attack_delay = 0.3 def draw(self, screen): if self.is_attacking: screen.blit(self.attack_image, (self.x, self.y)) else: screen.blit(self.image, (self.x, self.y)) def move(self, keys): pass def attack(self, enemy): current_time = time.time() if current_time - self.last_attack_time >= self.attack_delay: if abs(self.x - enemy.x) <= 50: enemy.health -= 2 if enemy.health < 0: enemy.health = 0 self.is_attacking = True self.last_attack_time = current_time else: self.is_attacking = False class Fighter1(Fighter): def move(self, keys): if keys[pygame.K_a] and self.x > 0: self.x -= self.speed if keys[pygame.K_d] and self.x < 750: self.x += self.speed if keys[pygame.K_k] and not self.is_jumping: self.is_jumping = True if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height if keys[pygame.K_j]: self.attack(fighter2) else: self.is_attacking = False class Fighter2(Fighter): def __init__(self, x, y, image_path, attack_image_path): super().__init__(x, y, image_path, attack_image_path) self.image = pygame.transform.scale(self.image, (int(self.image.get_width() * 0.5), int(self.image.get_height() * 0.5))) self.attack_image = pygame.transform.scale(self.attack_image, (int(self.attack_image.get_width() * 0.5), int(self.attack_image.get_height() * 0.5))) def move(self, keys): if keys[pygame.K_LEFT] and self.x > 0: self.x -= self.speed if keys[pygame.K_RIGHT] and self.x < 750: self.x += self.speed if keys[pygame.K_KP2] and not self.is_jumping: self.is_jumping = True if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height if keys[pygame.K_KP1]: self.attack(fighter1) else: self.is_attacking = False def draw_health_bars(screen, fighter1, fighter2): BAR_LENGTH = 300 BAR_HEIGHT = 20 # 角色 1 的血条 fill1 = (fighter1.health / 100) * BAR_LENGTH outline_rect1 = pygame.Rect(20, 20, BAR_LENGTH, BAR_HEIGHT) fill_rect1 = pygame.Rect(20, 20, fill1, BAR_HEIGHT) pygame.draw.rect(screen, (255, 0, 0), outline_rect1, 2) pygame.draw.rect(screen, (0, 255, 0), fill_rect1) # 角色 2 的血条 fill2 = (fighter2.health / 100) * BAR_LENGTH outline_rect2 = pygame.Rect(480, 20, BAR_LENGTH, BAR_HEIGHT) fill_rect2 = pygame.Rect(480, 20, fill2, BAR_HEIGHT) pygame.draw.rect(screen, (255, 0, 0), outline_rect2, 2) pygame.draw.rect(screen, (0, 255, 0), fill_rect2) pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("格斗游戏") background_image = pygame.image.load("background.jpg") background_image = pygame.transform.scale(background_image, (width, height)) fighter1 = Fighter1(100, 500, "webwxgetmsgimg.jpg", "attack1.jpg") fighter2 = Fighter2(600, 500, "_cgi-bin_mmwebwx-bin_webwxgetmsgimg__&MsgID=4151463399841909084&skey=@crypt_3b46ed7a_7efc25287b2eeaad8d2392b1642162ed&mmweb_appid=wx_webfilehelper.jpg", "attack2.jpg") running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() fighter1.move(keys) fighter2.move(keys) screen.blit(background_image, (0, 0)) draw_health_bars(screen, fighter1, fighter2) fighter1.draw(screen) fighter2.draw(screen) pygame.display.flip() pygame.quit() 我要让角色的跳跃高度降低
最新发布
10-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值