标签:set bsp get oid lse man code sizeof int()
题意:给一个矩阵,从一个点可以走向左下和右下,走过的路径形成一条路径,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; }
标签:set bsp get oid lse man code sizeof int()
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/11487951.html