题意:给一个n*m的矩形,每个格子有一个非负数,求一条从(1,1)到(n,m)的路径(不能经过重复的格子),使得经过的数的和最大,输出具体的方案

思路:对于row为奇数的情况,一行行扫下来即可全部走完得到最大和,对于col为奇数的情况一列列扫即可。对于行和列全部为偶数的情况,将所有格子进行黑白染色,起点和终点的颜色一样,而路径上的颜色是交替的,说明总有一个点不能走到,枚举得到不可到点上的最小值,总和减去就是答案。具体的方案构造方法如下:由于只有一个格子被挖掉不能走,考虑整行或整列的走,走完这个格子前面的所有格子,然后把后面的两行或两列走完,这两行或两列相当于一行或一列,那么整个图相当于是奇数行或奇数列的图了,往后走一定可以遍历完。

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-12; /* -------------------------------------------------------------------------------- */ int n, m, sum;
int a[][]; void out() {
printf("%d\n", sum);
if (n & ) {
char ch = 'R';
for (int i = ; i < n; i ++) {
for (int j = ; j < m; j ++) putchar(ch);
if (i < n - ) putchar('D');
ch = ch == 'L'? 'R' : 'L';
}
}
else {
char ch = 'D';
for (int j = ; j < m; j ++) {
for (int i = ; i < n; i ++) putchar(ch);
if (j < m - ) putchar('R');
ch = ch == 'D'? 'U' : 'D';
}
}
putchar('\n');
} void work() {
int minnum = INF, x, y;
for (int i = ; i < n; i ++) {
for (int j = ; j < m; j ++) {
bool r = i & , c = j & ;
if ((r == c)) continue;
if (umin(minnum, a[i][j])) {
x = i;
y = j;
}
}
}
printf("%d\n", sum - minnum);
if (x & ) {
char ch = 'D';
for (int j = ; j < y; j ++) {
for (int i = ; i < n; i ++) putchar(ch);
putchar('R');
ch = ch == 'D'? 'U' : 'D';
}
ch = 'R';
for (int i = ; i < x; i ++) {
putchar(ch);
putchar('D');
ch = ch == 'L'? 'R' : 'L';
}
for (int i = x + ; i < n; i ++) {
putchar('D');
putchar(ch);
ch = ch == 'L'? 'R' : 'L';
}
if (y < m - ) {
putchar('R');
ch = 'U';
for (int j = y + ; j < m; j ++) {
for (int i = ; i < n; i ++) putchar(ch);
if (j < m - ) putchar('R');
ch = ch == 'D'? 'U' : 'D';
}
}
}
else {
char ch = 'R';
for (int i = ; i < x; i ++) {
for (int j = ; j < m; j ++) putchar(ch);
putchar('D');
ch = ch == 'R'? 'L' : 'R';
}
ch = 'D';
for (int j = ; j < y; j ++) {
putchar(ch);
putchar('R');
ch = ch == 'U'? 'D' : 'U';
}
for (int j = y + ; j < m; j ++) {
putchar('R');
putchar(ch);
ch = ch == 'U'? 'D' : 'U';
}
if (x < n - ) {
putchar('D');
ch = 'L';
for (int i = x + ; i < n; i ++) {
for (int j = ; j < m; j ++) putchar(ch);
if (i < n - ) putchar('D');
ch = ch == 'R'? 'L' : 'R';
}
}
}
putchar('\n');
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> n >> m) {
sum = ;
for (int i = ; i < n; i ++) {
for (int j = ; j < m; j ++) {
scanf("%d", &a[i][j]);
sum += a[i][j];
}
}
if (n % || m % ) out();
else work();
}
return ;
}

[hdu5402 Travelling Salesman Problem]YY的更多相关文章

  1. hdu5402 Travelling Salesman Problem

    Problem Description Teacher Mai is in a maze with n rows and m columns. There is a non-negative numb ...

  2. PAT A1150 Travelling Salesman Problem (25 分)——图的遍历

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  3. PAT 甲级 1150 Travelling Salesman Problem

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...

  4. 构造 - HDU 5402 Travelling Salesman Problem

    Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...

  5. 1150 Travelling Salesman Problem(25 分)

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  6. HDU 5402 Travelling Salesman Problem (构造)(好题)

    大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...

  7. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

  8. PAT_A1150#Travelling Salesman Problem

    Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...

  9. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

随机推荐

  1. HTTPoxy漏洞(CVE-2016-5385)复现记录

    漏洞介绍: httpoxy是cgi中的一个环境变量:而服务器和CGI程序之间通信,一般是通过进程的环境变量和管道. CGI介绍 CGI 目前由 NCSA 维护,NCSA 定义 CGI 如下:CGI(C ...

  2. 必须先理解的RocketMQ入门手册,才能再次深入解读

    RocketMQ入门手册 RocketMQ是一个分布式.队列模型的开源消息中间件,前身是MetaQ,是阿里研发的一个队列模型的消息中间件,后开源给apache基金会成为了apache的顶级开源项目,具 ...

  3. C#集合ArrayList、泛型集合List(3)

    数组的制约:局限性.有多少放多少,要想追加,就必须重新再定义一个数组,这就造成了资源的极大浪费而且性能消耗也比较大.因此此操作不太推荐.所以集合就来了. ,,,} 创建集合: ArrayList li ...

  4. 关于暴力破解的一些学习笔记(pikachu)

    这几天的笔记都懒得发博客都写在本地了,随缘搬上来 什么是暴力破解 就是在攻击者不知道目标账号密码情况下的,对目标系统的常识性登陆 一般会采用一些工具+特定的字典 来实现高效的连续的尝试性登陆 一个有效 ...

  5. 字符串的z型转换

    class Solution(object):     def convert(self, s, numRows):         if numRows==1:             return ...

  6. 2019-2020-1 20199328《Linux内核原理与分析》第九周作业

    笔记部分 2019/11/12 14:45:44 从CPU和内存的角度看linux系统的运行 CPU角度:首先我们进行了系统调度,然后系统进入内核态,把信息压栈,然后我们进行进程管理,由于进入系统调用 ...

  7. vue2.x学习笔记(三十一)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12683075.html. 安全 现在的企业都比较在意信息系统的安全问题,在使用vue的过程中也要注意这一点. 报告 ...

  8. 10倍处理能力 阿里云推云上首个支持12层4K非编NAS产品

    5月23日,阿里云在2017云栖大会·成都峰会上正式推出了云上首个支持广电级非编的文件存储产品------NAS Plus,作为阿里云文件存储NAS的升级款,NAS Plus提供高达200Gbps的吞 ...

  9. NLP入门之语音模型原理

    这一篇文章其实是参考了很多篇文章之后写出的一篇对于语言模型的一篇科普文,目的是希望大家可以对于语言模型有着更好地理解,从而在接下来的NLP学习中可以更顺利的学习. 1:传统的语音识别方法: 这里我们讲 ...

  10. 机器学习之分类回归树(python实现CART)

    之前有文章介绍过决策树(ID3).简单回顾一下:ID3每次选取最佳特征来分割数据,这个最佳特征的判断原则是通过信息增益来实现的.按照某种特征切分数据后,该特征在以后切分数据集时就不再使用,因此存在切分 ...