DP入门
穿过街道
Problem:20
Time Limit:1000ms
Memory Limit:65536K
Description
一个城市的街道布局如下:从最左下方走到最右上方,每次只能往上或往右走,一共有多少种走法?
Input
输入很多行行数,每行1个数字代表n的值,当n=0时结束(2<=n<=15)
Output
输出对应每行n值的走法.
Sample Input
1 2 10 5 0
Sample Output
2 6 184756 252
<span style="font-size:24px;"><span style="color: rgb(51, 51, 51);">第一种做法是递归 把边界置为 1, </span><span style="color:#ff0000;">然后中间每个格子等于上一步和左一部的走法之和</span></span>
<span style="font-family:Menlo, Monaco, Consolas, Courier New, monospace;color:#333333;"><span style="font-size: 18px; line-height: 25.7143px; white-space: pre-wrap;">#include <iostream> using namespace std; int main() { long long a[21][21]; for(int i=0;i<21;i++) { a[i][0]=1; a[0][i]=1; } for(int i=1;i<21;i++) { for(int j=1;j<21;j++) a[i][j]=a[i-1][j]+a[i][j-1]; } int n; while(cin>>n&&n) { cout<<a[n][n]<<endl; } return 0; }</span></span>
<span style="font-family:Menlo, Monaco, Consolas, Courier New, monospace;color:#333333;"><span style="font-size: 18px; line-height: 25.7143px; white-space: pre-wrap;"> </span></span>
<span style="font-family:Menlo, Monaco, Consolas, Courier New, monospace;color:#333333;"><span style="font-size: 18px; line-height: 25.7143px; white-space: pre-wrap;"> </span></span>
<span style="font-family:Menlo, Monaco, Consolas, Courier New, monospace;color:#333333;"><span style="font-size: 18px; line-height: 25.7143px; white-space: pre-wrap;">递归写法 倒着推</span></span>
<xmp class="prettyprint prettyprinted" style="box-sizing: border-box; word-wrap: break-word !important;"><span style="font-size:24px;color:#333333;"><span style="white-space: pre-wrap;">#include <iostream> #include<string.h> using namespace std; long long a[21][21]; long long digui(int x,int y) { if (a[x][y]!=0) return a[x][y]; //被算过 int result; if (x==0||y==0) result=1; else result=digui(x-1,y)+digui(x,y-1); a[x][y]=result; //每个格子只算一次~~ return result; } int main() { memset(a,0,sizeof(a)); int n; while(cin>>n&&n) { cout<<digui(n,n)<<endl; //从最后往上推 } return 0; }</span></span>
<div class="panel-heading" style="box-sizing: border-box; padding: 10px 15px; border-bottom-width: 1px; border-bottom-style: solid; border-color: rgb(51, 122, 183); border-top-left-radius: 3px; border-top-right-radius: 3px; color: rgb(255, 255, 255); font-family: 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'WenQuanYi Micro Hei', 'Microsoft YaHei UI', 'Microsoft YaHei', sans-serif; font-size: 14px; line-height: 24.5px; white-space: normal; background-color: rgb(51, 122, 183);"><h3 class="text-center" id="contestShowProblem-title" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 24px; text-align: center;">步步惊心</h3></div><div class="panel-body" style="box-sizing: border-box; padding: 15px; color: rgb(80, 80, 80); font-family: 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'WenQuanYi Micro Hei', 'Microsoft YaHei UI', 'Microsoft YaHei', sans-serif; font-size: 14px; line-height: 24.5px; white-space: normal;"><div class="row text-center" style="box-sizing: border-box; text-align: center; margin-right: -15px; margin-left: -15px;"><div class="col-sm-4" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 287.234px;"><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Problem:<span id="contestShowProblem-problem" style="box-sizing: border-box;">D</span></h4></div><div class="col-sm-4" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 287.234px;"><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Time Limit:<span id="contestShowProblem-timeLimit" style="box-sizing: border-box;">1000</span>ms</h4></div><div class="col-sm-4" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 287.234px;"><h4 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 10px; margin-bottom: 10px; font-size: 18px;">Memory Limit:<span id="contestShowProblem-memoryLimit" style="box-sizing: border-box;">65536</span>K</h4></div></div></div><div class="panel panel-info" style="box-sizing: border-box; margin-bottom: 20px; border: 1px solid rgb(188, 232, 241); border-radius: 4px; box-shadow: rgba(0, 0, 0, 0.0470588) 0px 1px 1px; color: rgb(80, 80, 80); font-family: 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'WenQuanYi Micro Hei', 'Microsoft YaHei UI', 'Microsoft YaHei', sans-serif; font-size: 14px; line-height: 24.5px; white-space: normal;"><div class="panel-heading" style="box-sizing: border-box; padding: 10px 15px; border-bottom-width: 1px; border-bottom-style: solid; border-color: rgb(188, 232, 241); border-top-left-radius: 3px; border-top-right-radius: 3px; color: rgb(49, 112, 143); background-color: rgb(217, 237, 247);"><h3 class="panel-title" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 0px; margin-bottom: 0px; font-size: 16px;">Description</h3></div><div class="panel-body" style="box-sizing: border-box; padding: 15px;"><pre class="pre-large-font" id="contestShowProblem-description" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 15px; padding: 0px 10px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: normal; border: 0px; border-radius: 4px; word-wrap: break-word !important; white-space: pre-wrap !important; background-color: inherit;"><xmp style="box-sizing: border-box; font-family: inherit; font-size: 18px; white-space: pre-wrap !important; word-wrap: break-word !important;">马尔泰·若曦是康熙年间镇西大将军马尔泰的小女儿,自幼失母,却深得父亲姐姐宠爱,性格活泼任性。张晓,本是21世纪一都市白领,聪慧谨慎,玲珑剔透。因车祸而灵魂穿越到若曦身上,自此开始了步步惊心的宫庭之旅,并身不由己卷进了九龙夺嫡的风波。在这里,若曦与大清未来的皇帝----雍正皇帝新觉罗·胤禛相遇,并上演了一场爱恨情仇中的生死挣扎。权利与亲情、与爱情;欲望和名利下上演一场场惊天动地,凄凉婉转的、曲折的惊心动魄的历史片段。最后在无奈和挣扎中香消玉损,只留下雍正痛苦的坚持和对大清的责任。 若曦刚来到北京皇宫时,就对复杂的皇宫所迷惑---屋子太多了。皇宫的屋子是m行n列的方格,进到理想的屋子里会得到奖赏(银子),走到禁闭的屋子要扣月厘(银子),难啊。若曦从左下角(1,1)位置,走到右上角(m,n)位置,通过最短的距离能获得的最多银子是多少啊? (1,1)是左下角的屋子的坐标。
Input
输入数据有多组,每组第一行有2个数m和n(0 < m,n <=100),代表m行,n列,接下来有m行,每行n个数;这m行里的第1行的n个数代表的是皇宫位置m行的位置;而这m行里的第2行的n个数代表的是皇宫位置m-1行的位置;依次;这m行里的第m行的n个数代表的是皇宫位置第1行的位置;具体见图就明白了!每个屋子能得到或失去银子的值为0~100,得到用正数,失去用负数表示。
Output
输出从(1,1)位置开始,走到右上角(m,n)位置,通过最短的距离若曦能获得的最大银子数(不为了银子,谁穿越啊)!
Sample Input
5 5 1 2 7 1 4 1 -6 -7 -3 -2 -6 20 2 0 4 -2 12 6 24 30 10 12 13 -9 -4
Sample Output
101
Hint
sample_input 中的10是(1,1)位置,而4是(5,5)位置,别错! 要求通过最短的距离即在图中只能往上或往右走!
这个题和上面的很像 就是图是倒着的。。。。。。
<span style="font-family:Menlo, Monaco, Consolas, Courier New, monospace;color:#333333;"><span style="font-size: 18px; line-height: 25.7143px; white-space: pre-wrap;">#include <iostream> #include<string.h> #include<algorithm> using namespace std; int a[101][101],b[101][101]; int main() { int n,m; while(cin>>m>>n) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) cin>>a[i][j]; } b[m][1]=a[m][1]; for(int i=m-1;i>=1;i--) b[i][1]=b[i+1][1]+a[i][1]; for(int j=2;j<=n;j++) b[m][j]=b[m][j-1]+a[m][j]; for(int i=m-1;i>=1;i--) { for(int j=2;j<=n;j++) b[i][j]=max(b[i+1][j],b[i][j-1])+a[i][j]; } cout<<b[1][n]<<endl; } return 0; }</span></span><span style="color: rgb(51, 51, 51); font-family: inherit; font-size: 18px; line-height: 1.42857; white-space: pre-wrap !important;"> </span>
命运
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18699 Accepted Submission(s): 6485Problem Description
穿过幽谷意味着离大魔王lemon已经无限接近了!可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个机关。要知道,不论何人,若在迷宫中被困1小时以上,则必死无疑!可怜的yifenfei为了去救MM,义无返顾地跳进了迷宫。让我们一起帮帮执着的他吧!命运大迷宫可以看成是一个两维的方格阵列,如下图所示:
yifenfei一开始在左上角,目的当然是到达右下角的大魔王所在地。迷宫的每一个格子都受到幸运女神眷恋或者痛苦魔王的诅咒,所以每个格子都对应一个值,走到那里便自动得到了对应的值。现在规定yifenfei只能向右或者向下走,向下一次只能走一格。但是如果向右走,则每次可以走一格或者走到该行的列数是当前所在列数倍数的格子,即:如果当前格子是(x,y),下一步可以是(x+1,y),(x,y+1)或者(x,y*k) 其中k>1。 为了能够最大把握的消灭魔王lemon,yifenfei希望能够在这个命运大迷宫中得到最大的幸运值。
Input
输入数据首先是一个整数C,表示测试数据的组数。每组测试数据的第一行是两个整数n,m,分别表示行数和列数(1<=n<=20,10<=m<=1000);接着是n行数据,每行包含m个整数,表示n行m列的格子对应的幸运值K ( |k|<100 )。
Output
请对应每组测试数据输出一个整数,表示yifenfei可以得到的最大幸运值。
Sample Input
1
3 8
9 10 10 10 10 -10 10 10
10 -11 -1 0 2 11 10 -20
-11 -11 10 11 2 10 -10 -10
Sample Output
52
这个题又是上一个的升级版了 每一格的值除了来自于上方 左一格 还可以来自于左边的因子
#include <iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int a[21][1001],b[21][1001];
int main()
{ int t,m,n;
while(cin>>t)
{
while(t--)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cin>>a[i][j];
}
b[1][1]=a[1][1];
for(int i=2;i<=n;i++) //处理第一列
b[i][1]=b[i-1][1]+a[i][1];
for(int i=2;i<=m;i++) //处理第一行 注意一下方法 找完最大值再加本身
{ b[1][i]=b[1][i-1];
for(int j=1;j<=i/2;j++) //算到一半就好咯 if(i%j==0)b[1][i]=max(b[1][i],b[1][j]);
b[1][i]+=a[1][i]; }
for(int i=2;i<=n;i++)
{ for(int j=2;j<=m;j++)
{ b[i][j]=max(b[i][j-1],b[i-1][j]); for(int k=1;k<=j/2;k++) { if(j%k==0)b[i][j]=max(b[i][j],b[i][k]); } b[i][j]+=a[i][j]; } } cout<<b[n][m]<<endl; } } return 0;}
免费馅饼
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 48510 Accepted Submission(s): 16785Problem Description
都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:

为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)
Input
输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。
Output
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。
Sample Input
6
5 1
4 1
6 1
7 2
7 2
8 3
0
Sample Output
4
#include <iostream> #include<string.h> #include<algorithm> #include<stdio.h> using namespace std; int a[100001][11],b[100001][11]; int main() { int n; while(cin>>n&&n) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int x,t,max_t=-1; for(int i=0;i<n;i++) { scanf("%d%d",&x,&t); a[t][x]++; //不知道这里有几个馅饼 每次加 if(max_t<t)max_t=t; } for(int i=0;i<=10;i++) //初始化最后一行 b[max_t][i]=a[max_t][i]; for(int i=max_t-1;i>=0;i--) { for(int j=0;j<=10;j++) { if(j==0)b[i][j]=max(b[i+1][j],b[i+1][j+1])+a[i][j]; else if(j==10)b[i][j]=max(b[i+1][j],b[i+1][j-1])+a[i][j]; else b[i][j]=max(max(b[i+1][j],b[i+1][j-1]),b[i+1][j+1])+a[i][j]; } } cout<<b[0][5]<<endl; } return 0; }
滑雪
#include <iostream>
#include<string.h>
using namespace std;
int r,c,map[101][101],data[101][101];
int cal(int i,int j)
{
if(data[i][j]!=0)return data[i][j];
int maxright=0,maxleft=0,maxup=0,maxdown=0;
if(j+1<c&&map[i][j]>map[i][j+1])
maxright=cal(i,j+1);
if(j-1>=0&&map[i][j]>map[i][j-1])
maxleft=cal(i,j-1);
if(i-1>=0&&map[i][j]>map[i-1][j])
maxup=cal(i-1,j);
if(i+1<r&&map[i][j]>map[i+1][j])
maxdown=cal(i+1,j);
int max1,max2;
max1=max(maxright,maxleft);
max2=max(maxup,maxdown);
data[i][j]=max(max1,max2)+1;
return data[i][j];
}
int main()
{
while(cin>>r>>c)
{
memset(map,0,sizeof(map));
memset(data,0,sizeof(data));
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cin>>map[i][j];
}
int ans=-999;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
if(ans<cal(i,j))ans=cal(i,j);
}
cout<<ans<<endl;
}
return 0;
}