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 (,) 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(≤n,m≤,n∗m≥). 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 .
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−), "R" means you walk to cell (x,y+), "U" means you walk to cell (x−,y), "D" means you walk to cell (x+,y).
Sample Input
Sample Output
RRDLLDRR
Author
xudyh
Source
真是个鸟题,一开始把情况都考虑了,交上去直接WA了,后来意识到是n、m同为偶数有错。 其实只要找找规律就可以得出找的最小值的横纵坐标的和要为奇数,不过这个的顺序也是很难写啊。。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int mp[][];
int x,y;
int minn;
void get()
{
x = ; y = ;
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
if (((i + j) & ) && mp[x][y] > mp[i][j]) x = i, y = j;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int sum=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
}
}
if((n&))
{ printf("%d\n",sum);
if(n==)
{
for(int i=;i<m;i++)
printf("R");
printf("\n");
continue;
}
for(int i=;i<m;i++)
printf("R");
printf("D");
for(int i=;i<=n;i++)
{
if(i%==)
{
for(int j=;j<m;j++)
printf("L");
printf("D");
}
else
{
if(i!=n)
{
for(int i=;i<m;i++)
printf("R");
printf("D");
}
else
{
for(int i=;i<m;i++)
printf("R");
}
}
}
printf("\n"); }
else if((m%))
{
printf("%d\n",sum);
if(m==)
{
for(int i=;i<n;i++)
printf("D");
printf("\n");
continue;
}
for(int i=;i<n;i++)
printf("D");
printf("R");
for(int i=;i<=m;i++)
{
if(i%==)
{
for(int j=;j<n;j++)
printf("U");
printf("R");
}
else
{
if(i!=m)
{
for(int j=;j<n;j++)
printf("D");
printf("R");
}
else
{
for(int j=;j<n;j++)
printf("D");
}
}
}
printf("\n");
}
else if((m%)== && (n%)==)
{
get();
printf("%d\n", sum - mp[x][y]);
for (int i = ; i <= n; i += )
{
if (x == i || x == i + )
{
for (int j = ; j < y; j++)
{
if (j & ) printf("D"); else printf("U");
printf("R");
}
if (y < m) printf("R");
for (int j = y + ; j <= m; j++)
{
if (j & ) printf("U"); else printf("D");
if (j < m) printf("R");
}
if (i < n - ) printf("D");
}
else if (i < x)
{
for (int j = ; j < m; j++) printf("R");
printf("D");
for (int j = ; j < m; j++) printf("L");
printf("D");
}
else
{
for (int j = ; j < m; j++) printf("L");
printf("D");
for (int j = ; j < m; j++) printf("R");
if (i < n - ) printf("D");
}
}
printf("\n");
} }
return ;
}