Description

You are playing the game ‘Whack the Groundhog’ with your little nephew. Considering your terrible performance, you decide to get some help to establish your glory image in front of him. The first is to collect some necessary data. After a few times of practicing, you get to know exactly when the groundhogs will get up. Easy job, isn’t it? However, it seems that this is not enough, how to move your hammer is really a problem, and you’d like to solve it by computer.

To make things easier, we assume that the game is playing on one N × Nrectangle grid, means that there are N × N holes in the land. The hammer is on the top left corner at the zero second (the beginning of the game). You can move the hammer to the up, down, right or left cell, at the speed of one cell per second, while whacking the groundhogs takes zero second. Knowing that when and where the groundhogs are getting up, your task here is to calculate the maximum possible number of groundhogs that can be whacked. Please note that only when a groundhog gets up and at the same time the hammer is on the same cell with that groundhog, it gets whacked. You can also keep the hammer for any time on any cell.

Input

The first line of input will be a positive integer TT ≤ 10, indicating T test cases follow.

For each test case, the first line of input will be a positive integer NN ≤ 10, indicating the size of the grid. The second line of input will be a positive integer CC ≤ 1000, then C lines follow, each line contain 3 integers XY and S, separated by one blank space. X and Y indicating the row and column of the specific groundhog getting up, and S indicating the second of the groundhog getting up, both XY and S are counting from 0, 0 ≤ XY < N, 0 ≤ S ≤ 1000.

Output 

For each test case, output one integer on one line, indicating the maximum possible number of groundhogs that can be whacked.

Sample Input
1
2
4
0 0 0
0 1 1
1 0 1
1 0 2

Sample Output
3

Hint

In the example above, the optimal solution should be as follow.

1.        Whacked the groundhog in row 0 and column 0 at time 0;

2.        Move the hammer to row 1 and column 0, whacked the groundhog there at time 1;

3.        Keep the hammer at row 1 and column 0, whacked the groundhog there at time 2.

Problem Source: 庆五一,迎省赛——gdcpc2010

分析:

dp[t][i][j]:t代表时间,(i,j)代表坐标。

挺简单的一个动规,一开始思路错了,后来把思路改过来,初始化方式又错了……→_→

之前我把数组所有的元素都初始化为0,WA。

后来改的是:如果第0时刻(0,0)位置有地鼠,dp[0][0][0]就初始化为1,否则初始化为0,其余均初始化为负无穷。

若dp[t][i][j]为负数,则代表t时刻锤子无法到达(i,j)位置。

因为第0时刻锤子在(0, 0)位置,且一个单位时间只能四方向移动一个格。如果全部初始化为0,就意味着第0时刻锤子可以在任意位置,这显然是不对的。

View Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5
 6 using namespace std;
 7
 8 const int MAXN = 20;
 9 const int MAXS = 1010;
10 const int INF = 2147483645;
11 const int dx[] = { 0, 0, 0, -1, 1 };
12 const int dy[] = { 0, -1, 1, 0, 0 };
13
14 struct node
15 {
16     int x, y;
17     int t;
18 };
19
20 node D[MAXS];
21 int dp[MAXS][MAXN][MAXN];
22 int vis[MAXS][MAXN][MAXN];
23 int N, C, maxT;
24
25 bool check( int x, int y )
26 {
27     return x >= 0 && x < N && y >= 0 && y < N;
28 }
29
30 void solved()
31 {
32     memset( dp, 0, sizeof(dp) );
33
34     for ( int i = 0; i < N; ++i )
35         for ( int j = 0; j < N; ++j )
36             dp[0][i][j] = -INF;
37
38     dp[0][0][0] = vis[0][0][0];
39
40     int ans = vis[0][0][0];
41     for ( int t = 1; t <= maxT + 1; ++t )
42     {
43         for ( int i = 0; i < N; ++i )
44             for ( int j = 0; j < N; ++j )
45                 dp[t][i][j] = -INF;
46         for ( int i = 0; i < N; ++i )
47             for ( int j = 0; j < N; ++j )
48             {
49                 for ( int k = 0; k < 5; ++k )
50                 {
51                     int tpx = i + dx[k];
52                     int tpy = j + dy[k];
53                     if ( check( tpx, tpy ) )
54                     {
55                         dp[t][i][j] = max( dp[t][i][j], dp[t - 1][tpx][tpy] + vis[t][i][j] );
56                         ans = max( ans, dp[t][i][j] );
57                     }
58                 }
59             }
60     }
61
62     printf( "%d\n", ans );
63     return;
64 }
65
66 int main()
67 {
68     int T;
69     scanf( "%d", &T );
70     while ( T-- )
71     {
72         scanf( "%d%d", &N, &C );
73         memset( vis, 0, sizeof(vis) );
74         maxT = 0;
75         for ( int i = 0; i < C; ++i )
76         {
77             scanf( "%d%d%d", &D[i].x, &D[i].y, &D[i].t );
78             maxT = max( maxT, D[i].t );
79             vis[ D[i].t ][ D[i].x ][ D[i].y ] = 1;
80         }
81
82         solved();
83     }
84     return 0;
85 }

转载于:https://www.cnblogs.com/GBRgbr/archive/2013/04/30/3052323.html

SOJ 8064 Whack the Groundhog相关推荐

  1. SOJ 4543 4542

    http://acm.scu.edu.cn/soj/problem.action?id=4542 递归用数组保存中间值 #include <cstdio> #include <cma ...

  2. Soj题目分类 python代码)

    正值期末复习,刷点soj放松下 但想看看能不能在找点关于数据结构的题目来做一下. 在网上看到有不少人上传过那些关于部分SOJ题目的描述,但是说实话有些乱 不过我看到有个网页中包含的一个类似文档的东西, ...

  3. apq 8064 uart debug

    一般需要示波器测试TX,但是8064有个内部循环,直接enable可以不需要示波器也能判断,UART DMA是否是通的,相当于内部将TX短接到RX,这样给我们软件调试 串口功能带来了便利.

  4. 【2020年牛客暑假第九场】E题 Groundhog Chasing Death

    [2020年牛客暑假第九场]E题 Groundhog Chasing Death 质因子分解 题意 思路 方法一:先枚举iii再枚举公共质因子 Code(286ms) 方法二:先枚举公共质因子再枚举i ...

  5. soj 4421 laobi与回文子串

    链接:http://cstest.scu.edu.cn/soj/problem.action?id=4421 上次初赛我们队并没有出这题,,只能眼睁睁的看着出了好多只队... 思路:将这个字符串翻转过 ...

  6. 2020牛客暑期多校训练营(第九场) Groundhog Chasing Death

    Groundhog Chasing Death 题意:给出a.b.c.d.x.y,让求下列式子. 思路:对于gcd(x,y)我们知道 gcd(x,y)=p1 ^(min(n1,n2)) * p2 ^( ...

  7. E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解)

    E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解) 链接:https://ac.nowcoder.com/acm/contest ...

  8. Groundhog Chasing (数论质因数)

    Groundhog Chasing (数论&质因数) 思路:枚举质因子贡献. 然后第一维暴力,第二维用公式求和. 第二维分三种情况: 设当前因子为 s s s, x x x的该因子个数为 c ...

  9. soj 4539 贪心+优先队列

    不难,想到优先队列就比较容易做,具体思路见代码注释 也可以使用  重载小于号写到结构体内 如果按能力值排序会比较麻烦一点 #include<cstdio> #include<algo ...

最新文章

  1. Alibaba Dubbo框架同步调用原理分析-1
  2. 未转变者服务器床id,最新id欢迎补充
  3. 星型模型和雪花型模型比较
  4. 不会三种编程语言的不算程序员 走近阿里云 MVP烁淼吐槽大佬
  5. MySql中4种批量更新的方法
  6. 2021河南固高高考成绩查询,河南信阳最好的4所高中,前三所学霸如云,看看有没有你的母校?...
  7. 安装配置ASMlib驱动
  8. 译]bootstrap-select (selectpicker)方法
  9. 并发编程模型Akka
  10. day10.函数,函数的参数
  11. Windows系统查看svg缩略图插件
  12. matlab 读取shp面文件,在matlab中将处理结果输出为shp文件
  13. SpringBoot:整合Solr
  14. 社会管理网格化 源码_【西市场快讯】槐荫区委政法委副书记李岩雍赴西市场街道督导网格化管理工作...
  15. 2021年北京高校数学建模校际联赛题目出版社图书印制策略解题论文及程序
  16. UOJ #60 [UR #5] 怎样提高智商
  17. 新手小白如何选择吉他,初学者的入门吉他推荐
  18. Spring 事件发布机制@EventListener源码深度解析
  19. NLP逻辑回归模型(LR)实现分类问题实例详解
  20. HttpClient javax.net.ssl.SSLPeerUnverifiedException: Certificate doesn't match 错误解决办法

热门文章

  1. 计算机学院 拔河比赛加油词,运动会拔河比赛加油词
  2. 网站开启https后很慢_HTTPS会影响网站打开速度吗
  3. 梦幻桌面wmv_【 梦幻桌面 】梦幻桌面(DreamScene桌面美化工具)新版下载 - U大师
  4. oracle 主机名改ip,[oracle 10.2]主机名或者IP地址改变造成的dbconsole服务无法启动解决...
  5. python编程程序设计_程序设计入门—Python
  6. PCL安装与环境变量配置(Win10)
  7. python postgresql跨数据库查询_Postgresql跨数据库查询
  8. [JUC-5]ConcurrentHashMap源码分析JDK8
  9. 联科集团携手阿里云发布科研混合云平台 共建科研教育新生态
  10. Docker 1.7.0 深度解析