akoj-1369 贪吃蛇
来源:互联网 发布:男士淡斑清洁面膜知乎 编辑:程序博客网 时间:2024/06/07 02:41
贪吃蛇
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
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]; }}
- akoj-1369 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- ios-Alcatraz以及插件使用--
- HiveQL数据定义
- matlab 基础 —— 文本文件读取
- HttpModule事件
- httpclient basic认证
- akoj-1369 贪吃蛇
- 十二生肖排序的真实原因
- thrift(1):TNonblockingServer分析
- Qt/C++ 音乐播放器源码
- cocos2dx 3.10 黄金矿工学习笔记
- 线程sleep() 和 wait()简单比较
- POJ 1840 Eqs
- opensuse上使用zypper代替yum
- Node.js 简单的HelloWorld