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. servlet三种实现方式之一实现servlet接口

    servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...

  2. kafka集群配置与测试

    刚接触一些Apache Kafka的内容,用了两天时间研究了一下,仅以此文做相关记录,以供学习交流.  概念: kafka依赖的项: 1. 硬件上,kafka利用线性存储来进行硬盘直接读写. 2. k ...

  3. 灵动标签内sql语句调用

    本节来介绍帝国cms中,灵动标签中如何写数据库调用我们所要的信息.方便一些没有学习过数据库的朋友 转载请注明出处:谢寒的博客 灵动标签默认的语法 [e:loop={栏目ID/专题ID,显示条数,操作类 ...

  4. C语言数组内存初始化

    内存初始化当然有必然,但是不用memset,直接这样写就可以了:char* szRemoteFile = new char[MAX_LENGTH](); http://blog.csdn.net/pa ...

  5. MSSQL 如何删除字段的所有约束和索引

    原文MSSQL 如何删除字段的所有约束和索引 代码如下: ---------------------------------------------------------- --  mp_DropC ...

  6. 1005 - Rooks(规律)

    1005 - Rooks   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB A rook is ...

  7. Lua学习笔记5:类及继承的实现

    -- Lua中类的实现 -------------------------------- 基类 ---------------------------- classBase = {x = 0,y = ...

  8. csv批量导入mysql命令

    今天把从Kaggle上下载下来的csv数据导入mysql,想做个统计分析,怎奈csv文件有些大.所以仅仅能用mysql 命令导入,现mark下,以备以后不时之需: 1. 导入: 基本的语法: load ...

  9. COBOL学习

    COBOL概述 什么是COBOL语言:            COBOL是Common Business Oriented Language的缩写,是面向商业通用编程语言.它是专门为商业数据处理而设计 ...

  10. Android 建造者(Builder)模式

    关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android  中 使用了 Bui ...