Codevs 1043 Squares to take the number

Source: Internet
Author: User

$n the squares of the \times n$, each lattice has a natural number. One person in the square, can only go right or down. From the upper left to the lower right to walk two times, ask the maximum number of numbers can be obtained (each number is not taken, that is, two times through the same location can only get a value).

Consider walking together twice, each line passing through the segment is two segments assumed to be [a, b] and [C, D]. The current row state is [a, b] [C, d] when the transfer is from the previous line of B equals a of this line, and the previous line of D equals the C of this line.

In this case the complexity is $O (n^9) $, probably can not, but consider pruning, you can let $c \ge a$, because otherwise it can be two times the second half of the path as a $c \ge a$.

For example, 3-5 and 1-9, can be changed to 1-5 and 3-9.

This is probably $3\times 10^7$ at n = 10.

#include <bits/stdc++.h>using std::vector;const int maxn = 12;const int maxs = 1 << maxn;int dp[maxn][maxs];in  T a[maxn][maxn];int sum[maxn][maxn];struct status{int A, B, C, D;    int sumvalue (int i) {int res = sum[i][b + 1]-sum[i][a] + sum[i][d + 1]-sum[i][c];    if (c <= b) Res-= sum[i][b + 1]-sum[i][c];  return res;  }};vector<status> Status; #define REP (I, L, R) for (int i = l; i < R; i++) int statustable (int n) {int res = 0;    Rep (i0, 0, N) Rep (I1, I0, N) Rep (j0, I0, N) Rep (J1, J0, N) {status.push_back (status) {I0, I1, J0, J1});  res++; } return res;  int main () {int n;  scanf ("%d", &n);  int sCnt = statustable (n);  int x, y, V;    for (; scanf ("%d%d%d", &x, &y, &v), (x + y + V);)  A[x][y] = v;  for (int i = 1, i <= N; i++) {for (int j = 1; J <= N; j + +) Sum[i][j] = Sum[i][j-1] + a[i][j]; } Rep (i, 0, sCnt) if (status[i].a = = 0 && status[i].b >= status[i].c-1) {Dp[1][i] = Status[i].sumvalue (1); } for (int i = 2; I <= n; i++) {Rep (j, 0, SCnt) Rep (k, 0, SCnt) {if (status[j].b-status[k].a | | status[j].      D-STATUS[K].C) continue;    Dp[i][k] = Std::max (Dp[i][k], dp[i-1][j] + status[k].sumvalue (i));  }} int result = 0;  Rep (i, 0, sCnt) if (status[i].d = = N-1 && status[i].b = = n-1) {result = Std::max (dp[n][i], result);  } printf ("%d\n", result); return 0;}

  

Codevs 1043 Squares to take the number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.