这道题因为某些位置要重复走,所以不能用标记的方法,但是为了提高效率,可以采用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. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

  2. 手动构建Servlet项目的流程

    前面讨论过手动建立jsp的项目,jsp是tomcat服务器负责编译执行,所以配置相对简单,而Servlet需要先把java源文件编译成字节码class文件,然后再执行,所以需要servlet-api. ...

  3. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. IOS多线程(NSOperation,NSOperationQueue)

    含义:NSOperation,NSOperationQueue是什么. The NSOperation class is an abstract class you use to encapsulat ...

  5. fedora yum 使用代理的方法

    配置yum: 编辑/etc/yum.conf添加下列一行: proxy=http://domain/user:passwd@<proxy ip>:80 <proxy ip>:代 ...

  6. IDE整理

    1.eclipse 下载地址:http://www.eclipse.org/downloads/     2.myeclipse 下载地址:http://www.myeclipseide.com/mo ...

  7. Android之XML序列化和解析

    XML文件是一种常用的文件格式,可以用来存储与传递数据 ,本文是XML文件序列化与解析的一个简单示例 写文件到本地,并用XML格式存储 /** * 写xml文件到本地 */ private void ...

  8. wp7 BaseDictionary<TKey, TValue>

    /// <summary>/// Represents a dictionary mapping keys to values./// </summary>/// /// &l ...

  9. php 解决和避免form表单重复提交的方法

    在提交表单的时候,可能遇到网速等导致页面突然加载变慢,用户重复地点击提交按钮,将在数据库产生多条数据,导致不可控情况. 比如下面的情况就会导致表单重复提交: 点击提交按钮两次. 点击刷新按钮. 使用浏 ...

  10. Centos 6.4 32位 gcc 升级(已验证)

    具体需要升级成什么版本自行下载https://gcc.gnu.org/ 本文升级为4.8.5 1.下载编译所需依赖库 cd gcc-4.8.5 ./contrib/download_prerequis ...