zoukankan      html  css  js  c++  java
  • hdu 5402 Travelling Salesman Problem(大模拟)

    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
     
    真是个鸟题,一开始把情况都考虑了,交上去直接WA了,后来意识到是n、m同为偶数有错。  其实只要找找规律就可以得出找的最小值的横纵坐标的和要为奇数,不过这个的顺序也是很难写啊。。。。。。
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 int n,m;
      6 int mp[106][106];
      7 int x,y;
      8 int minn;
      9 void get()
     10 {
     11     x = 1;    y = 2;
     12     for (int i = 1; i <= n;i++)
     13         for (int j = 1; j <= m; j++)
     14             if (((i + j) & 1) && mp[x][y] > mp[i][j]) x = i, y = j;
     15 }
     16 int main()
     17 {
     18     while(scanf("%d%d",&n,&m)==2)
     19     {
     20         int sum=0;
     21         for(int i=1;i<=n;i++)
     22         {
     23             for(int j=1;j<=m;j++)
     24             {
     25                 scanf("%d",&mp[i][j]);
     26                 sum+=mp[i][j];
     27             }
     28         }
     29         if((n&1))
     30         {
     31 
     32             printf("%d
    ",sum);
     33             if(n==1)
     34             {
     35                 for(int i=1;i<m;i++)
     36                     printf("R");
     37                 printf("
    ");
     38                 continue;
     39             }
     40             for(int i=1;i<m;i++)
     41                     printf("R");
     42             printf("D");
     43             for(int i=2;i<=n;i++)
     44             {
     45                 if(i%2==0)
     46                 {
     47                     for(int j=1;j<m;j++)
     48                         printf("L");
     49                     printf("D");
     50                 }
     51                 else
     52                 {
     53                       if(i!=n)
     54                       {
     55                           for(int i=1;i<m;i++)
     56                              printf("R");
     57                           printf("D");
     58                       }
     59                       else
     60                       {
     61                           for(int i=1;i<m;i++)
     62                             printf("R");
     63                       }
     64                 }
     65             }
     66             printf("
    ");
     67 
     68         }
     69         else if((m%2))
     70         {
     71             printf("%d
    ",sum);
     72             if(m==1)
     73             {
     74                 for(int i=1;i<n;i++)
     75                   printf("D");
     76                 printf("
    ");
     77                 continue;
     78             }
     79             for(int i=1;i<n;i++)
     80                   printf("D");
     81             printf("R");
     82             for(int i=2;i<=m;i++)
     83             {
     84                 if(i%2==0)
     85                 {
     86                     for(int j=1;j<n;j++)
     87                         printf("U");
     88                     printf("R");
     89                 }
     90                 else
     91                 {
     92                     if(i!=m)
     93                     {
     94                         for(int j=1;j<n;j++)
     95                             printf("D");
     96                         printf("R");
     97                     }
     98                     else
     99                     {
    100                         for(int j=1;j<n;j++)
    101                             printf("D");
    102                     }
    103                 }
    104             }
    105             printf("
    ");
    106         }
    107         else if((m%2)==0 && (n%2)==0)
    108         {
    109                     get();
    110                     printf("%d
    ", sum - mp[x][y]);
    111                     for (int i = 1; i <= n; i += 2)
    112                     {
    113                         if (x == i || x == i + 1)
    114                         {
    115                             for (int j = 1; j < y; j++)
    116                             {
    117                                 if (j & 1) printf("D"); else printf("U");
    118                                 printf("R");
    119                             }
    120                             if (y < m) printf("R");
    121                             for (int j = y + 1; j <= m; j++)
    122                             {
    123                                 if (j & 1) printf("U"); else printf("D");
    124                                 if (j < m) printf("R");
    125                             }
    126                             if (i < n - 1) printf("D");
    127                         }
    128                         else if (i < x)
    129                         {
    130                             for (int j = 1; j < m; j++) printf("R");
    131                             printf("D");
    132                             for (int j = 1; j < m; j++) printf("L");
    133                             printf("D");
    134                         }
    135                         else
    136                         {
    137                             for (int j = 1; j < m; j++) printf("L");
    138                             printf("D");
    139                             for (int j = 1; j < m; j++) printf("R");
    140                             if (i < n - 1) printf("D");
    141                         }
    142                     }
    143                     printf("
    ");
    144         }
    145 
    146 
    147     }
    148     return 0;
    149 }
    View Code
     
  • 相关阅读:
    031:verbatim 标签
    030:spaceless和autoescape 标签
    WinForm webbrowser控件的使用
    c# WebBrowser开发参考资料--杂七杂八
    C# Winform WebBrowser控件
    使用webBrowser进行C#和JS通讯
    webBrowser 应用编程函数总结
    C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
    利用WebBrowser控件实现百度自动搜索
    c#winform使用WebBrowser 大全
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4740430.html
Copyright © 2011-2022 走看看