Poj3034 -- Whac-a-Mole (dp)

Source: Internet
Author: User

Poj3034 -- Whac-a-Mole (dp)

 

The n * n Square, the hammer moves d at most each time, and the hamster appears at (x, y) time at the t time to maintain a unit of time, two rats won't appear at the same time. The hammer can hit the mouse and ask how many rats can be smashed at most. (The initial hammer can be anywhere)

Dp [t] [I] [j]: the maximum number of hamster that a hammer can hit at the position (I, j) at the t moment.

State transition equation: Because the hammer moves d at most, the points (tx, ty) from (x-d, y-d) to (x + d, y + d) are enumerated ), this is the first point that (x, y) moves to (tx, ty), and then calculates the point in the range of d. Count the number of hit hamster

Note: you are willing to move the nodes outside the boundary.

 

20 5 41 0 10 1 10 5 21 6 20 0 0
Result 4

 

So moving all the points (5, 5) can avoid negative coordinates.

 

#include 
 
  #include 
  
   #include using namespace std ;int dp[12][41][41] ;int Map[12][41][41] ;void f(int n,int t,int x,int y,int d) {    int i , j , k , p , q , num ;    for(i = max(0,x-d) ; i <= min(n-1,x+d) ; i++) {        for(j = max(0,y-d) ; j <= min(n-1,y+d) ; j++) {            if( i == x && j == y ) continue ;            p = i - x ;            q = j - y ;            k = num = 0 ;            while( x+k*p >= 0 && x+k*p < n && y+k*q >= 0 && y+k*q < n && k*k*(q*q+p*p) <= d*d ) {                if( Map[t][x+k*p][y+k*q] ) num++ ;                dp[t][x][y] = max(dp[t][x][y],dp[t-1][x+k*p][y+k*q]+num) ;                k++ ;            }        }    }    return ;}int main() {    int n , d , m ;    int x , y , t ;    int i , j , max_t , ans ;    while( scanf(%d %d %d, &n, &d, &m) && n+d+m != 0 ) {        memset(dp,0,sizeof(dp)) ;        memset(Map,0,sizeof(Map)) ;        max_t = ans = 0 ;        while( m-- ) {            scanf(%d %d %d, &x, &y, &t) ;            Map[t][x+5][y+5] = 1 ;            max_t = max(max_t,t) ;        }        n += 12 ;        for(t = 1 ; t <= max_t ; t++) {            for(i = 0 ; i < n ; i++) {                for(j = 0 ; j < n ; j++) {                    f(n,t,i,j,d) ;                    ans = max(ans,dp[t][i][j]) ;                }            }        }        printf(%d, ans) ;    }    return 0 ;}
  
 


 

 

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.