#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. 到底啥是鸭子类型(duck typing)带简单例子

    #百度百科鸭子类型定义 这是程序设计中的一种类型推断风格,这种风格适用于动态语言(比如PHP.Python.Ruby.Typescript.Perl.Objective-C.Lua.Julia.Jav ...

  2. Github: 提升Github下载速度

    通过修改hosts文件来提速(该方法也可加速其他因为CDN被屏蔽导致访问慢的网站) 第一步:获取GitHub的IP地址 通过访问:http://tool.chinaz.com/dns或者https:/ ...

  3. OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)

    一. OpenGL的基本概念 OpenGL 的结构可以从逻辑上划分为下面 3 个部分: 图元(Primitives) 缓冲区(Buffers) 光栅化(Rasterize) 图元(Primitives ...

  4. memcpy字节序问题

    /* memcpy用法详解 */ #include <stdio.h> #include <stdlib.h> #include <string.h> //memc ...

  5. 123457123456#4#----com.MC.mathGame246----前拼后广--数学Q版训练Game-mc4444

    com.MC.mathGame246----前拼后广--数学Q版训练Game-mc

  6. 【linux学习笔记五】帮助命令

    man //查看ls作用 man ls man -f命令 相当于 whatis命令 --help ls --help help help shell help cd info详细命令帮助

  7. 09点睛Spring4.1-AOP

    9.1 AOP AOP可以了让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,这样使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上; 下面演示一个日志系统的实现,简单但不 ...

  8. [BJOI2019] 删数 [dp转贪心结论+线段树]

    题面 传送门 思路 dp部分 以下称合法序列为原题面中可以删空的序列 这个是我在模拟考场上的思路 一开始我是觉得,这个首先可以写成一个dp的形式:$dp[i][j]$表示用$j$个数字填满了目标序列的 ...

  9. webpack config to use plugin and webpack-dev-server

    Demo3操作手册 本Demo演示如何配合各种plugin进行偏复杂的使用 准备环境 初始化环境, cd到demo1目录之后, 执行如下命令: npm init -y npm install webp ...

  10. 【转帖】DevOps和SRE的区别

    DevOps和SRE的区别 https://zhuanlan.zhihu.com/p/87598465 DevOps 和 SRE 最近有一位朋友和我聊职业发展方向问题,聊了不少 DevOps 和 SR ...