HDU-5402 Travelling Salesman Problem(模拟)

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

比赛时已经想出和题解大致一样的思路,只是绕格子的方式分的太细,结果没时间写完(主要是自己太渣)

题解:

首先如果nn为奇数或者mm为奇数,那么显然可以遍历整个棋盘。

如果n,mn,m都为偶数,那么讲棋盘黑白染色,假设(1,1)(1,1)(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。

构造方法是这样,首先RRRRDLLLLD这样的路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。然后走完这两行,接着按LLLLDRRRR这样的路径往下走。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n,m,w[105][105],r,c,v;

int main() {
    int i,j,sum,t,tr,tc;
    while(2==scanf("%d%d",&n,&m)) {
        sum=0,v=0x7fffffff;
        for(i=0;i<n;++i)
            for(j=0;j<m;++j) {
                scanf("%d",&w[i][j]);
                sum+=w[i][j];
                if(((i+j)&1)&&w[i][j]<v)
                    v=w[r=i][c=j];
            }
        if(n&1) {//行为奇数
            printf("%d\n",sum);
            for(i=0,t=n>>1;i<t;++i) {
                for(j=1;j<m;++j)
                    printf("R");
                printf("D");
                for(j=1;j<m;++j)
                    printf("L");
                printf("D");
            }
            for(j=1;j<m;++j)
                printf("R");
        }
        else if(m&1) {//列为奇数
            printf("%d\n",sum);
            for(i=0,t=m>>1;i<t;++i) {
                for(j=1;j<n;++j)
                    printf("D");
                printf("R");
                for(j=1;j<n;++j)
                    printf("U");
                printf("R");
            }
            for(j=1;j<n;++j)
                printf("D");
        }
        else {//这样固定的绕格子方式太厉害了,完全想不到这种适合各种情况的方法...
            printf("%d\n",sum-v);
            tr=r>>1,tc=c>>1;
            for(i=0;i<tr;++i) {
                for(j=1;j<m;++j)
                    printf("R");
                printf("D");
                for(j=1;j<m;++j)
                    printf("L");
                printf("D");
            }
            for(i=0;i<tc;++i)
                printf("DRUR");
            if(r&1)
                printf("RD");
            else
                printf("DR");
            for(i=tc+1,t=m>>1;i<t;++i)
                printf("RURD");
            for(i=tr+1,t=n>>1;i<t;++i) {
                printf("D");
                for(j=1;j<m;++j)
                    printf("L");
                printf("D");
                for(j=1;j<m;++j)
                    printf("R");
            }
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(HDU)