1. 程式人生 > >hdu5402:Travelling Salesman Problem

hdu5402:Travelling Salesman Problem

.... pat man 。。 scan oos 沒有 columns ext

 Travelling Salesman Problem

2017-09-01


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,n?m2).
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).

Sample Input

INPUT_1 3 3 2 3 3 3 3 3 3 3 2 INPUT_2 2 2 1 0 1 1 INPUT_3 4 4 6 0 6 6 6 6 6 6 6 6 6 6 6 6 6 6

Sample Output

OUT_1 25 RRDLLDRR OUT_2 3 DR OUT_3 90 DRRURDDLLLDRRR DDDRUURURDDLDR皆可。。 spj.....

此題是一個好大的模擬,出題人你節操被10萬買走了吧 題意:給你一個大矩陣,一筆從(1,1)走到(n,m)路徑上最大的和. 看出來結論:如果n||m有一個是奇數,那麽就可以走所有的點,怎麽走看心情 n和m全為偶數就找到一個i,j(1<=i<n;1<=j<=m)&&(i+j)是奇數的點,是繞過這個看不順的點走到(n,m) 路程全部模擬; 要不是掉rating我才不幹呢,什麽破玩意 技術分享
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read(){
    char ch=getchar();
    int f=1,an=0;
    while(!(0<=ch&&ch<=9)){if(ch==-)f=-f;ch=getchar();}
    while(0<=ch&&ch<=9){an=an*10+(ch-0);ch=getchar();}
    return an*f;
}
int n,m,sum;
int x,y,z;
int a[100+99][100+99];
void P1(int n,int m){
    cout<<sum<<endl;
    for(int i=1;i<=n;i++){
        if(i&1){
            for(int j=1;j<m;j++)cout<<"R";if(i!=n)cout<<"D";}
        else {
            for(int j=1;j<m;j++)cout<<"L";if(i!=n)cout<<"D";}
    }
    cout<<endl;
}
void P2(int n,int m){
    cout<<sum<<endl;
    for(int i=1;i<=m;i++){
        if(i&1){
            for(int j=1;j<n;j++)cout<<"D";if(i!=m)cout<<"R";}
        else{
            for(int j=1;j<n;j++)cout<<"U";if(i!=m)cout<<"R";}
    }
    cout<<endl;
}
void P5(int a,int b,int c,int d){
    int e=a;
    e=(e-1)>>1;
    for(int i=1;i<=e;i++){
        for(int j=1;j<d;j++)cout<<"R";cout<<"D";
        for(int j=1;j<d;j++)cout<<"L";cout<<"D";}
    int k=1,z=1;bool flag=1;
    while(z<d){
        if(z==b){cout<<"R";z++;}
        else{
            if(flag){cout<<"DR";flag^=1;z++;}
            else {cout<<"UR";flag^=1;z++;}
        }
    }
    if(b!=m)cout<<"D";
    c>>=1;c-=e;
    for(int i=2;i<=c;i++){
        if(i==2)cout<<"D";
        for(int j=1;j<d;j++)cout<<"L";cout<<"D";
        for(int j=1;j<d;j++)cout<<"R";if(i!=c)cout<<"D";}
}
void P3(int n,int m){
    cout<<sum-a[x][y]<<endl;
    P5(x,y,n,m);
    cout<<endl;
}
void G(){sum=0;n=m=0;}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        z=9999999;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){a[i][j]=read();sum+=a[i][j];
            if((i+j)&1)if(z>a[i][j]){z=a[i][j];x=i;y=j;}
        }
        if(n&1)P1(n,m);
        else if(m&1)P2(n,m);
        else P3(n,m);
        G();
    }
    return 0;
}
有節操模擬

by:s_a_b_e_r


什麽破玩意+1 題意見上邊(實力甩鍋 n或者m任意一個是奇數的話就一筆畫走完這個矩形 兩個都是偶數的話就麻煩了x 要在矩陣上去掉一個點 但是不是所有點都能去 4x4的點圖如下

A X C X
X C X C
C X C X
X C X B

其中X是可以去掉的點,C是不能去掉的點 關於怎麽去點……這個做法很多啊w 表示可以每次掃兩行 兩行內都沒有要去掉的點的話就直接走直線 有的話就扭過去x 大概這樣 → → ↑ ↓→↑ …… ……我自己都看不懂了x ……沒寫完下午再補

hdu5402:Travelling Salesman Problem