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. centos6.5 升级python 到 python 2.7.11 安装 pip

    1.首先官方下载源码,然后安装(./configure,make all,make install,make clean,make distclean) 注意:需要先安装zlib-devel,open ...

  2. linux文件系统操作——底层文件访问

        在不使用标准I/O的情况下,使用write,read,open实现对文件的复制操作,这些调用都是直接使用底层系统调用,完成从用户代码到内核代码的切换,消耗大量的系统资源,今天对此进行研究主要是 ...

  3. 基于SIM 卡卡基不同制作工艺的研究

    1 国内外现行的SIM 卡卡基制作工艺 SIM 卡由卡基和芯片两部分组成.卡基上有植入芯片的台阶式芯片槽,SIM 卡的芯片通过多点焊接植入台阶式芯片槽之中与卡基组成SIM 卡,然后经过个性化数据处理, ...

  4. hdoj 2896 病毒侵袭(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另 ...

  5. Sightseeing Cows(最优比率环)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8915   Accepted: 3000 ...

  6. 概率图模型(PGM)学习笔记(三)模式判断与概率图流

    我们依旧使用"学生网络"作为样例,如图1. 图1 首先给出因果判断(Causal Reasoning)的直觉解释. 能够算出来 即学生获得好的推荐信的概率大约是0.5. 但假设我们 ...

  7. Mac OS使用技巧之十五:快捷方便的Mini Dock

    Mini Dock是前面忘记了提,这里做一些补充.       Mini Dock是Mac OSX的一个值得大书特书的亮点.尽管windows下也有类似的东西,但Mac下却提供了更为全面的功能.通过M ...

  8. MVC3 验证码

    public ActionResult GetValidateCode()        {                         string code = CreateValidateC ...

  9. 文件下载 .net

    protected void Button1_Click(object sender, EventArgs e)  { /* 微软为Response对象提供了一个新的方法TransmitFile来解决 ...

  10. HTML+CSS基础总结

    1. 首先声明正确的文档类型doctype 通常有四种文档类型可供选择: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ...