多校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(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
  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
",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("
");
 39         }
 40         else if(m%2==1)
 41         {
 42             printf("%d
",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("
");
 59         }
 60         else
 61         {
 62             mi=a[1][2];
 63             x=1,y=2;
 64             //printf("ok
");
 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
",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
");
 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
");
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("
");
148         }
149     }
150     return 0;
151 }
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4771398.html