陳老師的多校聯合20140816A題||spoj 10228 動態規劃
http://www.spoj.com/problems/AMR11A/
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did we not tell you that it was just an online game ? uhhh! now here is the real onsite task for Harry. You are given a magrid S ( a magic grid ) having R rows and C columns. Each cell in this magrid has either a Hungarian horntail dragon that our intrepid hero has to defeat, or a flask of magic potion that his teacher Snape has left for him. A dragon at a cell (i,j) takes away |S[i][j]| strength points from him, and a potion at a cell (i,j) increases Harry's strength by S[i][j]. If his strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.
Harry starts from the top-left corner cell (1,1) and the Sorcerer's Stone is in the bottom-right corner cell (R,C). From a cell (i,j), Harry can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and he can not move outside the magrid. Harry has used magic before starting his journey to determine which cell contains what, but lacks the basic simple mathematical skill to determine what minimum strength he needs to start with to collect the Sorcerer's Stone. Please help him once again.
Input (STDIN):
The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers. Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with S[i][j] < 0 contain dragons, others contain magic potions.
Output (STDOUT):
Output T lines, one for each case containing the minimum strength Harry should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).
Constraints:
1 ≤ T ≤ 5
2 ≤ R, C ≤ 500
-10^3 ≤ S[i][j] ≤ 10^3
S[1][1] = S[R][C] = 0
Sample Input:
3
2 3
0 1 -3
1 -2 0
2 2
0 1
2 0
3 4
0 -2 -3 1
-1 4 0 -2
1 -2 -3 0
Sample Output:
2
1
2
解題思路:我們用dp[i][j]表該點活下來需要的最小值,現在知道dp[n][m]的值必為1,狀態轉移方程:
dp[i-1][j]=min(dp[i-1][j],dp[i][j]-a[i-1][j]);
if(dp[i-1][j]<=0)
dp[i-1][j]=1;
dp[i][j-1]=min(dp[i][j-1],dp[i][j]-a[i][j-1]);
if(dp[i][j-1]<=0)
dp[i][j-1]=1;.
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int a[550][555],dp[555][555];
int n,m;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
memset(dp,0x3f3f3f,sizeof(dp));
dp[n][m]=1;
for(int i=n;i>0;i--)
for(int j=m;j>0;j--)
{
dp[i-1][j]=min(dp[i-1][j],dp[i][j]-a[i-1][j]);
if(dp[i-1][j]<=0)
dp[i-1][j]=1;
dp[i][j-1]=min(dp[i][j-1],dp[i][j]-a[i][j-1]);
if(dp[i][j-1]<=0)
dp[i][j-1]=1;
}
printf("%d\n",dp[1][1]);
}
return 0;
}
相關文章
- 陳老師的多校聯合 D題 字串處理起來挺麻煩字串
- HDU 5375 Gray code(2015年多校聯合 動態規劃)動態規劃
- HDU 5389 Zero Escape(2015年多校聯合第八場 動態規劃)動態規劃
- 動態規劃-最少硬幣組合問題動態規劃
- 好題——動態規劃動態規劃
- 動態規劃專題動態規劃
- 動態規劃題單動態規劃
- 我的動態規劃題單動態規劃
- 動態規劃2:面試題 17.16. 按摩師動態規劃面試題
- 動態規劃練習題動態規劃
- 動態規劃解題方法動態規劃
- 動態規劃做題思路動態規劃
- 【動態規劃(一)】動態規劃基礎動態規劃
- 整數劃分問題(動態規劃)動態規劃
- 動態規劃-硬幣組合數目動態規劃
- 動態規劃 擺花 題解動態規劃
- 動態規劃之子序列問題動態規劃
- 揹包問題----動態規劃動態規劃
- 【動態規劃】揹包問題動態規劃
- 做題記錄 --- 動態規劃動態規劃
- 動態規劃入門——動態規劃與資料結構的結合,在樹上做DP動態規劃資料結構
- 動態規劃動態規劃
- 動態規劃9:變態跳臺問題動態規劃
- 動態規劃 01揹包問題動態規劃
- 找零問題與動態規劃動態規劃
- 【動態規劃】01揹包問題動態規劃
- leetcode題解(動態規劃)LeetCode動態規劃
- 動態規劃-01揹包問題動態規劃
- 動態規劃,股票問題留坑動態規劃
- 動態規劃篇——揹包問題動態規劃
- 醜數問題——動態規劃、Java動態規劃Java
- (動態規劃)最小分糖果問題動態規劃
- 動態規劃--01揹包問題動態規劃
- 一維動態規劃和二維動態規劃中兩道經典題目動態規劃
- 動態規劃之數的劃分動態規劃
- 動態規劃分析動態規劃
- 動態規劃(DP)動態規劃
- 動態規劃初步動態規劃