这道题因为某些位置要重复走,所以不能用标记的方法,但是为了提高效率,可以采用time[]数组和step[]数组来剪枝,很容易想到,当你从一条路劲走到(x,y)处的时间和步骤

比从另一条路劲走到(x,y)处的时间和步骤小时,那么time[]数组和step[]数组将这个最小的时间和步骤记录下来,即time[]和step[]记录的是到达每个位置用的最小的时间和步骤,如果当你从某一条路径到达(x,y)处的时间和步骤大于已记录值时,直接返回而不继续走下去。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits> const int MAX = ; int dir[][]={,,-,,,,,-}; int map[MAX][MAX],step[MAX][MAX],time[MAX][MAX];
int n,m,sx,sy,dx,dy,minx; void dfs(int x,int y,int len,int cnt){
if(x< || y< || x>=n || y>=m)return;
if(len<= || cnt>=minx)return;
if(map[x][y]==)return;
if(map[x][y]==){
if(cnt<minx)minx=cnt;
return;
}
if(map[x][y]==){
len=;
}
//下面的这个剪枝很重要,不剪就会超时
//从当前点x,y走到下一个可能点的距离大于从其他途径到tx,ty的距离,且到tx,ty点时的剩余时间大于由x,y点到tx,ty点后的剩余时间,就跳过
//这是因为结点可重复访问所以本身没标记,那么当上述条件满足时,由tx,ty开始的最优解已经求过(存在或者不存在),所以不需要再重复求了。
if(cnt>=step[x][y] && time[x][y]>=len)return;
step[x][y]=cnt;
time[x][y]=len;
int tx,ty,i;
for(i=;i<;++i){
tx = x+dir[i][];
ty = y+dir[i][];
dfs(tx,ty,len-,cnt+);
}
} int main(){ //freopen("in.txt","r",stdin);
int t,i,j,len,cnt;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(i=;i<n;++i){
for(j=;j<m;++j){
time[i][j]=;
step[i][j]=INT_MAX-;//这里置一个大数,表示到i,j的步数无限大
scanf("%d",&map[i][j]);
if(map[i][j]==){
sx = i;
sy = j;
}else if(map[i][j]==){
dx = i;
dy = j;
}
}
}
len = ;
cnt = ;
minx = INT_MAX;
dfs(sx,sy,len,cnt);
if(minx==INT_MAX){
printf("-1\n");
}else{
printf("%d\n",minx);
}
} return ;
}

hdu的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

  10. HDU 2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...

随机推荐

  1. MyBatis3: Could not find SQL statement to include with refid ‘

    错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...

  2. html标签属性大全

    <marquee>...</marquee>普通卷动 <marquee behavior=slide>...</marquee>滑动 <marqu ...

  3. Android studio 添加依赖

    以前添加依赖总是到github上下载源码,再添加源码到module的依赖当中,其实在studio中,应该使用maven库. 比如在github上看到了sliding-menu这个项目,就应该到mave ...

  4. CHM文档打开空白的解决

    网上打包的CHM格式的文档,有时候打开无论点击目录哪一章节都会出现一片空白或者显示已取消到该网页的导航 这个情况的原因就是CHM文件在Windows的HTFS文件系统中会默认被阻止显示,解决方法就是在 ...

  5. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. JS Replace 全部替换字符 用法

    转载自:http://www.cnblogs.com/skykang/archive/2011/08/04/2127158.html <script language="javascr ...

  7. 项目总结(四)--- 网络封包分析工具Charles

    Charles是Mac下一款截取网络封包的工具,主要原理就是将自己设置成为熊网络访问的代理服务器,这样的话,所有的网络请求都得通过它来完成,从而实现网络封包的拦截分析. 这款软件功能整体来说还是非常强 ...

  8. MySQL数据库的主从同步实现及应用

    >>主从同步机制及应用 读写分离(Read/Write Splitting)让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELETE),从数据库处理SELECT查询操作 ...

  9. php动态安装mongo扩展

    首先下载mongo扩展包  http://pecl.php.net/package/mongo 开始安装把 wget http://pecl.php.net/get/mongo-1.5.8.tgz t ...

  10. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...