mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点。路径的权就是数的和。输出一条权值最大的路径方案


思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇数。显然能够遍历。要是有一个偶数。能够绘图发现,把图染成二分图后,(1,1)为黑色,总能有一种构造方式能够仅仅绕过不论什么一个白色的点。然后再遍历其它点。而绕过黑色的点必定还要绕过两个白色点才干遍历所有点,这是绘图发现的。所以找一个权值最小的白色点绕过就能够了,

题解给出了证明:

如果n,mn,m都为偶数,那么讲棋盘黑白染色。如果(1,1)(1,1)(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数同样。所以必定有一个白格不会被经过。所以选择白格中权值最小的不经过。




//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i< int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<int,int> pii;

template <class T>
inline bool RD(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ?

0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void PT(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) PT(x / 10); putchar(x % 10 + '0'); } const int N = 123; int mp[N][N]; int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ int sum = 0; memset(mp,0,sizeof(mp)); REP(i,n) REP(j,m) RD(mp[i][j]), sum += mp[i][j]; if( (n&1)||(m&1) ){ PT(sum);puts(""); if( n&1 ){ REP(r,n){ if( r&1 ) REP(i,m-1) putchar('R'); else REP(i,m-1) putchar('L'); if( r != n) putchar('D'); } }else{ REP(c,m){ if( c&1 ) REP(i,n-1) putchar('D'); else REP(i,n-1) putchar('U'); if( c != m) putchar('R'); } } }else{ int minn = 1LL<<30; int sx,sy; REP(x,n) REP(y,m){ if( (x+y)&1 ){ if( mp[x][y] < minn) minn = mp[x][y], sx = x,sy = y; } } printf("%d\n",sum-minn); bool ok = 0; REP(y,m){ if( (y-1)/2+1 == (sy-1)/2+1){ ok = 1; bool rgt = 1; REP(x,n){ if( x == sx) { if( x != n) putchar('D'); continue; } if( rgt) putchar('R'); else putchar('L'); if( x != n) putchar('D'); rgt = !rgt; } y++; }else{ if( ((y&1)&&ok==0) || ((y%2 == 0)&&ok) ){ REP(x,n-1) putchar('D'); }else{ REP(x,n-1) putchar('U'); } } if( y != m) putchar('R'); } } puts(""); } }






Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 864    Accepted Submission(s): 313
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.
 

Input
There are multiple test cases.

For each test case, the first line contains two numbers n,m(1n,m100,nm2).

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,y1), "R" means you walk to cell (x,y+1), "U" means you walk to cell (x1,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
 

Author
xudyh
 

Source
 

Recommend
wange2014

posted on 2017-05-23 09:42  mthoutai  阅读(279)  评论(0编辑  收藏  举报