uva-10564-dp

  题意:给一个矩阵,从一个点可以走向左下和右下,走过的路径形成一条路径,sum=路径上所有数字的总和,问,走到最后一行,是否存在一条路径的等于S,如果有,那么有多少条不同的路径,并且输出最小路径。

dp:dp[i][j][k] 表示 i 行 j 列 和为 k的路径总数。为了保证路径最小,从最后一行往前生成路径。

https://vjudge.net/problem/UVA-10564

#include <string>
#include<iostream>
#include <sstream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::bitset;
    using std::stringstream;



    constexpr int N = 20;
    constexpr int S = 500;

    constexpr int MAXR = 2 * N;
    constexpr int MAXC = 2 * N;

    int nums[MAXR][MAXC];
    long long dp[MAXR][MAXC][S + 1];
    int n, s;


    void init()
    {
        memset(nums, -1, sizeof(nums));
        for (int i = 0; i < MAXC; i++)
            nums[2 * n - 1][i] = 0;
        for (int i = 0; i < MAXR; i++)
        {
            for (int j = 0; j < MAXR; j++)
            {
                for (int k = 0; k < S + 1; k++)
                {
                    dp[i][j][k] = 0;
                }
            }
        }
        for (int i = 0; i < MAXC; i++)
        {
            dp[2 * n - 1][i][0] = 1;
        }

    }
    void read()
    {
        int rows = 2 * n - 1;
        int cols = n;
        int start = 1;
        for (int i = 0; i < rows; i++)
        {
            if (i < n)
            {
                int dx = 0;
                for (int j = 0; j < cols; j++)
                {
                    cin >> nums[i][start + dx];
                    dx = dx + 2;
                }
                cols--;
                start++;
            }
            else
            {
                if (cols == 0)
                {
                    cols++;
                    --start;
                }
                cols++;
                --start;
                int dx = 0;
                for (int j = 0; j < cols; j++)
                {
                    cin >> nums[i][start + dx];
                    dx = dx + 2;
                }
            }
        }
    }
    void dpss(int r, int c)
    {
        int pr = r + 1;
        int pcl = c - 1;
        int pcr = c + 1;
        for (int k = 0; k <= s; k++)
        {
            if (nums[pr][pcl] != -1 && dp[pr][pcl][k] != 0)
            {
                dp[r][c][k + nums[r][c]] = dp[r][c][k + nums[r][c]] + dp[pr][pcl][k];
            }
            if (nums[pr][pcr] != -1 && dp[pr][pcr][k] != 0 && pr!=2*n-1)
            {
                dp[r][c][k + nums[r][c]] = dp[r][c][k + nums[r][c]] + dp[pr][pcr][k];
            }
        }
    }
    void dps()
    {
        int rows = 2 * n - 1;
        int cols = n;
        int start = 1;
        for (int i = rows - 1; i >= 0; i--)
        {
            if (i >= n - 1)
            {
                int dx = 0;
                for (int j = 0; j < cols; j++)
                {
                    int r = i;
                    int c = start + dx;
                    dpss(r, c);
                    dx = dx + 2;
                }
                cols--;
                start++;
            }
            else
            {
                if (cols == 0)
                {
                    cols++;
                    --start;
                }
                cols++;
                --start;
                int dx = 0;
                for (int j = 0; j < cols; j++)
                {
                    int r = i;
                    int c = start + dx;
                    dpss(r, c);
                    dx = dx + 2;
                }
            }
        }

    }
    void printpath(int r,int c,int val) 
    {
        int ar = r + 1;
        if (ar == 2 * n - 1)
        {
            return;
        }
        int acl = c - 1;
        int acr = c + 1;
        if (nums[ar][acl] != -1 && dp[ar][acl][val] !=0) 
        {
            cout << "L";
            printpath(ar,acl,val- nums[ar][acl]);
        }
        else if (nums[ar][acr] != -1 && dp[ar][acr][val] != 0)
        {
            cout << "R";
            printpath(ar, acr, val - nums[ar][acr]);
        }

    }
    void print()
    {
        //0 line
        int start = -1;
        long long total = 0;
        for (int i = 0; i < 2 * n; i++)
        {
            if (dp[0][i][s] != 0)
            {
                if (start == -1)
                {
                    start = i;
                }
                total += dp[0][i][s];
            }
        }
        cout << total << endl;
        if (total != 0)
        {
            cout << start / 2 << " ";
            printpath(0,start,s-nums[0][start]);
            cout << endl;
        }
        else
            cout << endl;
    }
    void solve()
    {
        while (cin >> n >> s)
        {
            if (n == 0 && s == 0)
                return;
            init();
            read();
            dps();
            print();
        }


    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}

 

posted on 2019-09-08 19:20  好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11487951.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值