多校9 1007 Travelling Salesman Problem

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 829    Accepted Submission(s): 182
Special Judge

Problem Description

Teacher Mai is in a maze with n rows and m columns. There is a non-negative number in each cell. Teacher Mai wants to walk from the top left corner (1,1) to the bottom right corner (n,m). He can choose one direction and walk to this adjacent cell. However, he can‘t go out of the maze, and he can‘t visit a cell more than once.

Teacher Mai wants to maximize the sum of numbers in his path. And you need to print this path.

Input

There are multiple test cases.

For each test case, the first line contains two numbers n,m(1≤n,m≤100,n∗m≥2).

In following n lines, each line contains m numbers. The j-th number in the i-th line means the number in the cell (i,j). Every number in the cell is not more than 104.

Output

For each test case, in the first line, you should print the maximum sum.

In the next line you should print a string consisting of "L","R","U" and "D", which represents the path you find. If you are in the cell (x,y), "L" means you walk to cell (x,y−1), "R" means you walk to cell (x,y+1), "U" means you walk to cell (x−1,y), "D" means you walk to cell (x+1,y).

Sample Input

3 3
2 3 3
3 3 3
3 3 2

Sample Output

25
RRDLLDRR

  1 #include <stdio.h>
  2 #include <string.h>
  3 int main()
  4 {
  5     int n,m;
  6     int a[106][106],s,x,y;
  7     int i,j,k;
  8     while(scanf("%d %d",&n,&m)!=EOF)
  9     {
 10         s=0;
 11         int mi;
 12         for(i=1;i<=n;i++)
 13         {
 14             for(j=1;j<=m;j++)
 15             {
 16                 scanf("%d",&a[i][j]);
 17                 s=s+a[i][j];
 18             }
 19         }
 20         if(n%2==1)
 21         {
 22             printf("%d\n",s);
 23             for(i=1;i<=n;i++)
 24             {
 25                 if(i%2==1)
 26                 {
 27                     for(j=1;j<m;j++)
 28                         printf("R");
 29                 }
 30                 else
 31                 {
 32                     for(j=1;j<m;j++)
 33                         printf("L");
 34                 }
 35                 if(i!=n)
 36                     printf("D");
 37             }
 38             printf("\n");
 39         }
 40         else if(m%2==1)
 41         {
 42             printf("%d\n",s);
 43             for(j=1;j<=m;j++)
 44             {
 45                 if(j%2==1)
 46                 {
 47                     for(i=1;i<n;i++)
 48                         printf("D");
 49                 }
 50                 else
 51                 {
 52                     for(i=1;i<n;i++)
 53                         printf("U");
 54                 }
 55                 if(j!=m)
 56                     printf("R");
 57             }
 58             printf("\n");
 59         }
 60         else
 61         {
 62             mi=a[1][2];
 63             x=1,y=2;
 64             //printf("ok\n");
 65             for(i=1;i<=n;i++)
 66             {
 67                 for(j=1;j<=m;j++)
 68                 {
 69                     if(i%2==1 && j%2==0 && a[i][j]<mi)
 70                     {
 71                         mi=a[i][j];
 72                         x=i,y=j;
 73                     }
 74                     if(i%2==0 && j%2==1 && a[i][j]<mi)
 75                     {
 76                         mi=a[i][j];
 77                         x=i,y=j;
 78                     }
 79                 }
 80             }
 81             s=s-mi;
 82             printf("%d\n",s);
 83             for(i=1;2*i<x;i++)
 84             {
 85                 for(j=1;j<m;j++)
 86                 {
 87                     printf("R");
 88                 }
 89                 printf("D");
 90                 for(j=1;j<m;j++)
 91                 {
 92                     printf("L");
 93                 }
 94                 printf("D");
 95             }
 96             //printf("ok\n");
 97             int flg=1;
 98             for(i=1;i<=m;i++)
 99             {
100                 if(y==i)
101                 {
102                     if(i==m && x!=n-1)
103                     {
104                         printf("D");
105                         continue;
106                     }
107                     if(i!=m)
108                         printf("R");
109                 }
110                 else
111                 {
112                     if(i==m)
113                     {
114                         if(x==n-1 || x==n)
115                             printf("D");
116                         else
117                             printf("DD");
118                     }
119                     else if(flg==1)
120                     {
121                         printf("DR");flg=2;
122                     }
123                     else if(flg==2)
124                     {
125                         printf("UR");flg=1;
126                     }
127                 }
128             }
129             //printf("ok\n");
130             if(x%2==1)
131                 x++;
132             for(i=x+1;i<=n;i++)
133             {
134                 if(i%2==1)
135                 {
136                     for(j=1;j<m;j++)
137                         printf("L");
138                 }
139                 else
140                 {
141                     for(j=1;j<m;j++)
142                         printf("R");
143                 }
144                 if(i!=n)
145                     printf("D");
146             }
147             printf("\n");
148         }
149     }
150     return 0;
151 }

时间: 2024-10-16 14:32:01

多校9 1007 Travelling Salesman Problem的相关文章

多校第九场Travelling Salesman Problem总结

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:n*m的矩阵格子,每个格子有相应的数字,上要从矩阵的左上角走到右下角,要求使得走过的数字之和尽可能多,同时每个格只能走一次,输出走过的数字之和,以及路径 思路:对于n,m任何一个是奇数,那么就能经过所有的格子,如果n,m两个数都是偶数,那么那么讲棋盘黑白染色,假设(1,1)和(n,m)都为黑色,那么这条路径中黑格个数比白格个数多1,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,

构造 - HDU 5402 Travelling Salesman Problem

Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一个n*m的迷宫,每一个格子都有一个非负整数,从迷宫的左上角(1,1)到迷宫的右下角(n,m),并且使得他走过的路径的整数之和最大,问最大和为多少以及他走的路径. analyse: 首先,因为每个格子都是非负整数,而且规定每个格子只能走一次,所以为了使和尽可能大,必定是走的格子数越多越好.这样我们就需

hdu5402 Travelling Salesman Problem(棋盘染色+模拟)

题目: Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 906    Accepted Submission(s): 331 Special Judge Problem Description Teacher Mai is in a maze with n rows and m c

HDOJ 5402 Travelling Salesman Problem 模拟

行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 747    Accepted Submission(s): 272

【HDOJ 5402】Travelling Salesman Problem

[HDOJ 5402]Travelling Salesman Problem 一开始以为是搜索 仔细画了画发现就一模拟 奇数行或奇数列的时候怎么走都能全走完 偶数行偶数列的时候就要挑了 . * . * . * * . * . * . . * . * . * * . * . * . 以4*6为例(如上图 星号可以保证不取其中一个可遍历完全图 点好的话就会连带一些星号 所以绕过星号中的最小值 是最佳遍历方式 输入的时候找到最小值并记录下行列 遍历到改行前以 右走到头 下 左走到头 下 右走到头这种方

HDU 5402 Travelling Salesman Problem (模拟 有规律)

Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 200 Special Judge Problem Description Teacher Mai is in a maze with n rows and m colum

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

大致题意:n*m的非负数矩阵,从(1,1) 只能向四面走,一直走到(n,m)为终点,路径的权就是数的和,输出一条权值最大的路径方案 思路:由于这是非负数,要是有负数就是神题了,要是n,m中有一个是奇数,显然可以遍历,要是有一个偶数,可以画图发现,把图染成二分图后,(1,1)为黑色,总能有一种构造方式可以只绕过任何一个白色的点,然后再遍历其他点,而绕过黑色的点必然还要绕过两个白色点才能遍历全部点,这是画图发现的,所以找一个权值最小的白色点绕过就可以了, 题解给出了证明: 如果n,mn,m都为偶数,

PAT 甲级 1150 Travelling Salesman Problem

https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possib

1150 Travelling Salesman Problem (25 分)

1150 Travelling Salesman Problem (25 分) The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and retu