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.
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.
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).
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
这道题就是找规律暴力,也没有简化成什么。注意有的情况不是可以全部遍历到。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define MAXN 100010
int main()
{
int n,m,a;
while(scanf("%d%d",&n,&m)!=EOF)
{
int sum=0,minn=MAXN,x=0,y=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&a);
sum+=a;
if(minn>a&&(i+j)%2)
{
x=i;
y=j;
minn=a;
}
}
if(n%2==1)
{
printf("%d\n",sum);
for(int i=0;i<n/2;i++)
{
for(int j=0;j<m-1;j++)
printf("R");
printf("D");
for(int j=0;j<m-1;j++)
printf("L");
printf("D");
}
for(int j=0;j<m-1;j++)
printf("R");
}
else if(m%2==1)
{
printf("%d\n",sum);
for(int i=0;i<m/2;i++)
{
for(int j=0;j<n-1;j++)
printf("D");
printf("R");
for(int j=0;j<n-1;j++)
printf("U");
printf("R");
}
for(int j=0;j<n-1;j++)
printf("D");
}
else
{
printf("%d\n",sum - minn);
for(int i=0;i<x/2;i++)
{
for(int j=0;j<m-1;j++)
printf("R");
printf("D");
for(int j=0;j<m-1;j++)
printf("L");
printf("D");
}
for(int j=0;j<y/2;j++)
printf("DRUR");
if(x%2==1)
printf("RD");
else
printf("DR");
for(int j=y/2+1;j<m/2;j++)
printf("RURD");
for(int i=x/2+1;i<n/2;i++)
{
printf("D");
for(int j=0;j<m-1;j++)
printf("L");
printf("D");
for(int j=0;j<m-1;j++)
printf("R");
}
}
puts("");
}
return 0;
}