zoukankan      html  css  js  c++  java
  • P1004 方格取数[棋盘dp]

    题目来源:洛谷

    题目描述

    设有N×N的方格图(N9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0。如下图所示(见样例):

    A
     0  0  0  0  0  0  0  0
     0  0 13  0  0  6  0  0
     0  0  0  0  7  0  0  0
     0  0  0 14  0  0  0  0
     0 21  0  0  0  4  0  0
     0  0 15  0  0  0  0  0
     0 14  0  0  0  0  0  0
     0  0  0  0  0  0  0  0
                             B

    某人从图的左上角的A点出发,可以向下行走,也可以向右走,直到到达右下角的B点。在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字0)。
    此人从A点到B点共走两次,试找出2条这样的路径,使得取得的数之和为最大。

    输入输出格式

    输入格式:

    输入的第一行为一个整数N(表示N×N的方格图),接下来的每行有三个整数,前两个表示位置,第三个数为该位置上所放的数。一行单独的0表示输入结束。

    输出格式:

    只需输出一个整数,表示2条路径上取得的最大的和。

    输入输出样例

    输入样例#1: 
    8
    2 3 13
    2 6  6
    3 5  7
    4 4 14
    5 2 21
    5 6  4
    6 3 15
    7 2 14
    0 0  0
    
    输出样例#1: 
    67

    说明

    NOIP 2000 提高组第四题

    解析:

    这题真是跟P1006 传纸条一毛一样,连一点区别都没有,CCF你要点脸好不

    我写的传纸条的题解,戳这里。这题就不多讲了,没区别,真的一点都没有。

    参考代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<string>
     7 #include<cstdlib>
     8 #include<queue>
     9 #include<vector>
    10 #define INF 0x3f3f3f3f
    11 #define PI acos(-1.0)
    12 #define N 101
    13 #define MOD 2520
    14 #define E 1e-12
    15 using namespace std;
    16 int a[11][11],dp[31][20][20];
    17 int main()
    18 {
    19     int n,x,y,val;
    20     scanf("%d",&n);
    21     while(cin>>x>>y>>val&&x!=0&&y!=0&&val!=0)
    22         a[x][y]=val;
    23     dp[1][1][1]=a[1][1];
    24     for(int i=2;i<=n*2-1;i++)
    25      for(int x1=1;x1<=min(n,i);x1++)
    26       for(int x2=1;x2<=min(n,i);x2++){
    27           int y1=i+1-x1,y2=i+1-x2;
    28           dp[i][x1][x2]=max(max(dp[i-1][x1][x2],dp[i-1][x1-1][x2]),max(dp[i-1][x1-1][x2-1],dp[i-1][x1][x2-1]));
    29           if(x1==x2) dp[i][x1][x2]+=a[x1][y1];
    30           else dp[i][x1][x2]+=a[x1][y1]+a[x2][y2];
    31       }
    32     cout<<dp[n*2-1][n][n]<<endl;
    33     return 0;
    34 }
  • 相关阅读:
    Android 2.2 r1 API 中文文档系列(11) —— RadioButton
    Android API 中文 (15) —— GridView
    Android 中文 API (16) —— AnalogClock
    Android2.2 API 中文文档系列(7) —— ImageButton
    Android2.2 API 中文文档系列(6) —— ImageView
    Android 2.2 r1 API 中文文档系列(12) —— Button
    Android2.2 API 中文文档系列(8) —— QuickContactBadge
    [Android1.5]TextView跑马灯效果
    [Android1.5]ActivityManager: [1] Killed am start n
    Android API 中文(14) —— ViewStub
  • 原文地址:https://www.cnblogs.com/DarkValkyrie/p/11041548.html
Copyright © 2011-2022 走看看