#include<stdio.h>
#include<string.h>
char mapp[220][220];
int m,n,mmin;
void dfs(int x,int y,int time){
  if(mapp[x][y]=='#'){
   return ;
  }
  if(x<0||x>=m||y<0||y>=n){
   return ;
  }
  if(mapp[x][y]=='a'){
         dfs(x,y+1,0);
         dfs(x,y-1,0);
         dfs(x+1,y,0);
         dfs(x-1,y,0);
     }
  if(mapp[x][y]=='r'){
   if(mmin>time){
    mmin=time;
   }
  }
  if(mapp[x][y]=='x'){
   mapp[x][y]='#';
   dfs(x+1,y,time+2);
   dfs(x-1,y,time+2);
   dfs(x,y+1,time+2);
   dfs(x,y-1,time+2);
   mapp[x][y]='x';
  }
  if(mapp[x][y]=='.'){
   mapp[x][y]='#';
   dfs(x+1,y,time+1);
   dfs(x-1,y,time+1);
   dfs(x,y+1,time+1);
   dfs(x,y-1,time+1);
   mapp[x][y]='.';
  }
}
int main(){
 int i,j;
 while(scanf("%d %d",&m,&n)!=EOF){
  for(i=0;i<m;i++){
   scanf("%s",mapp[i]);
  }
  mmin=99999;
  for(i=0;i<m;i++){
   for(j=0;j<n;j++){
    if(mapp[i][j]=='a'){
     dfs(i,j,0);
    }
   }
  }
  if(mmin!=99999){
   printf("%d\n",mmin+1);
  }
  else{
   printf("Poor ANGEL has to stay in the prison all his life.\n");
  } 
 }
 return 0;
}

这道题目用正常的深搜去做的话会超时。。。  在讨论区看到别人这样用  挺有意思的。。

通过改变mapp的值来代替标记 回溯的过程    有意思。。

还有 对于大规模的输入  最好用scanf。。。。。。 不要轻易的使用scanf("%c")..  特别是有回车符的时候。。。  很危险。。。。

hdu 1242 不用标记数组的深搜的更多相关文章

  1. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

  2. HDU 2553 N皇后问题 (深搜)

    题目链接 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对 ...

  3. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 1010 Tempter of the Bone 深搜+剪枝

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. HDU 1010 Temper of the bone(深搜+剪枝)

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  6. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

  7. HDU 1312 Red and Black (深搜)

    题目链接 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore ...

  8. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  9. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

随机推荐

  1. Dynamic Filter Networks

    Dynamic Filter Networks 2019-06-10 11:29:19 Paper:http://papers.nips.cc/paper/6578-dynamic-filter-ne ...

  2. 小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_6、SpringBoot2.xHTTP请求配置讲解

    1.SpringBoot2.xHTTP请求配置讲解 简介:SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧 1.@RestController and @RequestMapping是 ...

  3. Kotlin集合——Set集合

    Kotlin集合——Set集合 转 https://www.jianshu.com/p/3c95d7729d69   Kotlin的集合类由两个接口派生:Collection和Map. Kotlin的 ...

  4. shell编程系列17--文本处理三剑客之awk动作中的表达式用法

    shell编程系列17--文本处理三剑客之awk动作中的表达式用法 awk动作表达式中的算数运算符 awk动作中的表达式用法总结: 运算符 含义 + 加 - 减 * 乘 / 除 % 模 ^或** 乘方 ...

  5. Spring cloud微服务安全实战-3-14章节小结

    认证:一个httpBasic 一个是用户名密码的认证 授权:数据库内判断的r就是读 w就是写 ,ACL 获取用户信息的这段逻辑,实际上着也是安全机制的一种.防止越权,当前只能看到自己的信息. Spri ...

  6. Python json序列化时default/object_hook指定函数处理

    在Python中,json.dumps函数接受参数default用于指定一个函数,该函数能够把自定义类型的对象转换成可序列化的基本类型.json.loads函数接受参数objec_thook用于指定函 ...

  7. MongoDB学习笔记一:MongoDB基础

    目录 MongoDB是什么? 学了有什么用? MongoDB入门 安装 修改数据库位置 MongoDB的启动方式 MongoDB的图形化工具 MongoDB基本命令 增 查询 更新 删除 排序 投影 ...

  8. Swift4.0复习协议

    1.协议的定义: /// 定义一个协议MyProt protocol MyProt {   /// 声明了一个实例方法foo, /// 其类型为:() -> Void func foo()   ...

  9. keepalived通过飘移ip实现高可用配置步骤

    环境:两台虚拟机即可 centos7.3虚拟机A 10.0.3.46 centos7.3虚拟机B 10.0.3.110 对外开放的虚拟ip 10.0.3.96(这个ip只需要在keepalived里面 ...

  10. BASH输出着色显示

    通过将其输出着色,可以使BASH脚本更漂亮.使用ANSI转义序列设置文本属性,例如前景色和背景色. 使用以下模板格式来编写彩色文本: echo -e "\e[前景色值;背景色值;2m文本\e ...