hdu5402 模拟

D - Travelling Salesman Problem
Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 5402
Appoint description:  System Crawler  (Sep 1, 2016 9:32:06 AM)

Description

Teacher Mai is in a maze with  rows and  columns. There is a non-negative number in each cell. Teacher Mai wants to walk from the top left corner  to the bottom right corner  . 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 

In following  lines, each line contains  numbers. The  -th number in the  -th line means the number in the cell  . Every number in the cell is not more than 

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  , "L" means you walk to cell  , "R" means you walk to cell  , "U" means you walk to cell  , "D" means you walk to cell 

Sample Input

3 3
2 3 3
3 3 3
3 3 2

Sample Output

25

RRDLLDRR

题意:有一个n*m的矩阵,每个格子一个正整数,要从(1,1)->(n,m),每一个格子只能走一次使得路径和最大,输出和,并且输出路径

应该说是一个模拟,因为都是正数,所以尽可能走遍所有格子,当n 或m 有一个奇数时,就可以走遍所有格子,当n,m,全是偶数时,不可能所有格子都到达,最多走n*m-1个格子,我们选择一个数字最小的格子,作为不走的格子,但是这个格子必须是横纵坐标和必须是奇数,画一画找找规律嘛,然后找到后,那么这个数所在的一行以及它的上面或下面一行单独处理,当它在奇数行时,单独处理他这行和他下面那行,如果是偶数单独处理它上面以及它这行,一位要保证单独处理的两行的上面是偶数行,下面也是偶数行,那么处理完这些特殊行,剩余的可以直接打了

ac代码

#include 
#include 
#include 
#include 
using namespace std;
const int inf=0x3f3f3f3f;
int a[104][105];
int main()
{
    int n,m;
    int x,y,k,ny;
    int min_num,sum;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        sum=0;
        min_num=inf;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&a[i][j]);
                sum+=a[i][j];
                 if((i+j)%2)
                 {
                    if(a[i][j]



你可能感兴趣的:(-------模拟)