Time limit(ms): 1000    Memory limit(kb): 65535
Several coins are placed in cells of an n×m board. A robot, located in the upper left cell of the board, needs to collect as many of the coins as possible and bring them to the bottom right cell. On each step, the robot can move either one cell to the right or one cell down from its current location.

Description
The fist line is n,m, which 1< = n,m <= 1000. 
Then, have n row and m col, which has a coin in cell, the cell number is 1, otherwise is 0.

Input
The max number Coin-collecting by robot.

Output
1
2
3
4
5
6
7
5 6
0 0 0 0 1 0
0 1 0 1 0 0
0 0 0 1 0 1
0 0 1 0 0 1
1 0 0 0 1 0
 
Sample Input
1
2
5
 
Sample Output
Hint
algorithm text book
 
题目大意:就是给出一个方阵nXm,每个格子1代表有硬币,0代表没有,问从左上角,到右下角(每次只能向下和向右移动)最多能收集多少硬币
 
思路其实也挺简单的就是一个从终点到起点的反向dp,每次只能每次只能向下和向右移动(注意dp是反向进行的)
于是得到了一个dp方程dp[i][j] += max(dp[i + 1][j] , dp[i][j + 1] )  注:这里为了降低空间复杂度直接用dp数据存贮的矩阵
 
代码如下
 #include <stdio.h>
int rows, dp[][];
int main()
{
int i, j, n, m;
scanf("%d%d", &n, &m);
for (i = ; i < n; i++)
for (j = ; j < m; j++)
scanf("%d", &dp[i][j]);
for (i = n - ; i >= ; i--)
for (j = m - ; j >= ; j--)
dp[i][j] += dp[i + ][j] > dp[i][j + ] ? dp[i + ][j] : dp[i][j + ];
printf("%d\r\n", dp[][]);
return ;
}

其实最开始并没有想到dp(还是题做的少,没这个概念),直接两个方位的bfs+优先队列

感觉应该是对的,为啥就是wa 呢?贴出代码,求大神指教

 #include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int map[][], vis[][], dir[][] = { , , , };
int n, m;
struct node{
int x, y, cur;
friend bool operator<(node x, node y){
return x.cur < y.cur;
}
};
int bfs(){
priority_queue<node>Q;
struct node now, next;
now.x = now.y = , now.cur = map[][];
vis[][] = ;
Q.push(now);
while (!Q.empty()){
now = Q.top();
Q.pop();
if (now.x == n&&now.y == m)
return now.cur;
for (int i = ; i < ; i++){
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
if (next.x >= && next.x <= n && next.y >= && next.y <= m &&!vis[next.x][next.y]){
next.cur = now.cur + map[next.x][next.y];
vis[next.x][next.y] = ;
Q.push(next);
}
}
}
}
int main(){
cin >> n >> m;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
cin >> map[i][j];
cout << bfs() << "\r\n";
return ;
}

[Swust OJ 1132]-Coin-collecting by robot的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  5. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  6. [Swust OJ 795]--Penney Game

    题目链接:http://acm.swust.edu.cn/problem/795/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  7. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  8. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  9. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

随机推荐

  1. Jquery对select的操作(附日历天数变化代码)

    转载请注明出处. 逃不开传统的四种操作:增.删.改.查. <四处搜刮了jquery对select操作的代码,汇集一下,方便以后查看.日历天数变化代码为原创.> [增]: $("# ...

  2. mysql 多重游标嵌套

    1.DECLARE CONTINUE HANDLER FOR NOT FOUND 在mysql的存储过程中经常会看到这句话:DECLARE CONTINUE HANDLER FOR NOT FOUND ...

  3. Android 开发笔记“Eclipse 调试和快捷键”

    原文地址:http://blog.sina.com.cn/s/blog_5cf876340100aswr.html Eclipse 调试器和 Debug 视图 Eclipse SDK 是针对 Java ...

  4. bzoj 1040: [ZJOI2008]骑士 树形dp

    题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3054  Solved: 1162[Submit][S ...

  5. squid客户端命令

    常用squid客户端命令: squidclient -p mgr:info #取得squid运行状态信息: squidclient -p mgr:mem #取得squid内存使用情况: squidcl ...

  6. C++ 面向对象学习1

    #include "stdafx.h" #include <iostream> //不要遗漏 否则不能使用cout using namespace std; class ...

  7. Splunk

    http://www.huxiu.com/article/33724/1.html http://www.netis.com.cn/splunk/%E4%BB%80%E4%B9%88%E6%98%AF ...

  8. html img 使用data格式加载图片

    背景 这久闲来无事给一位客户测试一款体检软件,是winform结构的,其中有一个功能是需要把生成的体检报告导出为html格式.测试导出后直接在谷歌浏览器里查看,体检详细内容.医生签名.条形码都能正常显 ...

  9. 在Spring Boot启动后执行指定代码

    在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据等等. 在Spring中可以通过ApplicationListener来实现相关的功能,不过在配合Spring Bo ...

  10. 从事web前端两年半后的迷茫

    做了两年半的重构,突然有种迷茫的感觉,好像瓶颈了,不知道自己该怎么继续走下去,以前刚毕业的时候,总觉得自己有好多的东西要学在前端方面,所以有那个促使自己去学习的动力,每当没工作任务的时候,自己总是去主 ...