编程介的小学生 2019-08-15 21:46 采纳率: 20.5%
浏览 162

通过编写程序来实现Whac-a-Mole

Problem Description
While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the Whac-a-Mole game is to… well… whack moles. With a hammer. To make the job easier you have first consulted the fortune teller and now you know the exact appearance patterns of the moles.

The moles appear out of holes occupying the n2 integer points (x, y) satisfying 0 ≤ x, y < n in a two-dimensional coordinate system. At each time step, some moles will appear and then disappear again before the next time step. After the moles appear but before they disappear, you are able to move your hammer in a straight line to any position (x2, y2) that is at distance at most d from your current position (x1, y1). For simplicity, we assume that you can only move your hammer to a point having integer coordinates. A mole is whacked if the center of the hole it appears out of is located on the line between (x1, y1) and (x2, y2) (including the two endpoints). Every mole whacked earns you a point. When the game starts, before the first time step, you are able to place your hammer anywhere you see fit.

Input
The input consists of several test cases. Each test case starts with a line containing three integers n, d and m, where n and d are as described above, and m is the total number of moles that will appear (1 ≤ n ≤ 20, 1 ≤ d ≤ 5, and 1 ≤ m ≤ 1000). Then follow m lines, each containing three integers x, y and t giving the position and time of the appearance of a mole (0 ≤ x, y < n and 1 ≤ t ≤ 10). No two moles will appear at the same place at the same time.

The input is ended with a test case where n = d = m = 0. This case should not be processed.

Output
For each test case output a single line containing a single integer, the maximum possible score achievable.

Sample Input
4 2 6
0 0 1
3 1 3
0 1 2
0 2 2
1 0 2
2 0 2
5 4 3
0 0 1
1 2 1
2 4 1
0 0 0

Sample Output
4
2

  • 写回答

1条回答 默认 最新

  • CcCcoes 2019-08-16 17:27
    关注

    #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 ;
    }

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?