hdu 5402 ,Travelling Salesman Problem,2015多校联合训练赛#9

探讨了在有限网格中从左上角到右下角的最优化路径问题,目标是最大化通过路径上的数字之和,并提供了一种解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 183    Accepted Submission(s): 70
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(1n,m100,nm2) .

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,y1) , "R" means you walk to cell  (x,y+1) , "U" means you walk to cell  (x1,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
 

Author
xudyh
 

Source

当n,m有奇数时,可以选择一条蛇形道路全部点都取到。算总和就行。

如果n,m为偶数时,选择(i+j)%==1的位置中最小的点不走,其他全部点选择即可。

还是蛇形道路,但是对于选择不走的那个位置所在行和相邻行,要特殊处理,优先上下方向走就行。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int num[111][111];

int n,m;
void work(){
    long long ans = 0;
    for(int i = 0;i < n; i++)
        for(int j = 0;j < m; j++)
            ans += num[i][j];
    printf("%I64d\n",ans);
    if(n % 2 == 1){
        for(int i = 0;i < n; i++){
            for(int j = 1;j < m; j++){
                if(i%2==0) putchar('R');
                else putchar('L');
            }
            if(i != n - 1) putchar('D');
        }
    }
    else {
        for(int i = 0;i < m; i++){
            for(int j = 1;j < n; j++){
                if(i%2  == 0) putchar('D');
                else putchar('U');
            }
            if(i != m-1)putchar('R');
        }
    }
}
int check[111][111];
int ok(int x,int y){
    if(x < 0 || x == n || y < 0 || y == m) return 0;
    if(check[x][y] == 1) return 0;
    return 1;
}
void dfs(int x,int y,int c1,int c2){
    check[x][y] = 1;
    if(x == c1){
        if(ok(x+1,y)){
            putchar('D');
            dfs(x+1,y,c1,c2);
        }
        else if(ok(x,y+1)){
            putchar('R');
            dfs(x,y+1,c1,c2);
        }
        else if(ok(x,y-1)){
            putchar('L');
            dfs(x,y-1,c1,c2);
        }
    }
    else if(x == c2){
        if(ok(x-1,y)){
            putchar('U');
            dfs(x-1,y,c1,c2);
        }
        else if(ok(x,y+1)){
            putchar('R');
            dfs(x,y+1,c1,c2);
        }
        else if(ok(x,y-1)){
            putchar('L');
            dfs(x,y-1,c1,c2);
        }
        else if(ok(x+1,y)){
            putchar('D');
            dfs(x+1,y,c1,c2);
        }
    }
    else {
        if(ok(x,y+1)){
            putchar('R');
            dfs(x,y+1,c1,c2);
        }
        else if(ok(x,y-1)){
            putchar('L');
            dfs(x,y-1,c1,c2);
        }
        else if(ok(x+1,y)){
            putchar('D');
            dfs(x+1,y,c1,c2);
        }
    }
}
void work2(){
    long long ans = 0, res = 10000000000ll;
    int c1,c2,y;
    for(int i = 0 ;i < n; i++)
        for(int j = 0;j < m; j++){
            ans += num[i][j];
            if((i+j)% 2 == 1){
                if(res > num[i][j]){
                    res = num[i][j];
                    c1 = i;
                    y = j;
                }
            }
        }
    printf("%I64d\n",ans-res);
    if(c1 == 0) c2 = 1;
    else c2 = c1-1;
    memset(check,0,sizeof(check));
    check[c1][y] = 1;
    dfs(0,0,min(c1,c2),max(c1,c2));
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i = 0;i < n; i++)
            for(int j = 0;j < m; j++)
                scanf("%d",&num[i][j]);
        if( n%2 == 1 || m % 2 == 1) work();
        else work2();
        printf("\n");
    }
    return 0;
}

/*

3 3
2 3 3
3 3 3
3 3 2
3 2
2 3
3 3
3 3
2 3
2 3 3
3 3 3
4 4
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2

2 2
1 2
5 2

1 2
1 2

2 1
2 1

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值