码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5402 Travelling Salesman Problem 模拟

时间:2017-05-24 18:33:15      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:rac   ora   ini   ack   end   script   strong   get   ott   


行数或列数为奇数就能够所有走完.

行数和列数都是偶数,能够选择空出一个(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


Problem Description
Teacher Mai is in a maze with  rows and  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 . 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 .

In following  lines, each line contains  numbers. The -th number in the -th line means the number in the cell . 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 , "L" means you walk to cell , "R" means you walk to cell , "U" means you walk to cell , "D" means you walk to cell .
 

Sample Input
3 3 2 3 3 3 3 3 3 3 2
 

Sample Output
25 RRDLLDRR
 

Author
xudyh
 

Source
 

/* ***********************************************
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 模拟

标签:rac   ora   ini   ack   end   script   strong   get   ott   

原文地址:http://www.cnblogs.com/zhchoutai/p/6900082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!