zoukankan      html  css  js  c++  java
  • hdu5402 Travelling Salesman Problem

    当n,m有一个为奇数时,所有点都可被覆盖。

    当n,m全为偶数时,考虑从方格上的点(i,j)走到相邻点,其横纵坐标之和的奇偶性改变一次,因此从偶数点(1,1)走到偶数点(n,m),

    经过的偶数点比奇数点多一,由于表格上的奇数点数目和偶数点相同,因此至少有一个奇数点不被遍历,找出并绕过该点即可。

    路径经过构造可得。

    http://acm.hdu.edu.cn/listproblem.php?vol=45

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 const int maxn = 1e2 + 10;
     8 const int inf = 0x3f3f3f3f;
     9 
    10 int G[maxn][maxn];
    11 int n, m, x1, y1, low;
    12 int sum;
    13 
    14 int main(){
    15     //freopen("in.txt", "r", stdin);
    16     while(~scanf("%d%d", &n, &m)){
    17         low = inf;
    18         sum = 0;
    19         for(int i = 1; i <= n; i++){
    20             for(int j = 1; j <= m; j++){
    21                 scanf("%d", &G[i][j]);
    22                 sum += G[i][j];
    23                 if((i + j) % 2 && G[i][j] < low) low = G[i][j], x1 = i, y1 = j;
    24             }
    25         }
    26         if(n % 2){
    27             printf("%d
    ", sum);
    28             for(int i = 1; i < m; i++) putchar('R');
    29             for(int i = 0; i < (n - 1) / 2; i++){
    30                 putchar('D');
    31                 for(int j = 1; j < m; j++) putchar('L');
    32                 putchar('D');
    33                 for(int j = 1; j < m; j++) putchar('R');
    34             }
    35             putchar('
    ');
    36             continue;
    37         }
    38         if(m % 2){
    39             printf("%d
    ", sum);
    40             for(int i = 1; i < n; i++) putchar('D');
    41             for(int i = 0; i < (m - 1) / 2; i++){
    42                 putchar('R');
    43                 for(int j = 1; j < n; j++) putchar('U');
    44                 putchar('R');
    45                 for(int j = 1; j < n; j++) putchar('D');
    46             }
    47             putchar('
    ');
    48             continue;
    49         }
    50         printf("%d
    ", sum - low);
    51         if(y1 % 2){
    52             for(int i = 0; i < x1 / 2 - 1; i++){
    53                 for(int j = 1; j < m; j++) putchar('R');
    54                 putchar('D');
    55                 for(int j = 1; j < m; j++) putchar('L');
    56                 putchar('D');
    57             }
    58             for(int i = 0; i < (y1 - 1) / 2; i++) printf("DRUR");
    59             printf("RD");
    60             for(int i = 0; i < (m - y1 - 1) / 2; i++) printf("RURD");
    61             for(int i = 0; i < (n - x1) / 2; i++){
    62                 putchar('D');
    63                 for(int j = 1; j < m; j++) putchar('L');
    64                 putchar('D');
    65                 for(int j = 1; j < m; j++) putchar('R');
    66             }
    67             putchar('
    ');
    68             continue;
    69         }
    70         for(int i = 0; i < (x1 - 1) / 2; i++){
    71             for(int j = 1; j < m; j++) putchar('R');
    72             putchar('D');
    73             for(int j = 1; j < m; j++) putchar('L');
    74             putchar('D');
    75         }
    76         printf("DR");
    77         for(int i = 0; i < (y1 - 2) / 2; i++) printf("URDR");
    78         for(int i = 0; i < (m - y1) / 2; i++) printf("RURD");
    79         for(int i = 0; i < (n - x1 - 1) / 2; i++){
    80             putchar('D');
    81             for(int j = 1; j < m; j++) putchar('L');
    82             putchar('D');
    83             for(int j = 1; j < m; j++) putchar('R');
    84         }
    85         putchar('
    ');
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    What is the difference between the ways to implement inheritance in javascript.
    understand dojo/domReady!
    Using dijit/Destroyable to build safe Components
    Session Tracking Approaches
    difference between forward and sendredirect
    What is the DD in java web application
    棋牌游戏-后端架构(1)
    成为技术领导者笔记--领导的MOI模型
    最小表示法
    SGI STL rope
  • 原文地址:https://www.cnblogs.com/astoninfer/p/4800966.html
Copyright © 2011-2022 走看看