天天看點

hdu 5402 ,Travelling Salesman Problem,2015多校聯合訓練賽#9 Travelling Salesman Problem

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(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
        

Author xudyh  

Source 2015 Multi-University Training Contest 9

當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

*/
           

繼續閱讀