HDU 5402 Travelling Salesman Problem(多校9 模拟)
题目链接: 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)这个点均能够走完剩下的全部点。
剩下模拟就可以。
- n,m当中一个为奇数的时候,相似下图走法就可以。顺着偶数边走,若均为奇数,则随意都可。
- n。m均为偶数,先找出最小的位置(ni,nj)且ni和nj奇偶不同的位置(下图中(ni,nj)为黑点位置)。
- 假设nj为奇数,相似下图走法就可以。
- 假设nj为偶数,相似下图走法就可以。
- 特别的是nj为1的时候由于不能向左分出一列。所以向右分出一列。特判就可以。
- 假设nj为奇数,相似下图走法就可以。
代码。
#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;
}
HDU 5402 Travelling Salesman Problem(多校9 模拟)的更多相关文章
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- HDU 5402 : Travelling Salesman Problem
题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...
- hdu 5402 Travelling Salesman Problem (技巧,未写完)
题意:给一个n*m的矩阵,每个格子中有一个数字,每个格子仅可以走一次,问从(1,1)走到(n,m) 的路径点权之和. 思路: 想了挺久,就是有个问题不能短时间证明,所以不敢下手. 显然只要n和m其中一 ...
- HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
随机推荐
- vue-devtools安装
https://www.cnblogs.com/yuqing6/p/7440549.html
- csu-2018年11月月赛Round2-div1题解
csu-2018年11月月赛Round2-div1题解 A(2191):Wells的积木游戏 Description Wells有一堆N个积木,标号1~N,每个标号只出现一次 由于Wells是手残党, ...
- Leetcode 560.和为k的子数组
和为k的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1 ...
- linux基础(基本命令)
Linux学习 1.Linux安装.配置 Linux的操作背景介绍 Linux操作系统 开源.自由且开发源代码的类Unix操作系统 厂商较多 著名的有R ...
- table中填写数据并批量增加
<table class = "table jtable table-bordered table-striped hide" id = "table_1" ...
- BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT
分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...
- js 一/二维数组排序
JavaScript中数组排序方法 用到的最多的当然是封装好的sort()方法了 一:sort()方法怎么使用? sort方法并不像我们想的那么容易使用,不是单纯的arr.sort()就行了,需要我们 ...
- hdu 5930 GCD 线段树上二分/ 强行合并维护信息
from NOIP2016模拟题28 题目大意 n个点的序列,权值\(<=10^6\) q个操作 1.单点修改 2.求所有区间gcd中,不同数个数 分析 1.以一个点为端点,向左或向右的gcd种 ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...
- div模拟dropdownlist控件 div下拉菜单
原文发布时间为:2009-10-16 -- 来源于本人的百度文章 [由搬家工具导入] 控件发布:div2dropdownlist(div模拟dropdownlist控件) div3dropdownli ...