HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完.
行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点.
假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点
Travelling Salesman Problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 747 Accepted Submission(s): 272
Special Judge
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.
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.
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).
3 3 2 3 3 3 3 3 3 3 2
25 RRDLLDRR
- /* ***********************************************
- Author :CKboss
- Created Time :2015年08月19日 星期三 13时43分44秒
- File Name :HDOJ5402.cpp
- ************************************************ */
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <cstdlib>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- int n,m;
- int g[110][110];
- char dir[110][110];
- char loop_down[4]={'R','D','L','D'};
- char loop_up[4]={'R','U','L','U'};
- void R(int &x,int &y) { y+=1; }
- void L(int &x,int &y) { y-=1; }
- void U(int &x,int &y) { x-=1; }
- void D(int &x,int &y) { x+=1; }
- string road;
- string UP_TO_DOWN(int x,int y)
- {
- string midroad="";
- memset(dir,'.',sizeof(dir));
- dir[x][y]='$';
- int curx=1,cury=1;
- for(int i=1,id=0;i<2*n;i++,id++)
- {
- int nx=curx,ny=cury;
- if(loop_down[id%4]=='R') R(nx,ny);
- else if(loop_down[id%4]=='L') L(nx,ny);
- else if(loop_down[id%4]=='U') U(nx,ny);
- else if(loop_down[id%4]=='D') D(nx,ny);
- if(dir[nx][ny]=='.')
- {
- dir[curx][cury]=loop_down[id%4];
- midroad+=dir[curx][cury];
- curx=nx; cury=ny;
- }
- else if(dir[nx][ny]=='$')
- {
- dir[curx][cury]='D';
- midroad+=dir[curx][cury];
- D(curx,cury);
- id=3;
- }
- }
- midroad[midroad.length()-1]='R';
- return midroad;
- }
- string DOWN_TO_UP(int x,int y)
- {
- string midroad="";
- memset(dir,'.',sizeof(dir));
- dir[x][y]='$';
- int curx=n,cury=1;
- for(int i=1,id=0;i<2*n;i++,id++)
- {
- int nx=curx,ny=cury;
- if(loop_up[id%4]=='R') R(nx,ny);
- else if(loop_up[id%4]=='L') L(nx,ny);
- else if(loop_up[id%4]=='U') U(nx,ny);
- else if(loop_up[id%4]=='D') D(nx,ny);
- if(dir[nx][ny]=='.')
- {
- dir[curx][cury]=loop_up[id%4];
- midroad+=dir[curx][cury];
- curx=nx; cury=ny;
- }
- else if(dir[nx][ny]=='$')
- {
- dir[curx][cury]='U';
- midroad+=dir[curx][cury];
- U(curx,cury);
- id=3;
- }
- }
- midroad[midroad.length()-1]='R';
- return midroad;
- }
- void SHOW(int x,int y)
- {
- road="";
- memset(dir,'.',sizeof(dir));
- dir[x][y]='$';
- if(y==1)
- {
- /// S road
- int curx=1,cury=1,id=0;
- for(int i=0;i<2*n-1;i++,id++)
- {
- int nx=curx,ny=cury;
- if(loop_down[id%4]=='R') R(nx,ny);
- else if(loop_down[id%4]=='L') L(nx,ny);
- else if(loop_down[id%4]=='U') U(nx,ny);
- else if(loop_down[id%4]=='D') D(nx,ny);
- if(dir[nx][ny]=='.')
- {
- dir[curx][cury]=loop_down[id%4];
- road+=dir[curx][cury];
- curx=nx; cury=ny;
- }
- else if(dir[nx][ny]=='$')
- {
- if(nx==n)
- {
- dir[curx][cury]='L';
- road+=dir[curx][cury];
- L(curx,cury);
- }
- else
- {
- dir[curx][cury]='D';
- road+=dir[curx][cury];
- D(curx,cury);
- id=1;
- }
- }
- }
- road[road.length()-1]='R';
- for(int i=3;i<=m;i++)
- {
- for(int j=1;j<n;j++)
- {
- if(i%2==0) road+='D';
- else road+='U';
- }
- road+='R';
- }
- }
- else
- {
- for(int i=1;i<y-1;i++)
- {
- for(int j=1;j<n;j++)
- {
- if(i%2==1) road+='D';
- else road+='U';
- }
- road+='R';
- }
- if(y%2==0)
- {
- /// from up to down
- road+=UP_TO_DOWN(x,2);
- for(int i=y+1,id=0;i<=m;i++,id++)
- {
- for(int j=1;j<n;j++)
- {
- if(id%2==0) road+='U';
- else road+='D';
- }
- road+='R';
- }
- }
- else if(y&1)
- {
- /// from down to up
- road+=DOWN_TO_UP(x,2);
- for(int i=y+1,id=0;i<=m;i++,id++)
- {
- for(int j=1;j<n;j++)
- {
- if(id%2==0) road+='D';
- else road+='U';
- }
- road+='R';
- }
- }
- }
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- int sum=0;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- scanf("%d",&g[i][j]);
- sum+=g[i][j];
- }
- }
- if(n&1)
- {
- printf("%d\n",sum);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<m;j++)
- {
- if(i&1) putchar('R');
- else putchar('L');
- }
- if(i!=n) putchar('D');
- }
- putchar(10);
- }
- else if(m&1)
- {
- printf("%d\n",sum);
- for(int i=1;i<=m;i++)
- {
- for(int j=1;j<n;j++)
- {
- if(i&1) putchar('D');
- else putchar('U');
- }
- if(i!=m) putchar('R');
- }
- putchar(10);
- }
- else
- {
- int mi=999999999;
- int px,py;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- if((i+j)%2)
- {
- if(mi>g[i][j])
- {
- mi=min(mi,g[i][j]);
- px=i; py=j;
- }
- }
- }
- }
- printf("%d\n",sum-mi);
- SHOW(px,py);
- road[road.length()-1]=0;
- cout<<road<<endl;
- }
- }
- return 0;
- }
HDOJ 5402 Travelling Salesman Problem 模拟的更多相关文章
- hdoj 5402 Travelling Salesman Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 类似于黑白棋盘,有的格子是可以不走的,有的格子是不能不走的,对于m或n中有一个奇数的情况, 所有 ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...
- hdu 5402 Travelling Salesman Problem (技巧,未写完)
题意:给一个n*m的矩阵,每个格子中有一个数字,每个格子仅可以走一次,问从(1,1)走到(n,m) 的路径点权之和. 思路: 想了挺久,就是有个问题不能短时间证明,所以不敢下手. 显然只要n和m其中一 ...
- HDU 5402 : Travelling Salesman Problem
题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...
随机推荐
- SQL 几个查看性能的语句
1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( ...
- 6.12---bug
- 计算机二级C语言冲刺笔记。
2018-03-0618:32:26 风萧萧兮易水寒,壮士一去...... 四级依旧没过,计算机二级接踵而至, default语句在switch语句中可以省略,所以B错误:switch语句中并非每个c ...
- 百度人脸识别AI实践.doc
0, 前言 百度开放了很多AI能力,其中人脸识别就是其中之一. 本文对百度人脸识别AI进行实践检验,看看其使用效果如何. 鉴于是最为基础的实践,基本都是在其接口范例代码修改而来. 百度人脸识别AI网站 ...
- Windows sever 2003 IIS6.0 搭建DVWA
DVWA 环境: Windows Sever 2003 IIS 6.0+MYSQL+PHP5.4+FASFCGI 详细教程: http://files.cnblogs.com/files/yyx001 ...
- Spring框架系列(九)--MyBatis面试题(转载)
1.什么是Mybatis? 1.Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动.创建 连接.创建statement ...
- ThinkPHP---thinkphp完善站内信功能
[一]收件箱 分析 控制器:EmailController.class.php 方法:recBox(全称receive box收件箱) 模板文件:recBox.html 分步操作: 第一步:创建方法r ...
- 社交网络图中结点的“重要性”计算 (30 分) C++解法
社交网络图中结点的"重要性"计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓 ...
- 服务器做ssh免秘钥登陆
集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...
- 虚拟机上CentOS-6.9-x86_64系统安装教程
最近想学学Linux系统如何使用,于是想用VM安装虚拟机学习一下. linux系统比较多,我这里用的是CentOS-6.9-x86_64 一.下载系统 下载地址:https://www.centos. ...