Hdu 5402 Travelling Salesman Problem 棋盘黑白染色

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1560    Accepted Submission(s): 581
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
 

Author
xudyh
 

Source



一个矩阵,从左上走到右下,每个格子最多经过一次,问路径上所有数字最大是多少。


显然长宽有一个为偶数时可以有遍历整个棋盘的方案。

当长宽都为偶数时,把棋盘黑白染色,左上和右下染成黑,和黑色相邻的染成白色,再把相邻的染成黑色,直到染完。发现黑格和白格数量永远相等。

这样,最优策略是绕过一个数字最小的白格子。因为路径上黑白交替,起点终点都是黑色,黑格子数量永远比白格子多一格,所以没办法绕过黑格子。


具体的路径怎么走,画画图可以推出来。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=105,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L); 
int a[maxn][maxn];

int main() {
    int n,m;
    char s[4]={'D','R','U','R'};
    int dir[4][2]={{1,0},{0,1},{-1,0},{0,1}}; 
    while (scanf("%d%d",&n,&m)!=EOF) {
        int i,j,sum=0,p=inf,x,y;
        for (i=1;i<=n;i++) {
            for (j=1;j<=m;j++) {
                scanf("%d",&a[i][j]);
                sum+=a[i][j];
                if ((i+j)%2==1&&a[i][j]<p) p=a[i][j],x=i,y=j;
            }
        }
        if (n%2==1) {
            printf("%d\n",sum);
            for (i=1;i<=n;i++) {
                for (j=1;j<m;j++) {
                    if (i%2==1) printf("R"); else printf("L");
                }
                if (i!=n) printf("D");
            }
        } else if (m%2==1) {
            printf("%d\n",sum);
            for (i=1;i<=m;i++) {
                for (j=1;j<n;j++) {
                    if (i%2==1) printf("D"); else printf("U");
                }
                if (i!=m) printf("R");
            }
        } else {
            printf("%d\n",sum-p);
            for (i=1;i<=(x-1)/2*2;i++) {
                for (j=1;j<m;j++) {
                    if (i%2==1) printf("R"); else printf("L");
                }
                printf("D");
            }
            int cnt=0,k=0;j=1;
            while (cnt<2*(m-1)) {
                cnt++;
                if (i+dir[k][0]==x&&j+dir[k][1]==y) {
                    k=(k-1+4)%4;
                    printf("%c",s[k]);
                    i+=dir[k][0];j+=dir[k][1];
                    k++;k%=4;
                } else {
                    printf("%c",s[k]);
                    i+=dir[k][0];j+=dir[k][1];
                    k++;k%=4;
                }
            }
            if (i!=n) printf("D");
            for (i=(x-1)/2*2+3;i<=n;i++) {
                for (j=1;j<m;j++) {
                    if (i%2==0) printf("R"); else printf("L");
                }
                if (i!=n) printf("D");
            }
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值