1. 程式人生 > >動態規劃入門(二)DP 基本思想 具體實現 經典題目 POJ1088

動態規劃入門(二)DP 基本思想 具體實現 經典題目 POJ1088

(一) POJ1088,動態規劃的入門級題目。嘿嘿,連題目描述都是難得一見的中文。

題目分析

求最長的滑雪路徑,關鍵是確定起點,即從哪開始滑。

不妨設以( i, j )為起點,現在求滑行的最長路徑。

首先,( i, j )能滑向的無非就是它四周比它低的點。到底滑向哪個點?很簡單,誰長滑行誰。假設(i, j )--->( i, j+1 ), 現在就變成了:以( i, j+1 )為起點,求最長滑行路徑的問題。這樣一直下去,直到某個區域性最低點,就算滑行結束了。


狀態轉換方程

dp( i,j ) = Max( dp( i-1, j ), dp( i, j+1 ),dp( i+1, j ), dp( i, j-1 ) ) + 1;

其中:dp( i,j )表示以( i, j )為起點,所能滑行的最長長度 

程式設計實現

列舉起點,找到最長的滑行路徑。因為涉及到上下左右的點,所以注意邊界情況的處理。還是記憶化遞迴來的簡便,直接把Invalid的情況給剪掉。

下面是程式碼:

/*
 * Michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。
 Michael想知道載一個區域中最長底滑坡。區域由一個二維陣列給出。陣列的每個數字代表點的高度。
 下面是一個例子
 1  2  3 4 5 
 16 17 18 19 6
 15 24 25 20 7
 14 23 22 21 8 
 13 12 11 10 9 
 一個人可以從某個點滑向上下左右相鄰四個點之一,當且僅當高度減小。在上面的例子中,一條可滑行的滑坡為24-17-16-1。當然25-24-23-...-3-2-1更長。事實上,這是最長的一條。
 */
import java.util.Scanner;

public class DP
{

	private int row;// 行數
	private int col;// 列數
	private int[][] height; // 存放各個點的高度
	private int maxLength = 0;
	private int[][] flag; // 存放從各個點開始的最長長度 ,主要實現動態規劃,不再重複計算同樣點的最大長度
	private int[] path = new int[1000];// 儲存最長路徑上的點

	public DP(int i, int j)
	{
		// 建構函式
		this.row = i;
		this.col = j;
		height = new int[row][col];
		flag = new int[row][col];
		create();
	}

	// 給二維陣列賦值
	public void create()
	{
		Scanner scan = new Scanner(System.in);
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				height[i][j] = (int) scan.nextInt();
				// 初始化各個點的高度值
				flag[i][j] = -1;
				// 初始化flag陣列
			}
		}
	}

	public int max_Len(int i, int j)
	{
		// 遞迴求最大值
		int max = 0;
		int[] temp =
		{ 1, 1, 1, 1 };
		// temp存放點height[i][j]的上下左右四個方向的最大長度
		if (flag[i][j] != -1)
		{
			return flag[i][j];
		} else
		{
			if (j > 0 && height[i][j - 1] <= height[i][j])// 當可以向左滑動時
			{
				// 當可以向左滑動時 temp[0]=max[i][j-1]+1,否則temp[0]=0;下面類似
				temp[0] = max_Len(i, j - 1) + 1;
			}
			if (j < col - 1 && height[i][j + 1] <= height[i][j])// 向右
			{
				temp[1] = max_Len(i, j + 1) + 1;
			}
			if (i > 0 && height[i - 1][j] <= height[i][j]) // 向上
			{
				temp[2] = max_Len(i - 1, j) + 1;
			}
			if (i < row - 1 && height[i + 1][j] <= height[i][j])// 向下
			{
				temp[3] = max_Len(i + 1, j) + 1;
			}

			// for迴圈找出從height[i][j]出發的最大長度
			for (int k = 0; k < temp.length; k++)
			{
				if (max < temp[k])
				{
					max = temp[k];
				}
			}
			flag[i][j] = max;
			return max;

		}
	}

	/**
	 * 預設取temp[i]=1是因為當該點不能向繼續滑動時,應返回1
	 */
	// 返回最大長度
	public void maxLength()
	{

		int x = 0;
		int y = 0;
		// x,y記錄座標位置
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				if (maxLength < max_Len(i, j))
				{
					maxLength = max_Len(i, j);
					x = i;
					y = j;
				}
			}
		}
		System.out.println("最大長度為:" + (maxLength));
		System.out.print("最大路徑:");
		printPath(x, y);
	}
	public void printPath(int a, int b)
	{
		// 列印最長路徑
		int i = a;
		int j = b;
		int x = 0;
		int y = 0;// x y記錄.座標
		int max = 0;

		while (maxLength > 0)
		{
			int[][] temp = new int[row][col];
			// 暫存前後左右四個方向滑行的長度以及座標
			System.out.print(height[i][j] + " ");
			// 輸出最長路徑上的點
			if (j > 0 && height[i][j - 1] < height[i][j])
			{
				// 向左試探
				temp[i][j - 1] = flag[i][j - 1];
			}
			if (j < col - 1 && height[i][j + 1] < height[i][j])
			{
				// 向右試探
				temp[i][j + 1] = flag[i][j + 1];
			}
			if (i > 0 && height[i - 1][j] < height[i][j])
			{
				// 向前試探
				temp[i - 1][j] = flag[i - 1][j];
			}
			if (i < row - 1 && height[i + 1][j] < height[i][j])
			{
				// 向後試探
				temp[i + 1][j] = flag[i + 1][j];
			}
			for (int m = 0; m < row; m++)
			{
				// 求前後左右四個方向滑雪最大長度點
				for (int n = 0; n < col; n++)
				{
					if (max < temp[m][n])
					{
						max = temp[m][n];
						x = m;
						y = n;
					}
				}
			}
			i = x;
			j = y;
			max = 0;// i j max重新賦值
			maxLength--;
		}
	}

	public static void main(String[] args)
	{
		int i;
		int j;
		Scanner scanner = new Scanner(System.in);
		i = scanner.nextInt();// 行
		j = scanner.nextInt();// 列
		DP m = new DP(i, j);
		m.maxLength();
	}

}


c++

#include <iostream>
using namespace std;

//***********************常量定義*****************************

const int MAX = 105;


//*********************自定義資料結構*************************




//********************題目描述中的變數************************

int row;
int col;
int data[MAX][MAX];


//**********************演算法中的變數**************************

//dp[i][j]表示從data[i][j]出發所能滑行的最大長度
int dp[MAX][MAX];


//***********************演算法實現*****************************


int DP( int r, int c )
{
	//如果已經計算過,則直接返回
	if( dp[r][c] > 0 ) return dp[r][c];
	
	int ans = 0;
	int tmp = 0;

	//只對有效的r、c進行計算
	if( r - 1 >= 1 )
	{
		//剪枝:只能滑行更低的點
		if( data[r][c] > data[r-1][c] )
		{
			tmp = DP( r-1, c );
			if( ans < tmp ) ans = tmp;
		}					
	}
	if( r + 1 <= row )
	{
		if( data[r][c] > data[r+1][c] )
		{
			tmp = DP( r+1, c );
			if( ans < tmp ) ans = tmp;
		}
	}
	if( c - 1 >= 1 )
	{
		if( data[r][c] > data[r][c-1] )
		{
			tmp = DP( r, c-1 );
			if( ans < tmp ) ans = tmp;
		}
	}
	if( c + 1 <= col )
	{
		if( data[r][c] > data[r][c+1] )
		{
			tmp = DP( r, c+1 );
			if( ans < tmp ) ans = tmp;
		}
	}
	
	//如果是普通點,由狀態轉換方程
	//DP(i,j) = max( DP(i,j-1), DP(i,j+1), DP(i-1,j), DP(i+1,j) ) + 1;
	//如果是某個區域性最低點,則返回 0 + 1 = 1
	dp[r][c] = ans + 1;
	return dp[r][c];
	
}

void Solve()
{
	int ans = 0;
	for( int i=1; i<=row; i++ )
	{
		for( int j=1; j<=col; j++ )
		{
			int tmp = DP( i, j );
			if( ans < tmp ) ans = tmp;			
		}
	}
	cout << ans << endl;

	cout<<"-----------------"<<endl;
	for( int i=1; i<=row; i++ )
	{
		for( int j=1; j<=col; j++ )
		{
			cout<< dp[i][j]<<" ";
		}
		cout<<endl;
	}

}


//************************main函式****************************

int main()
{
	//freopen( "in.txt", "r", stdin );	

	cin >> row >> col;

	for( int i=1; i<=row; i++ )
	{
		for( int j=1; j<=col; j++ )
		{
			cin >> data[i][j];
		}
	}

	//輸出
	for( int i=1; i<=row; i++ )
	{
		for( int j=1; j<=col; j++ )
		{
			cout<< data[i][j]<<" ";
		}
		cout<<endl;
	}
	
	Solve();
	system("pause");
	return 0;
}