题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

题意:每次仅仅能走 横着或竖着的 1~k 个格子。求最多能吃到的奶酪。

代码:



#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h> using namespace std; int n, k;
int a[110][110], dp[110][110];
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; bool is_ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= n) return false;
return true;
} int res(int x, int y)
{
int sum = 0,tmp = 0;
if (!dp[x][y])
{
for (int i = 1; i <= k; i++)
{
for (int j = 0; j < 4; j++)
{
int xx = x + dir[j][0] * i;
int yy = y + dir[j][1] * i;
if (is_ok(xx,yy) && a[xx][yy] > a[x][y])
{
sum = res(xx, yy);
tmp = max(tmp, sum);
}
}
}
dp[x][y] = tmp + a[x][y];
}
return dp[x][y];
} int main()
{
while (scanf("%d %d", &n,&k) != EOF)
{
if (n == -1 && k == -1) break;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d",&a[i][j]);
memset(dp, 0, sizeof(dp));
printf("%d\n", res(0,0));
}
return 0;
}

最新文章

  1. 安装Kafka
  2. Elasticsearch索引(company)_Centos下CURL增删改
  3. php字符串笔记
  4. [LeetCode] 423 Reconstruct Original Digits from English
  5. C#winform中DataGridView常用的属性
  6. IIS8托管WCF服务
  7. centos中MySQL,VmTools安装,使用及异常
  8. VMware Workstation 11序列号一枚
  9. vertical-align:top属性
  10. explicit 只对构造函数起作用,用来抑制隐式转换。
  11. ACM—Number Sequence(HDOJ1005)
  12. VS单元测试入门实践教程
  13. 麻瓜之我要学sql,啦啦啦啦
  14. Java经典编程题50道之十四
  15. the c programing language 学习过程4
  16. IntegrityError at /admin/users/userprofile/add/ (1452, &#39;Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_
  17. JS ----实现复制粘贴功能 (剪切板应用clipboardData)
  18. JVM综合调优汇总
  19. Delphi: 圆形进度(环形进度)
  20. windows下安装pthreads扩展注意问题

热门文章

  1. iOS:地图笔记
  2. fl2440 platform总线led字符设备驱动
  3. ElasticSearch 获取分词的Token
  4. win下写任务提交给集群
  5. 按照vue文档使用JavaScript钩子但是却不能执行动画?
  6. 【Qt编程】基于Qt的词典开发系列&amp;lt;八&amp;gt;--用户登录及API调用的实现
  7. node.js开发平台
  8. js apply和call区别
  9. Spring使用经验之Listener综述
  10. 把sqlserver查询结果复制到Excel出现数据记录遗漏