#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. flutter Form表单

    import 'package:flutter/material.dart'; class FormDemo extends StatelessWidget { @override Widget bu ...

  2. iOS App Store网址的命名规则

    App Store 命名规则:https://itunes.apple.com/cn/app/ +  拼音(最多6个,全部小写,空格要-) +  /id  +appID 一些参考实例: http:// ...

  3. Swift4.0复习整数,浮点数,布尔值

    1.类型相互转换: Int(a) Float(b) let a = Bool(truncating: NSNumber(value: c)) 2.元组: let tuple: (Int, String ...

  4. 如何关闭phpstrom的更新提醒?

    在file-----setting-------搜索updates 把检测版本更新的对勾点掉就可以了, 自己破解后的版本就别更新了,更新后就不能再用了,目前用代理服务器激活可以用版本3.3,升级到3. ...

  5. Java基础教程:多线程杂谈——Volatile

    Java基础教程:多线程杂谈——Volatile 引入Volatile Java语言提供了一种稍弱的同步机制,即Volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为Volati ...

  6. ibatis 参数 指定类型

    文档: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s04.html <update id="UpdateAccountVi ...

  7. 14、OpenCV实现图像的空间滤波——图像锐化及边缘检测

    1.图像锐化理论基础 1.锐化的概念 图像锐化的目的是使模糊的图像变得清晰起来,主要用于增强图像的灰度跳变部分,这一点与图像平滑对灰度跳变的抑制正好相反.而且从算子可以看出来,平滑是基于对图像领域的加 ...

  8. Docker快速安装

    目前装Docker得最简单方式就是脚本安装了,方法如下: curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh 安装后 ...

  9. 【C/C++开发】字符串操作

    1)字符串操作 strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat(p, p1, n) 附加指 ...

  10. 推荐linux运维必备的几本书

    首先,<鸟哥的linux私房菜> 鸟哥 其次,<linux就该这么学> 刘瑞版 然后,<CentOS linux系统运维> 张祥琳版 最后,<CentOS运维 ...