hdu 5402 Travelling Salesman Problem

运行这些测试数据都对,但就是wa


//4 4

//2 3 3 3
//3 3 3 3
//3 3 3 3
//3 3 3 2
//answer:
//43
//DRRURDDLLLDRRR
//
//4 4
//2 1 3 3
//3 3 3 3
//3 3 3 3
//3 3 3 2
//answer:
//43
//DRRURDDLLLDRRR
//
//4 4
//2 3 3 1
//3 3 3 3
//3 3 3 3
//3 3 3 2
//answer:
//43
//DRURDRDLLLDRRR
//
//4 4
//2 3 3 3
//1 3 3 3
//3 3 3 3
//3 3 3 2
//answer:
//43
//RDRURDDLLLDRRR
//
//4 4
//2 3 3 3
//3 3 1 3
//3 3 3 3
//3 3 3 2
//answer:
//43
//DRURRDDLLLDRRR
//
//4 4
//2 3 3 3
//3 3 3 3
//3 1 3 3
//3 3 3 2
//answer:
//43
//RRRDLLLDDRRURD
//
//4 4
//2 3 3 3
//3 3 3 3
//3 3 3 1
//3 3 3 2
//answer:
//43
//RRRDLLLDDRURDR
//
//4 4
//2 3 3 3
//3 3 3 3
//3 3 3 3
//1 3 3 2
//answer:
//43
//RRRDLLLDRDRURD
//
//4 4
//2 3 3 3
//3 3 3 3
//3 3 3 3
//3 3 1 2
//answer:
//43
//RRRDLLLDDRURRD
//
//3 4
//2 3 3 3
//3 3 3 3
//3 3 3 2
//answer:
//34
//RRRDLLLDRRR
//
//4 3
//2 3 3
//3 3 3
//3 3 3
//3 3 2
//answer:
//34
//DDDRUUURDDD
//
//4 4
//2 3 3 3
//3 1 3 3
//3 3 3 3
//3 3 3 2
//answer:
//41
//DRRURDDLLLDRRR
//
//2 4
//2 3 3 3
//3 3 3 2
//answer:
//19
//DRRURD
//
//2 4
//2 1 3 3
//3 3 3 2
//answer:
//19
//DRRURD
//
//2 4
//2 3 3 1
//3 3 3 2
//answer:
//19
//DRURDR
//
//2 4
//2 3 3 3
//1 3 3 2
//answer:
//19
//RDRURD
//
//2 4
//2 3 3 3
//3 3 1 2
//answer:
//19
//DRURRD
//


//思路 如果n或m是奇数的话 肯定可以走完
//如果 都是偶数的话寻找值最小的点
//行遍历RRDLLD 绕过最小点DRRURDD
//黑白块,i+j的奇偶性

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100;
int main(){
    int maps[maxn][maxn];
    int sum;
    int n, m, x , y ;
    while(scanf("%d%d", &n,&m) != EOF){
        sum = 0;
        x = 1;  y = 2;
        memset(maps, 100000, sizeof(maps));
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                scanf("%d", &maps[i][j]);
                sum += maps[i][j];
                if((i+j)%2&&maps[i][j] < maps[x][y]){
                    x = i; y = j;
                }
            }
        }
        if(n%2||m%2){
            printf("%d\n", sum);
            if(n%2){
              for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m-1; j++){
                    if(i&1) printf("R");
                    else printf("L");
                }
                if(i==n)  printf("\n");
                else printf("D");
              }
            }
            else if(m%2){
              for(int i = 1; i <= m; i++){
                for(int j = 1; j <= n-1; j++){
                    if(i&1) printf("D");
                    else printf("U");
                }
                  if(i==m) printf("\n");
                  else printf("R");
             }
            }
        }
        else {
            printf("%d\n", sum - maps[x][y]);
            if(x%2){
              for(int i = 1; i < x; i++){
                for(int j = 1; j <= m - 1 ; j++){
                        if(i%2) printf("R");
                        else printf("L");
                }
                printf("D");
              }
              for(int i = x, j = 1; j < m ; ){
                  if((i+j)%2==0 && j>=y){
                     printf("R");
                     j++;
                    }
                  else if((i+j)%2 && j > y){
                      if(x==i){
                         printf("D");
                         i++;
                       }
                        else {
                          printf("U");
                           i--;
                        }
                    }
                   else if((i+j)%2 && j < y){
                             printf("R");
                             j++;
                          }
                   else if((i+j)%2==0 && j < y){
                        if(x==i){
                            printf("D");
                                i++;
                        }
                        else {
                            printf("U");
                            i--;
                        }
                    }
                    if(i==x+1&&j==m&&x+1!=n){ printf("D");}
                    else if(i == x&&j==m&&x+1!=n) {printf("DD");}
                    else if(i == x&&j==m&&x+1==n) printf("D");
              }
              for(int i = x + 2; i <= n; i++){
                for(int j = 1; j <= m-1; j++){
                    if(i%2) printf("L");
                    else printf("R");
                }
                if(i!=n)printf("D");
              }
            }
            else {
                for(int i = 1; i < x-1; i++){
                    for(int j = 1; j <= m-1; j++){
                        if(i%2) printf("R");
                        else printf("L");
                    }
                    printf("D");
                }
                for(int i = x - 1, j = 1; j < m ;){
                      if(j > y && (i+j)%2){
                            if(x-1==i){
                                printf("D");
                                i++;
                            }
                            else {
                                printf("U");
                                i--;
                            }
                     }
                    else if(j>=y&&(i+j)%2==0){
                         printf("R");
                         j++;
                     }

                    else if((i+j)%2==0&&j<y){
                         if(x==i) {
                           printf("U");
                           i--;
                        }
                         else {
                            printf("D");
                             i++;
                        }
                    }
                    else if((i+j)%2 && j < y){
                          printf("R");
                          j++;
                    }

                    if(i == x&&j==m&&x!=n) printf("D");
                    else if(i == x-1&&j==m&&x!=n) printf("DD");
                    else if(i == x-1&&j==m&&x==n) printf("D");
                }
               for(int i = x + 1 ; i <= n; i++){
                  for(int j = 1; j <= m-1; j++){
                     if(i%2) printf("L");
                      else printf("R");
                  }
                  if(i!=n)printf("D");
              }

            }
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(C++,ACM,HDU)