这道题因为某些位置要重复走,所以不能用标记的方法,但是为了提高效率,可以采用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. VIM中的折叠命令

    参考:http://blog.csdn.net/bruce0532/article/details/8497284 za:在折叠与展开间自由转换 zf:创建折叠 示例:zf 3j    #本行及以下3 ...

  2. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

  3. DP:Apple Catching(POJ 2385)

    牛如何吃苹果 问题大意:一个叫Bessie的牛,可以吃苹果,然后有两棵树,树上苹果每分钟会掉一个,这只牛一分钟可以在两棵树中往返吃苹果(且不吃地上的),然后折返只能是有限次W,问你这只叫Bessie的 ...

  4. July 26th, Week 31st Tuesday, 2016

    The best preparation for tomorrow is doing your best today. 对明天最好的准备就是今天做到最好. The road toward tomorr ...

  5. sublime 实用 快捷键

    alt+- 向后导航 alt+shift+- 向前导航 ctrl+shift+↑↓ 上下移动一行 ctrl+k,ctrl+u 转换所选为大写 ctrl+k,ctrl+l(字母L) 转换所选为小写 ct ...

  6. adbWireless 简单教程

    adbWireless:无线使用ADB(siir.es.adbWireless) 下载: http://www.coolapk.com/apk/siir.es.adbWireless >> ...

  7. WPF常用方法,事件驱动和控件遍历

    //初始化数据,默认选中第一项,事件驱动 RadioButton btn = FuncClass.GetChildObject<RadioButton>(this.stackPanel1, ...

  8. 建模算法(六)——神经网络模型

    (一)神经网络简介 主要是利用计算机的计算能力,对大量的样本进行拟合,最终得到一个我们想要的结果,结果通过0-1编码,这样就OK啦 (二)人工神经网络模型 一.基本单元的三个基本要素 1.一组连接(输 ...

  9. 【转】HBase 超详细介绍

    ---恢复内容开始--- http://blog.csdn.net/frankiewang008/article/details/41965543 1-HBase的安装 HBase是什么? HBase ...

  10. 关于flume中的几个疑惑

    文章发自http://www.cnblogs.com/hark0623/p/4205756.html  转载请注明 flume越用越有一些疑惑,这个月中按计划是要阅读flume源码的,我希望能解决我的 ...