HDU 1278
题目大意:
从(1,1)到(n,n),每经过一个点都要花费一定的时间,问花最短时间的路径有多少条
dfs+dp
先用bfs把所有到n花费的时间逆向dp计算一遍
再用dfs不断找到前一个对应的较短路径的点不断搜索路径
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define LL long long
#define N 52 struct node{
int x,y;
}; queue<node> q;
int dir[][]={{,},{,-},{,},{-,}},dp[N][N],mat[N][N],n;
LL s[N][N]; void bfs()
{
dp[n][n]=mat[n][n];
node a;
a.x=n,a.y=n;
q.push(a);
while(!q.empty()){
node b=q.front();
q.pop();
for(int i=;i<;i++){
int xx=b.x+dir[i][];
int yy=b.y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=n){
if(dp[xx][yy]==||dp[xx][yy]>dp[b.x][b.y]+mat[xx][yy]){
node c;
c.x=xx,c.y=yy;
q.push(c);
dp[xx][yy]=dp[b.x][b.y]+mat[xx][yy];
}
}
}
}
} LL dfs(int x,int y)
{ if(x==n&&y==n) return ;
if(s[x][y]) return s[x][y];//在记忆化搜索中,就是利用已知的数据直接代入,
//避免重复计算,在此就是把数据保存在s中
for(int i=;i<;i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=n){
if(dp[xx][yy]<dp[x][y]){
s[x][y]+=dfs(xx,yy);
}
}
}
return s[x][y];
} int main()
{
while(~scanf("%d",&n)){ memset(dp,,sizeof(dp));
memset(s,,sizeof(s)); for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&mat[i][j]);
}
} bfs(); /*for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%d ",dp[i][j]);
}
puts("");
}*/
printf("%I64d\n",dfs(,));
}
return ;
}
HDU 1278的更多相关文章
- hdu 1278 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 51Nod 1278 相离的圆
51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- 17984 FFF团的怒火
17984 FFF团的怒火 该题有题解 时间限制:1000MS 内存限制:65535K提交次数:55 通过次数:3 收入:3 题型: 编程题 语言: G++;GCC;VC;JAVA Descri ...
- collection接口的实现:set,list,queue
在java.util包中提供了一些集合类,常用的有List.Set和Map类,其中List类和Set类继承了Collection接口.这些集合类又称为容器,长度是可变的,数组用来存放基本数据类型的数据 ...
- C#基础学习2
变量与数据类型!
- python工具之myql数据库操作
import pymysql import config ''' 1.0 简单封装 1.1 添加了insert_id属性,返回insert时返回的主键 1.2 添加了column属性,返回查询的col ...
- AJPFX辨析Java中运算符 ++ 和 += 的区别
我们都知道Java中 ++ 和 +=1 都是把数字增加一后,把值赋给左边,那二者有什么区别呢? i+=1 运行的底层Heap申请一个区域存放i,在数据区域开劈一个区域存放1,2个内存段被数据被送入到 ...
- calc() 计算CSS属性值
calc()是css3的一个新增的功能,用来指定元素的长度.比如说,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值.calc() ...
- SQL Server 查询锁表和接锁表
SQL Server 查询锁表 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) as tableNa ...
- Oracle | Java日期处理
public class Test{ public static void main (String args []){ j ...
- 再遇BGP
第一次遇到BGP,是在大学的课堂上,现在再次看到它,有种深深的无奈,我只记得它的名字,忘记了它的样子. 那么什么是BGP呢? 翻译过来就是边界网关协议,一个用来网络数据进行选路的路由协议,使用TCP协 ...
- CentOS 7下安装配置proftpd搭建ftp服务器
proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone.xinetd模式运 ...