首页 技术 正文
技术 2022年11月20日
0 收藏 325 点赞 2,494 浏览 2659 个字

贪吃蛇

Time Limit:1000MS Memory Limit:65536K
Total Submit:9 Accepted:2

Description

有童年的孩子都玩过这个经典游戏,不过这里的规则又有点不同,现在有一个N*M(N,M<=100)的方形矩形,在这个矩形的每一个方格上都放有若干个樱桃,一条可爱的小蛇从矩形的
左上角开始出发,每次移动都只能移动一格,向右或向下,而每到达一格贪吃的小蛇都会吧该位置上的樱桃吃个一干二净,直到到达右下角时停止。而贪吃的小蛇不怕撑死,它只想吃到最多
的樱桃,请你告诉它他最多能吃到多少樱桃以及具体路线吧。(数据保证最优路线只有一条)

Input

每个输入包含多个测试用例,每个测试用例第一行给出N,M,接下来N行M列数据代表每个位置上的樱桃个数。(矩阵坐标从(1,1)开始)。

Output

对于每个测试用例输出第一行为能吃到的最大樱桃个数,接下来为小蛇所需要走的路线的坐标,每个坐标占一行。

Sample Input

4 4
1 2 3 7
3 4 2 1
1 5 4 8
10 3 0 3

Sample Output

28
(1,1)
(2,1)
(2,2)
(3,2)
(3,3)
(3,4)
(4,4)

Source

https://www.shuzhiduo.com/A/pRdBBVWPdn/icpc7th@ahstu

思路:

这一题需要用到动态规划,由于贪吃蛇每次只能往右或往下走,所以对于每一个格子而言,当前格子的最大值(dp[i][j]) =  当前格子的值(a[i][j]) + max{当前格子上方格子的最大值dp[i-1][j],  当前格子左方格子的最大值dp[i][j-1]}。

即 表达式为dp[i][j] = a[i][j] + max(dp[i-1][j], dp[i][j-1])

好了,最大值即为dp[n][m], 求出了最大值,就需要求路径了,因为我们之前把每个格子的最大值都求出来了,所以从右下角开始倒推即可,首先将最后一个格子的坐标保存起来,我是放在在了stack中,然后依次放入当前上方的格子或左边的格子的坐标,因为当前格子的最大值是上方或左方格子的最大值+当前格子最初的值得来的,所以只需保存上方和左方格子中较大一个格子的坐标

需要注意的是当到了边界时,在左边界,只可以往上走了,所以需要把格子上方的坐标都保存起来,同理上方也一样,下面给出,c++和Java版的代码

欢迎大家提出宝贵意见

c++

#include <iostream>
#include <cstdio>
#include <stack>using namespace std;stack<int> st;
int a[101][101], dp[101][101], n, m;void scan()
{
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
scanf("%d", &a[i][j]);
for (int i=0; i<=n; i++)
dp[i][0] = 0;
for (int j=0; j<=m; j++)
dp[0][j] = 0;
}int solve()
{
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
dp[i][j] = a[i][j] + max(dp[i-1][j], dp[i][j-1]);
return dp[n][m];
}void Pu(int n, int m)
{
st.push(m);
st.push(n);
if (n == 1 && m == 1)
return;
else if (n == 1 && m > 1)
Pu(n, m-1);
else if (n > 1 && m == 1)
Pu(n-1, m);
else {
if (dp[n-1][m] > dp[n][m-1])
Pu(n-1, m);
else
Pu(n, m-1);
}
}void printPath()
{
Pu(n, m);
while (!st.empty()){
printf("(%d,", st.top());
st.pop();
printf("%d)\n", st.top());
st.pop();
}
}int main()
{
while (~scanf("%d%d", &n, &m)){
scan();
printf("%d\n", solve());
printPath();
}
return 0;
}

java

import java.util.Scanner;
import java.util.Stack;public class P1369 {
static int n, m, a[][], dp[][];
static Stack<Integer> stack;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
n = cin.nextInt();
m = cin.nextInt();
a = new int[n+1][m+1];
dp = new int[n+1][m+1];
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++)
a[i][j] = cin.nextInt();
}
System.out.println(solve());
printPath();
}
cin.close();
}
private static void printPath() {
stack = new Stack<>();
Pu(n, m);
while (!stack.isEmpty()) {
System.out.println("("+stack.pop()+","+stack.pop()+")");
}
stack.removeAllElements();
}
private static void Pu(int n, int m) {
stack.push(m);
stack.push(n);
if (n == 1 && m == 1)
return;
else if (n == 1 && m > 1)
Pu(n, m-1);
else if (n > 1 && m == 1)
Pu(n-1, m);
else {
if (dp[n-1][m] > dp[n][m-1])
Pu(n-1, m);
else
Pu(n, m-1);
}
}
private static int solve() {
for (int i=0; i<=n; i++)
dp[i][0] = 0;
for (int j=0; j<=m; j++)
dp[0][j] = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
dp[i][j] = a[i][j] + Math.max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[n][m];
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918