HDU 5402 Travelling Salesman problem (mut#9 violence simulation)

Source: Internet
Author: User

"Topic link" click here~~

"The main idea": Walk the Square, from "" to "n,m", the middle process to get the number and maximum, and the output path

"Ideas":

If there is an odd number in N and M, then all of them will be all right. Otherwise, to find a minimum point, do not, the coordinates of this point to meet x+y is odd if not, discard the point will certainly lead to another point also can not go. Then find the point where the brute force output path can be.

Code:

#include <bits/stdc++.h>using namespace Std;const int n=105;typedef long long LL; LL Mat[n][n]; LL n,m;    LL x,y;void getmin () {///In the case of n,m having an even number, it is necessary to discard a point and find the point with the lowest weight (the sum of coordinates must be odd) x=1,y=2;                for (int i=1, i<=n; ++i) for (int j=1; j<=m; ++j) {if (mat[i][j]<mat[x][y]&& (i+j) &1) {                X=i;            Y=j;        }}}int Main () {while (~scanf ("%i64d%i64d", &n,&m)) {memset (mat,0,sizeof (MAT));        LL sum=0;                for (int. I=1; i<=n; ++i) {for (int j=1; j<=m; ++j) {scanf ("%i64d", &mat[i][j]);   SUM+=MAT[I][J];        Statistics and} LL ans; if (n&1| |            M&AMP;1)////If there is an odd number, the squares can all go through, {printf ("%i64d\n", sum); if (n&1| | (n&1&&m&1)) {for (int i=0, i<n; ++i) {for (int j=1; j<m; ++j) {if (i&                        1) printf ("L");  else printf ("R");                  } if (i!=n-1) printf ("D");            } puts (""); } else if (m&1&&n%2==0) {for (Int. i=0; i<m; ++i) {for (int j=1; j <n;                        ++J) {if (i&1) printf ("U");                    else printf ("D");                } if (i!=m-1) printf ("R");            } puts ("");       }} else{///Even condition getmin ();            The coordinates of the point at which the minimum weight is obtained ans=sum-mat[x][y];            printf ("%i64d\n", ans); for (int i = 1; I <= n; i + = 2) {if (x = = I | | x = = i + 1) {///the path for the point is processed for (int j = 1 ; J < y;                        J + +) {if (J & 1) printf ("D"); else printf ("U");                    printf ("R");                    } if (y<m) printf ("R");             for (int j = y + 1; j <= M; j + +) {           if (J & 1) printf ("U");                        else printf ("D");                    if (J < m) printf ("R");                } if (I < n-1) printf ("D");                    } else if (I < x) {///the path before the point for (int j = 1; j < M; j + +) printf ("R");                    printf ("D");                    for (int j = 1; j < M; j + +) printf ("L");                printf ("D");                    } else {///the path after the point for (int j = 1; j < M; j + +) printf ("L");                    printf ("D");                    for (int j = 1; j < M; j + +) printf ("R");                if (i < n-1) printf ("D");        }} puts (""); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5402 Travelling Salesman problem (mut#9 violence simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.