P2905 [usaco 08open] farm crisis on the farm

Source: Internet
Author: User

Portal

DP

Set f [I] [J] [k] to indicate that step I has been taken, step J has been taken up, and step K has been taken to the Right to save the maximum number of cows (J, K can be negative, indicating reverse)

If G [I] [J] is set, the number of cows that happen to be on the straw is exactly the same (I, j can be negative)

F [I] [J] [k] = max (F [I-1] [J-1] [K], F [I-1] [J] [k-1], f [I-1] [J + 1] [K], F [I-1] [J] [k + 1]) + G [J] [k]

Because the array subscript cannot be negative, add a K to the coordinate group.

In order to facilitate the output in Lexicographic Order, we need to push it backwards and enumerate it in Lexicographic Order.

That is to say, the direction from F [k] to f [0] enumeration is 'E' 'n' ''W'

It doesn't seem easy to talk about. Let's look at the Code. The Code is not difficult.

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <set> using namespace STD; typedef long ll; inline int read () {int x = 0, F = 1; char CH = getchar (); While (CH <'0' | ch> '9 ') {If (CH = '-') F =-1; CH = getchar () ;}while (CH> = '0' & Ch <= '9 ') {x = (x <1) + (x <3) + (CH ^ 48); CH = getchar () ;}return x * f ;} const int n = 1007, K = 31; int n, m, K; int XX [4] = {1, 0,-1}, YY [4] = {0, 1, -}; char MP [4] = {'E', 'n', 's', 'w '}; int f [k <1] [N] [N], G [k <1] [k <1]; int cow [N] [2], hay [N] [2]; int main () {n = read (); M = read (); k = read (); For (INT I = 1; I <= N; I ++) cow [I] [0] = read (), cow [I] [1] = read (); For (INT I = 1; I <= m; I ++) hay [I] [0] = read (), hay [I] [1] = read (); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) if (ABS (hay [J] [0]-cow [I] [0]) + ABS (hay [J] [1]-cow [I] [1]) <= K) // if the contribution can be reached, G [hay [J] [0]-cow [I] [0] + k] [hay [J] [1]-cow [I] [1] + k] ++; // pre-processing g for (INT I = K; I> = 0; I --) for (INT x = k-I; x <= K + I; X ++) for (INT y = k-I; y <= K + I; y ++) // note the differences between uppercase and lowercase K {for (int o = 0; O <4; O ++) f [I] [x] [Y] = max (F [I] [x] [Y], f [I + 1] [x + XX [O] [Y + YY [O]); f [I] [x] [Y] + = G [x] [Y];} printf ("% d \ n ", f [0] [k] [k]); int posx = K, Posy = K; // store the current position for (INT I = 0; I <K; I ++) {int o; For (O = 0; O <4; O ++) if (F [I] [posx] [posy] = f [I + 1] [posx + XX [O] [posy + YY [O] + G [posx] [posy]) break; // if it is pushed from o, it will disconnect posx + = XX [O]; posy + = YY [O]; printf ("% C ", MP [O]);} return 0 ;}

 

P2905 [usaco 08open] farm crisis on the farm

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.