Topcoder SRM 525 DIV1 300

Source: Internet
Author: User

Test instructions: Give a n*m rectangle, each square is either empty, or there is gold, each time you can choose a direction of the rectangle all gold coins (up and down) to move a box, if the move after the gold coins out of the rectangle, then the gold coin disappears. Ask the least steps to make the checkered gold exactly k
( < Span class= "Mrow" id= "mathjax-span-290" > 1 ≤ n m ≤ )

Solution: Enumerate each sub-rectangle, if the sub-rectangle contains the amount of gold is exactly k, then greedy to calculate the cost of the sub-rectangle, that is, moving up and down to calculate the cost, left and right to move the cost, two times the cost is equal to the minimum number of moves *2+ the maximum number of move times

Code

#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>#include <iostream>#include <vector>using namespace STD;typedef Long Longllclassdropcoins{ Public:inta[ -][ -];intGetminimum ( vector<string>BintK) {memsetA0,sizeof(a)); for(inti =0; I < b.size (); i++) {intt =0; for(intj =0; J < b[0].length (); J + +) {if(B[i][j] = =' O ') ++t; A[I][J] = t;if(I >0) A[i][j] + = a[i-1][J]; }        }intAns =1e9; for(intI1 =0; I1 < B.size (); i1++) { for(intJ1 =0; J1 < b[0].length (); j1++) { for(intI2 = I1; I2 < B.size (); i2++) { for(intJ2 = J1; J2 < b[0].length (); j2++) {inta1 = A[i2][j2];if(I1 >0) A1-= a[i1-1][J2];if(J1 >0) A1-= a[i2][j1-1];if(I1 >0&& J1 >0) A1 + = a[i1-1][j1-1];if(A1 = = k) {intT1, t2, anst =0; T1 = i1, t2 = (int) b.size ()-I2-1; Anst + =2* min (t1, T2) + max (t1, T2); T1 = J1, t2 = (int) b[0].length ()-J2-1; Anst + =2* min (t1, T2) + max (t1, T2);                        ans = min (ans, anst); }                    }                }            }        }if(ans = =1e9) ans =-1;returnAns }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Topcoder SRM 525 DIV1 300

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.