Petrozavodsk Summer Training Camp 2015 Day 2: Xudyh (TooSimple) Contest, Saturday, August 22, 2015 G...

本文介绍了一种解决旅行商问题的方法,特别针对一个由n行m列构成的迷宫,目的是从左上角走到右下角,同时最大化路径上的数值总和。文中提供了一个C++实现的例子,并详细解释了如何通过不同路径选择来达到目标。

Problem G. Travelling Salesman Problem Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 64 mebibytes 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 First line of the input contains one integer T (1 ≤ T ≤ 130) — number of test cases. For each test case, the first line contains two numbers n and 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). Examples standard input standard output 1 3 3 2 3 3 3 3 3 3 3 2 25 RRDLLDRR

 

题目分析:首先n,m有一个是奇数的时候,是可以遍历掉每一个格子的,全是偶数的之后 期望最好的情况是只留下一个不走。

小范围暴力找可以不走哪些格子,发现画出4*4的只能不走奇偶不同的。

如果全为偶数,考虑奇偶性,找最小的奇偶不同的点,不走。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 const int MAXN = 100+10;
  5 int a[MAXN][MAXN];
  6 
  7 void firstRow(int M)
  8 {
  9     for(int i=0;i<M-1;i++)  printf("R");
 10     printf("D");
 11     for(int i=0;i<M-1;i++)  printf("L");
 12     printf("D");
 13 }
 14 
 15 void lastRow(int M)
 16 {
 17     printf("D");
 18     for(int i=0;i<M-1;i++)  printf("L");
 19     printf("D");
 20     for(int i=0;i<M-1;i++)  printf("R");
 21 }
 22 
 23 void firstCol()
 24 {
 25     printf("D");
 26     printf("R");
 27     printf("U");
 28     printf("R");
 29 }
 30 
 31 void lastCol()
 32 {
 33     printf("R");
 34     printf("U");
 35     printf("R");
 36     printf("D");
 37 }
 38 
 39 void Printf(int M,int N,int m,int n)
 40 {
 41     int i=0,j=0;
 42     while(i<(m/2)*2)    firstRow(N),i+=2;
 43     while(j<(n/2)*2)    firstCol(),j+=2;
 44     if(m%2==0)  printf("DR");
 45     else printf("RD");
 46     j+=2;
 47     while(j<N)  lastCol(),j+=2;
 48     i+=2;
 49     while(i<M)  lastRow(N),i+=2;
 50 }
 51 
 52 void Run()
 53 {
 54     int M,N;
 55     int sum=0;
 56     scanf("%d%d",&M,&N);
 57     for(int i=0;i<M;i++)
 58         for(int j=0;j<N;j++)
 59         {
 60             scanf("%d",&a[i][j]);
 61             sum+=a[i][j];
 62         }
 63     if(M%2==1)
 64     {
 65         printf("%d\n",sum);
 66         for(int i=0;i<M;i++)
 67         {
 68             for(int j=0;j<N-1;j++)
 69             {
 70                 if(i%2==0)  printf("R");
 71                 else printf("L");
 72             }
 73             if(i!=M-1)  printf("D");
 74         }
 75         return;
 76     }
 77     if(N%2==1)
 78     {
 79         printf("%d\n",sum);
 80         for(int j=0;j<N;j++)
 81         {
 82             for(int i=0;i<M-1;i++)
 83             {
 84                 if(j%2==0)  printf("D");
 85                 else printf("U");
 86             }
 87             if(j!=N-1)  printf("R");
 88         }
 89         return;
 90     }
 91 
 92     int Min=10000+1;
 93     int rei,rej;
 94     for(int i=0;i<M;i++)
 95     {
 96         for(int j=0;j<N;j++)
 97         {
 98             if((i%2+j%2)==1)
 99             {
100                 if(Min>a[i][j])
101                 {
102                     Min=a[i][j];rei=i;rej=j;
103                 }
104             }
105         }
106     }
107     printf("%d\n",sum-Min);
108     Printf(M,N,rei,rej);
109 
110 
111 }
112 
113 int main()
114 {
115     int T;
116     scanf("%d",&T);
117     while(T--)
118     {
119         Run();
120         printf("\n");
121     }
122     return 0;
123 }

 

转载于:https://www.cnblogs.com/poler/p/7359627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值