hdu 5402 Travelling Salesman Problem(模拟,棋盘染色问题)
来源:互联网 发布:巴基斯坦取消 知乎 编辑:程序博客网 时间:2024/05/23 23:11
Travelling Salesman Problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1404 Accepted Submission(s): 500
Special Judge
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 numbersn,m(1≤n,m≤100,n∗m≥2) .
In followingn 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
In following
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
Sample Input
3 32 3 33 3 33 3 2
Sample Output
25RRDLLDRR
思路:我的构思过程大概是这样的... BFS->DFS>记忆化搜索->最长路->搜题解...
没想到居然是道模拟题,而且用到了类似博弈的东西啊,棋盘染色。
我们知道,当n或者m为奇数的时候,可以遍历完所有点。
当n和m都为偶数时,我们需要策略才能解决问题。
首先我们将棋盘上(i+j)%2==1的格子染成白色,其余的为黑色。
那么有一个我也不知道怎么推出来的总之在纸上模拟几次就是那么回事的规律就出来了
n和m都为偶数时我们无法遍历到所有的点,但是我们一定可以不要一个点从而遍历到其他所有点
并且这个点必须是白点才可以,那么我们只要不取权值最小的白点即可得到答案。注意:如果不取黑点,必然导致至少一个白点无法取到,所以不用考虑不取黑点。
纸上模拟一遍即可知,只有不取白点的时候,我们才有可能通过拐弯取到其他所有点。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 110#define INF 1000000int ma[N][N];int main(){ int n,m; while(~scanf("%d %d",&n,&m)) { int sum=0,minn=INF,x,y; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { scanf("%d",&ma[i][j]); sum+=ma[i][j]; if((i+j)%2&&minn>ma[i][j]) { minn=ma[i][j]; x=i,y=j; } } if(n%2) { printf("%d\n",sum); for(int i=1; i<=n; i++) { for(int j=2; j<=m; j++) { if(i%2) printf("R"); else printf("L"); } if(i!=n) printf("D"); else printf("\n"); } } else if(m%2) { printf("%d\n",sum); for(int i=1; i<=m; i++) { for(int j=2; j<=n; j++) { if(i%2) printf("D"); else printf("U"); } if(i!=m) printf("R"); else printf("\n"); } } else { printf("%d\n",sum-minn); if(x%2==1) { for(int i=1;i<x;i++) { for(int j=2;j<=m;j++) { if(i%2) printf("R"); else printf("L"); } printf("D"); } for(int i=1; i<y; i++) { if(i%2) printf("DR"); else printf("UR"); } if(y!=m) { printf("R"); for(int i=y+1; i<=m; i++) { if(i%2) printf("U"); else printf("D"); if(i!=m) printf("R"); } } for(int i=x+2; i<=n; i++) { printf("D"); for(int j=2; j<=m; j++) { if(i%2) printf("L"); else printf("R"); } } printf("\n"); } else if(x%2==0) { for(int i=1; i<=x-2; i++) { for(int j=2; j<=m; j++) { if(i%2) printf("R"); else printf("L"); } printf("D"); } for(int i=1; i<y; i++) { if(i%2) printf("DR"); else printf("UR"); } printf("R"); for(int i=y+1; i<=m; i++) { if(i%2) printf("U"); else printf("D"); if(i!=m) printf("R"); } if(x!=n) printf("D"); for(int i=x+1; i<=n; i++) { for(int j=2; j<=m; j++) { if(i%2) printf("L"); else printf("R"); } if(i!=n) printf("D"); } printf("\n"); } } } return 0;}
0 0
- hdu 5402 Travelling Salesman Problem(模拟,棋盘染色问题)
- HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
- hdu 5402 Travelling Salesman Problem(构造+棋盘黑白染色)
- hdu5402 Travelling Salesman Problem(棋盘染色+模拟)
- Hdu 5402 Travelling Salesman Problem 棋盘黑白染色
- Travelling Salesman Problem (hdu 5402 模拟)
- HDU-5402 Travelling Salesman Problem(模拟)
- HDU 5402 Travelling Salesman Problem(棋盘黑白染色)——多校练习9
- 2015多校第九场 HDU 5402 Travelling Salesman Problem 棋盘染色法,构造
- hdu 5402 Travelling Salesman Problem 模拟构造
- hdu 5402 Travelling Salesman Problem(构造+模拟)
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
- hdu(5402)——Travelling Salesman Problem(模拟题)
- HDU 5402 Travelling Salesman Problem (MUT#9 暴力模拟)
- 规律,模拟,贪心(Travelling Salesman Problem,HDU 5402)
- 【HDU 5402】Travelling Salesman Problem(构造)
- hdu 5402 Travelling Salesman Problem(构造)
- HDU 5402 Travelling Salesman Problem
- Ubuntu 禁更新某些package的方法
- Java设计模式其一【单例设计模式】
- 操作系统ucore lab1实验报告
- No space left on device
- ios学习路线—iOS高级(UDP)
- hdu 5402 Travelling Salesman Problem(模拟,棋盘染色问题)
- 操作系统ucore lab2实验报告
- 试着解析以下概念dBx: dBm,dBw,dBi,dBd,dB,dBc
- 拼图响应式前端框架版响应式后台
- 加解密算法总结(RSA)
- 对于解析txt 格式的的文件BufferedReader
- JQuery实战中体验学习的快乐
- 第十六周--用文件保存的学生名单
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)