题目链接:

pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402

题意:给出一个n×m的矩阵,位置(i。j)有一个非负权值。

每一个点仅仅能经过一次。求从(1。1)到(n。m)权值总和最大的和。还需输出路径。

思路:由于走的点越多越好,所以得到规律,当n,m随意一个为奇数时。均能够走全然部点。

当n,m全为偶数时,当点(i。j)的i和j不同奇偶时,则除了(i,j)这个点均能够走完剩下的全部点。

剩下模拟就可以。

  1. n,m当中一个为奇数的时候,相似下图走法就可以。顺着偶数边走,若均为奇数,则随意都可。
  2. n。m均为偶数,先找出最小的位置(ni,nj)且ni和nj奇偶不同的位置(下图中(ni,nj)为黑点位置)。
    1. 假设nj为奇数,相似下图走法就可以。
    2. 假设nj为偶数,相似下图走法就可以。
    3. 特别的是nj为1的时候由于不能向左分出一列。所以向右分出一列。特判就可以。

代码。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <string>
#include <math.h>
using namespace std; const int N = 1e2 + 10; void out(int n, int m, char a, char b, char c) {
for (int i = 1; i <= n; i++) {
if (i > 1) printf("%c", a);
for (int j = 1; j < m; j++) {
if (i & 1) printf("%c", b);
else printf("%c", c);
}
}
} int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
int ans = 0, tmp = 10005;
int ni, nj;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
ans += x;
if (((i + j) & 1) && x < tmp) {
tmp = x;
ni = i;
nj = j;
}
}
}
if (n % 2 == 0 && m % 2 == 0)
ans -= tmp;
printf("%d\n", ans);
if (n & 1) {
out(n, m, 'D', 'R', 'L');
}
else if (m & 1) {
out(m, n, 'R', 'D', 'U');
}
else {
if (nj == 1) {
if (ni - 1 > 0) {
out(ni - 1, 2, 'D', 'R', 'L');
printf("D");
}
if (ni < n) {
printf("D");
out(n - ni, 2, 'D', 'L', 'R');
}
if (m > 2) {
printf("R");
out(m - 2, n, 'R', 'U', 'D');
}
printf("\n");
continue;
}
if (nj & 1) {
if (nj - 2 > 0) {
out(nj - 2, n, 'R', 'D', 'U');
printf("R");
}
if (n - ni > 0) {
out(n - ni, 2, 'U', 'R', 'L');
printf("U");
}
if (ni - 1 > 0) {
printf("U");
out(ni - 1, 2, 'U', 'R', 'L');
}
if (m - nj > 0) {
printf("R");
out(m - nj, n, 'R', 'D', 'U');
}
}
else {
if (nj - 2 > 0) {
out(nj - 2, n, 'R', 'D', 'U');
printf("R");
}
if (ni - 1 > 0) {
out(ni - 1, 2, 'D', 'R', 'L');
printf("D");
}
if (ni < n) {
printf("D");
out(n - ni, 2, 'D', 'R', 'L');
}
if (m - nj > 0) {
printf("R");
out(m - nj, n, 'R', 'U', 'D');
}
}
}
printf("\n");
}
return 0;
}

最新文章

  1. OC语言-05-OC语言-内存管理
  2. tcp MSL
  3. BZOJ3673 : 可持久化并查集
  4. 几点基于Web日志的Webshell检测思路
  5. iOS手势学习UIGestureRecognizer &amp; cocos2d 手势推荐
  6. global 用法
  7. JVM 优化问题
  8. &lt;四&gt; jQuery 事件
  9. 网页JavaScript3
  10. Thread 常搞混的几个概念sleep、wait、yield、interrupt (转)
  11. NodeJS stream 一:Buffer
  12. [0] C# 扩展方法(Extension Method)
  13. THUWC2018滚粗记
  14. 029、限制容器的block IO(2019-01-24 周四)
  15. ansible批量管理工具的搭建与简单的操作
  16. 高性能网络编程之IO和NIO阻塞分析
  17. linux 内核参数优化----最大线程数限制及当前线程数查询
  18. linux/centos6.5下编译安装python2.7和python3.5.2
  19. lightgbm原理以及Python代码
  20. Final阶段第1周/共1周 Scrum立会报告+燃尽图 01

热门文章

  1. CSU-2034 Column Addition
  2. Leetcode 605.种花问题
  3. aspx页面直接访问后台方法
  4. 如何获取JavaCard剩余空间
  5. CentOS7重新生成 /boot/grub2/grub.cfg
  6. 购物(sum)
  7. splay模板整理
  8. git 以及 工作区 版本库 暂存区
  9. pat 甲级 1098. Insertion or Heap Sort (25)
  10. 洛谷 P1072 Hankson 的趣味题