蒜头君的城堡之旅

蒜国地域是一个 nn 行 mm 列的矩阵,下标均从 11开始。蒜国有个美丽的城堡,在坐标 (n, m)(n,m)上,蒜头君在坐标 (1,1)(1,1) 的位置上。蒜头君打算出发去城堡游玩,游玩结束后返回到起点。在出发去城堡的路上,蒜头君只会选择往下或者往右走,而在返回的路上,蒜头君只会选择往上或者往左走,每次只能走一格。已知每个格子上都有一定数量的蒜味可乐,每个格子至多经过一次。

现在蒜头君请你来帮他计算一下,如何计划来回行程,可以收集到最多的蒜味可乐。

输入格式

第一行输入两个整数 n,m(1 \leq n, m \leq 50)n,m(1n,m50),表示蒜国是一个 nn 行 mm 列的矩阵。

接下来输入 nn 行,每行输入 mm 个整数,代表一个 n \times mn×m 的矩阵,每个整数代表对应位置上的蒜味可乐数量,每行的每两个整数之间用一个空格隔开。其中蒜头君的位置和城堡的位置上没有蒜味可乐,用 00 表示,其余位置上的整数范围在 [1,100][1,100] 内。

输出格式

输出一行,输出一个整数,表示蒜头君在来回路上能收集到的蒜味可乐的最大值。

#include <iostream>
#include <cstring>
using namespace std;
int n,m;
int dp[60][60][60][60];
int coordination[60][60];
int main(){
   // memset(dp,0,sizeof(dp));
    //memset(coordination,0,sizeof(coordination));
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>coordination[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            for(int k=1;k<=n;k++){
                for(int l=1;l<=m;l++){
                    if(i==k&&j==l){
                        continue;
                    }
                     if(i+j-k-l!=1){
                        dp[i][j][k][l]=max(max(dp[i-1][j][k-1]
                        [l],dp[i-1][j][k][l-1]),
                   max(dp[i][j-1][k][l-1],dp[i][j-1][k-1][l]))
                            +coordination[i][j]+coordination[k][l];}
                    else {
                        continue;
                    }
                
                    }
                }
            }
        }
    cout<<dp[n-1][m][n][m-1]<<endl;   
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基本的Python海龟命令画城堡的示例代码: ```python import turtle # 设置画笔 t = turtle.Pen() t.speed(0) # 设置画笔速度为最快 # 画城堡墙壁 t.pensize(5) t.color('gray') t.begin_fill() for i in range(4): t.forward(200) t.right(90) t.end_fill() # 画城堡门 t.penup() t.goto(80, -50) t.pendown() t.color('brown') t.begin_fill() t.forward(40) t.left(90) t.forward(80) t.left(90) t.forward(40) t.left(90) t.forward(80) t.end_fill() # 画城堡旗帜 t.penup() t.goto(0, 110) t.pendown() t.color('red') t.begin_fill() t.forward(40) t.left(120) t.forward(80) t.left(120) t.forward(80) t.left(120) t.forward(40) t.end_fill() # 画城堡塔 t.penup() t.goto(-100, 0) t.pendown() t.color('gray') t.begin_fill() t.circle(50) t.end_fill() # 画城堡塔顶 t.penup() t.goto(-100, 100) t.pendown() t.begin_fill() t.color('brown') t.circle(20) t.end_fill() # 画城堡一个塔 t.penup() t.goto(100, 0) t.pendown() t.color('gray') t.begin_fill() t.circle(50) t.end_fill() # 画城堡一个塔顶 t.penup() t.goto(100, 100) t.pendown() t.begin_fill() t.color('brown') t.circle(20) t.end_fill() # 隐藏画笔 t.hideturtle() # 点击关闭窗口 turtle.exitonclick() ``` 代码解释: - 创建一个海龟对象t。 - 画城堡墙壁:设置画笔粗细和颜色,然后使用for循环画出四个边长为200的正方形。 - 画城堡门:使用penup()抬起画笔,goto()移动到门的位置,pendown()放下画笔,然后画出门的形状。 - 画城堡旗帜:使用penup()抬起画笔,goto()移动到旗帜的位置,pendown()放下画笔,然后画出旗帜的形状。 - 画城堡塔:使用penup()抬起画笔,goto()移动到塔的位置,pendown()放下画笔,然后画出塔的形状。 - 画城堡塔顶:使用penup()抬起画笔,goto()移动到塔顶的位置,pendown()放下画笔,然后画出塔顶的形状。 - 隐藏画笔,然后等待用户单击窗口关闭程序。 运上述代码后,将会在窗口中画出一个城堡

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值