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. C++内联函数、函数模板之于头文件

    一.基本说明 C++标准中提到,一个编译单元是指一个.cpp文件以及它所include的所有.h文件,.h文件里的代码将会被扩展到包含它的.cpp文件里,然后编译器编译该.cpp文件为一个.obj文件 ...

  2. jQuery常用方法集锦

    用方法:http://www.cnblogs.com/linzheng/archive/2010/11/14/1877092.html 数组汇总:http://www.cnblogs.com/Andy ...

  3. php知识(第2天)

    运算符 PHP中运算符一共分为9类: 赋值运算符, 算术运算符,比较运算符, 逻辑运算符, 错误抑制符, 三目运算符, 位运算符, 自操作运算符, 连接操作符 算术运算符 算术运算: 基本运算符: + ...

  4. 帝国cms7.0,列表模板调用不支持附表字段

    帝国cms在制作列表模板时,是不支持一些字段的调用的,原因是因为有些字段所在的位置为附表,本段详细向你介绍 帝国如何调用副表字段 我们可在 系统---管理数据表---管理字段中查看 如果我们需要调用附 ...

  5. [C++ Basic]C++与Java的主要区别

    1.编译运行 java是解释性语言,java程序在运行时类加载器从类路经中加载相关的类,然后java虚拟机读取该类文件的字节,执行相应操作.而C++编译的 时候将程序编译成本地机器码.一般来说java ...

  6. 【剑指Offer学习】【面试题36:数组中的逆序对】

    题目:在数组中的两个数字假设前面一个数字大于后面的数字.则这两个数字组成一个逆序对.输入一个数组.求出这个数组中的逆序对的总数. 举例分析 比如在数组{7, 5, 6, 4 中, 一共存在5 个逆序对 ...

  7. poj2492 A Bug's Life【基础种类并查集】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  8. 关于RadUpload上传问题总结

    最近在开发上传控件,使用RadUpload上传大附件 发现了几个小问题,总结后分享给大家: 1.IE6浏览器下文件的路径显示的是物理路径,需要进行转换 2.IIS7.0 配置时要选择经典模式 3.we ...

  9. Oracle查询数据中占用空间最大的表

    --第一步,查询istaudit数据库文件ID,文件路径 select file#,name from v$datafile where lower(name) like '%istaudit.dbf ...

  10. BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )

    QAQ我没读过书...四边形都不会判定了 简单的dp.... --------------------------------------------------------------------- ...