2015 Multi-University Training Contest 9-1007 Travelling Salesman Problem
2015-08-19 10:01
330 查看
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(1≤n,m≤100,n∗m≥2).
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
3 3
2 3 3
3 3 3
3 3 2
Sample Output
25
RRDLLDRR
这是一道有趣的问题,如何才能从左上到右下使经过路径和最大,每个数字都是正数,那么如果能走全,肯定是走全比较好,所以当n||m有奇数时,可直接构造之,如果均为偶数时,可以发现,我可以绕啊绕的绕过一个点,剩下的都遍历,横纵坐标和偶数的无法只避开这一个点,所以要想绕开这个点,必须要附带至少一个其它点且是可以单独避开的奇点,所以我们只要找到偶点中最小的那个点绕开就好了。这样所有情况都构造出来了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void print(int x,int y,char dir)
{
for(int i=x;i<y;i++)
printf("%c",dir);
}
int f[111][111];
long long ret=0;
int x,y;
int m,n,mi;
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
ret=0;
mi=0x3f3f3f3f;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&f[i][j]);
ret+=f[i][j];
if((i+j)%2==1 && f[i][j]<mi)
{
mi=f[i][j];
x=i;
y=j;
}
}
if(m%2 == 1|| n%2==1)
{
cout<<ret<<'\n';
if(m%2==1)
{
for(int i=1;i<=m;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
if(i!=m)
{
print(i,i+1,'D');
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
if(i!=n)
print(i,i+1,'R');
}
}
}
else
{
cout<<ret-f[x][y]<<'\n';
if(x%2==1)
{
for(int i=1;i<x;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
print(i,i+1,'D');
}
for(int i=1;i<=n;i++)
{
if(i<y)
if(i%2==1)
print(1,2,'D');
else
print(1,2,'U');
else
if(i>y)
{
if(i%2==0)
print(1,2,'D');
else
print(1,2,'U');
}
if(i!=n)
print(1,2,'R');
}
for(int i=x+2;i<=m;i++)
{
print(1,2,'D');
if(i%2==1)
print(1,n,'L');
else
print(1,n,'R');
}
}
else
{
for(int i=1;i<y;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
print(1,2,'R');
}
for(int i=1;i<=m;i++)
{
if(i<x)
{
if(i%2==1)
print(1,2,'R');
else
print(1,2,'L');
}
else
if(i>x)
{
if(i%2==1)
print(1,2,'L');
else
print(1,2,'R');
}
if(i!=m)
print(1,2,'D');
}
for(int i=y+2;i<=n;i++)
{
print(1,2,'R');
if(i%2==1)
print(1,m,'U');
else
print(1,m,'D');
}
}
}
printf("\n");
}
return 0;
}
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(1≤n,m≤100,n∗m≥2).
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
3 3
2 3 3
3 3 3
3 3 2
Sample Output
25
RRDLLDRR
这是一道有趣的问题,如何才能从左上到右下使经过路径和最大,每个数字都是正数,那么如果能走全,肯定是走全比较好,所以当n||m有奇数时,可直接构造之,如果均为偶数时,可以发现,我可以绕啊绕的绕过一个点,剩下的都遍历,横纵坐标和偶数的无法只避开这一个点,所以要想绕开这个点,必须要附带至少一个其它点且是可以单独避开的奇点,所以我们只要找到偶点中最小的那个点绕开就好了。这样所有情况都构造出来了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void print(int x,int y,char dir)
{
for(int i=x;i<y;i++)
printf("%c",dir);
}
int f[111][111];
long long ret=0;
int x,y;
int m,n,mi;
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
ret=0;
mi=0x3f3f3f3f;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&f[i][j]);
ret+=f[i][j];
if((i+j)%2==1 && f[i][j]<mi)
{
mi=f[i][j];
x=i;
y=j;
}
}
if(m%2 == 1|| n%2==1)
{
cout<<ret<<'\n';
if(m%2==1)
{
for(int i=1;i<=m;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
if(i!=m)
{
print(i,i+1,'D');
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
if(i!=n)
print(i,i+1,'R');
}
}
}
else
{
cout<<ret-f[x][y]<<'\n';
if(x%2==1)
{
for(int i=1;i<x;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
print(i,i+1,'D');
}
for(int i=1;i<=n;i++)
{
if(i<y)
if(i%2==1)
print(1,2,'D');
else
print(1,2,'U');
else
if(i>y)
{
if(i%2==0)
print(1,2,'D');
else
print(1,2,'U');
}
if(i!=n)
print(1,2,'R');
}
for(int i=x+2;i<=m;i++)
{
print(1,2,'D');
if(i%2==1)
print(1,n,'L');
else
print(1,n,'R');
}
}
else
{
for(int i=1;i<y;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
print(1,2,'R');
}
for(int i=1;i<=m;i++)
{
if(i<x)
{
if(i%2==1)
print(1,2,'R');
else
print(1,2,'L');
}
else
if(i>x)
{
if(i%2==1)
print(1,2,'L');
else
print(1,2,'R');
}
if(i!=m)
print(1,2,'D');
}
for(int i=y+2;i<=n;i++)
{
print(1,2,'R');
if(i%2==1)
print(1,m,'U');
else
print(1,m,'D');
}
}
}
printf("\n");
}
return 0;
}
相关文章推荐
- Communications link failure Last packet sent to the server was 0 ms ago.问题解决
- HDU 5402 Travelling Salesman Problem (2015 Multi-University Training Contest 9 2015多校联合)
- Aisen新浪微博客户端项目源码
- tools:context=".MainActivity的作用
- HDU 5405 Sometimes Naive 2015多校联合训练赛#9 LCT 树链剖分
- 马尔可夫链及吉布斯抽样 入门详解(Markov Chain Monte Carlo and Gibbs Sampling)
- 再谈应用环境下的TIME_WAIT和CLOSE_WAIT
- 2015 HUAS Summer Trainning #6~I
- Contain Duplicate III*******
- Contain Duplicate II
- offsetof 和 container_of
- Contain Duplicate
- Fair Division
- Unable to find a team with the given Team ID或者Failed to code sign的问题解决
- linuc之waitpid
- POJ 1285 - Combinations, Once Again 泛化背包!
- 2015 Multi-University Training Contest 9 1007
- Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 27057872
- AAAI,ICML,IJCAI,AI,TPAMI,JMLR,DKE,TNNLS,SIGIR,TKDE,KDD
- 费用流 hdu3376 Matrix Again