hdu 5402 Travelling Salesman Problem(模拟,棋盘染色问题)

来源:互联网 发布:巴基斯坦取消 知乎 编辑:程序博客网 时间:2024/05/23 23:11

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1404    Accepted Submission(s): 500
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 32 3 33 3 33 3 2
 

Sample Output
25RRDLLDRR
题意:从左上角走到右下角每个点只能走一次最大经过权值是多少

思路:我的构思过程大概是这样的... BFS->DFS>记忆化搜索->最长路->搜题解...

没想到居然是道模拟题,而且用到了类似博弈的东西啊,棋盘染色。

我们知道,当n或者m为奇数的时候,可以遍历完所有点。

当n和m都为偶数时,我们需要策略才能解决问题。

首先我们将棋盘上(i+j)%2==1的格子染成白色,其余的为黑色。

那么有一个我也不知道怎么推出来的总之在纸上模拟几次就是那么回事的规律就出来了

n和m都为偶数时我们无法遍历到所有的点,但是我们一定可以不要一个点从而遍历到其他所有点

并且这个点必须是白点才可以,那么我们只要不取权值最小的白点即可得到答案。注意:如果不取黑点,必然导致至少一个白点无法取到,所以不用考虑不取黑点。

纸上模拟一遍即可知,只有不取白点的时候,我们才有可能通过拐弯取到其他所有点。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 110#define INF 1000000int ma[N][N];int main(){    int n,m;    while(~scanf("%d %d",&n,&m))    {        int sum=0,minn=INF,x,y;        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)            {                scanf("%d",&ma[i][j]);                sum+=ma[i][j];                if((i+j)%2&&minn>ma[i][j])                {                    minn=ma[i][j];                    x=i,y=j;                }            }        if(n%2)        {            printf("%d\n",sum);            for(int i=1; i<=n; i++)            {                for(int j=2; j<=m; j++)                {                    if(i%2) printf("R");                    else printf("L");                }                if(i!=n) printf("D");                else printf("\n");            }        }        else if(m%2)        {            printf("%d\n",sum);            for(int i=1; i<=m; i++)            {                for(int j=2; j<=n; j++)                {                    if(i%2) printf("D");                    else printf("U");                }                if(i!=m) printf("R");                else printf("\n");            }        }        else        {            printf("%d\n",sum-minn);            if(x%2==1)            {                for(int i=1;i<x;i++)                {                    for(int j=2;j<=m;j++)                    {                        if(i%2) printf("R");                        else printf("L");                    }                    printf("D");                }                for(int i=1; i<y; i++)                {                    if(i%2) printf("DR");                    else printf("UR");                }                if(y!=m)                {                    printf("R");                    for(int i=y+1; i<=m; i++)                    {                        if(i%2) printf("U");                        else printf("D");                        if(i!=m) printf("R");                    }                }                for(int i=x+2; i<=n; i++)                {                    printf("D");                    for(int j=2; j<=m; j++)                    {                        if(i%2) printf("L");                        else printf("R");                    }                }                printf("\n");            }            else if(x%2==0)            {                for(int i=1; i<=x-2; i++)                {                    for(int j=2; j<=m; j++)                    {                        if(i%2) printf("R");                        else printf("L");                    }                    printf("D");                }                for(int i=1; i<y; i++)                {                    if(i%2) printf("DR");                    else printf("UR");                }                printf("R");                for(int i=y+1; i<=m; i++)                {                    if(i%2) printf("U");                    else printf("D");                    if(i!=m) printf("R");                }                if(x!=n) printf("D");                for(int i=x+1; i<=n; i++)                {                    for(int j=2; j<=m; j++)                    {                        if(i%2) printf("L");                        else printf("R");                    }                    if(i!=n) printf("D");                }                printf("\n");            }        }    }    return 0;}



0 0