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中有奇数则可以全部走完。否则需要在(i+j-2)%2 == 1的点中选择一个最小值绕过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MAXN 300005
#define MIN 0
#define MAX 1000001 int main()
{
int n,m;
ll sum;
char ch;
int x;
//freopen("1007.txt","r",stdin);
while(scanf("%d%d",&n,&m) != EOF)
{
sum = 0;
int minx = 100000;
int mini=-1,minj=-1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d",&x);
sum += x;
if((i+j-2)%2)
{
if(x < minx)
{
minx = x;
mini = i;
minj = j;
}
}
}
if(n % 2 || m % 2)
{
printf("%I64d\n",sum);
if(n % 2)
{
for(int k = 1; k <= n; k++)
{
if(k % 2)
ch = 'R';
else
ch = 'L';
for(int i = 1; i <= m-1; i++)
printf("%c",ch);
if(k != n)
printf("D");
} }
else
{
for(int k = 1; k <= m; k++)
{
if(k % 2)
ch = 'D';
else
ch = 'U';
for(int i = 1; i <= n-1; i++)
printf("%c",ch);
if(k != m)
printf("R");
}
}
}
else
{
printf("%I64d\n",sum-minx);
int k=1;
while(1)
{
if(mini%2&&k==mini) break;
if(mini%2==0&&(k+1)==mini) break;
for(int i=2; i<=m; i++)
if(k%2) printf("R");
else printf("L");
printf("D");
k++;
}
if(mini%2)
{
int cx=k;
int cy=1;
while((cy+1)!=minj)
{
printf("DRUR");
cy+=2;
}
printf("DR");
cx++;
cy++;
while(cy!=m)
{
printf("RURD");
cy+=2;
}
k+=2;
}
else
{
int cx=k;
int cy=1;
while(cy!=minj)
{
printf("DRUR");
cy+=2;
}
printf("RD");
cx++;
cy++;
while(cy!=m)
{
printf("RURD");
cy+=2;
}
k+=2;
}
for(int i=k; i<=n; i++)
{
printf("D");
for(int j=2; j<=m; j++)
if(i%2) printf("L");
else printf("R");
}
}
printf("\n");
}
return 0;
}

  

最新文章

  1. 如何选择PHP框架?
  2. 微信学习总结 09 解析接口中的消息创建时间CreateTime
  3. H3C汇聚层交换机认证在线人数展示系统之需求说明和功能点说明
  4. javascript --- 子对象访问父对象的方式
  5. C++纯虚函数
  6. c语言开源项目--SQLite学习资料总结
  7. WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
  8. Linux Wine with *.bat *.exe ( Photoshop and etc.. )
  9. 怎样判断iOS App是通过哪种途径启动的?
  10. Swift 包管理器命令行使用
  11. 关于Android App开发技术分类的一个小总结
  12. Python——字符串2.0(实验)
  13. Web API中如何获取相对地址的绝对地址 Server.MapPath
  14. abaqus查询网格相关
  15. Linux 多主机SSH互信操作---noob....
  16. 从池子里的beta看秋香, 个性迥异
  17. Anaconda3配置环境变量
  18. 使用Git来撤销修改
  19. fieldOfView
  20. 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划

热门文章

  1. 如何减小ios安装包大小
  2. 解析与动作联动得SDN数据平面
  3. 《高级软件测试》云平台Jira的配置
  4. VMware虚拟机误删除vmdk文件后如何恢复?
  5. Gson序列化对象如何忽略字段
  6. DDD实战进阶第一波(二):开发一般业务的大健康行业直销系统(搭建支持DDD的轻量级框架一)
  7. Docker加速器(阿里云)
  8. SpringCloud的应用发布(四)vmvare+linux,防火墙和selinux
  9. 解决编写的 html 乱码问题
  10. html&lt;!DOCTYPE&gt;声明标签