hdu 1242 不用标记数组的深搜
#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 不用标记数组的深搜的更多相关文章
- 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 ...
- HDU 2553 N皇后问题 (深搜)
题目链接 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对 ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Temper of the bone(深搜+剪枝)
Tempter of the Bone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- HDU 1312 Red and Black (深搜)
题目链接 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- PCA python 实现
PCA 实现: 参考博客:https://blog.csdn.net/u013719780/article/details/78352262 from __future__ import print_ ...
- B站在微服务治理中的探索与实践
https://mp.weixin.qq.com/s/_iFe8DO1e-QcYG-CJDTHpg
- Flutter 页面布局 Stack层叠组件
Stack 表示堆的意思,我们可以用 Stack 或者 Stack 结合 Align 或者 Stack 结合 Positiond 来实现页面的定位布局 属性 说明 alignment 配置所有子元素的 ...
- Java回调机制在RPC框架中的应用示例
完整源码: https://gitee.com/shiyanjun/x-callback-demo 应用场景描述: 服务提供者在项目启动时,创建并启动一个TCP服务器,然后将自己提供的所有服务注册到注 ...
- ISO/IEC 9899:2011 条款6.7.2——类型说明符
6.7.2 类型说明符 语法 1.type-specifier: void char short int long float double signed unsigned _Bool _Comple ...
- 深入学习c++--智能指针(二) weak_ptr(打破shared_ptr循环引用)
1. 几种智能指针 1. auto_ptr: c++11中推荐不使用他(放弃) 2. shared_ptr: 拥有共享对象所有权语义的智能指针 3. unique_ptr: 拥有独有对象所有权语义的智 ...
- Spring cloud微服务安全实战-6-1本章概述
这一章来讲一下,微服务之间的通讯安全. 当前这个架构还存在的问题 在网关上做限流还是有一些问题的.例如我的订单服务限流是100,库存服务限流也是100.但是我的订单服务会调用我的库存服务.那么在网关这 ...
- LeetCode_349. Intersection of Two Arrays
349. Intersection of Two Arrays Easy Given two arrays, write a function to compute their intersectio ...
- dockerfile中设置环境变量
设置环境变量要用 ENV ENV JAVA_HOME /usr/local/jdk1.8.0_171 ENV PATH $JAVA_HOME/bin:$PATH 用 echo "JAVA_H ...
- LODOP打印超文本字符串拼接2 单选选择css样式表格
之前的相关字符串拼接的博文:LODOP打印超文本字符串拼接1 固定表格填充数值之前博文介绍过,字符串可以随意拼接,只要最后组织成的字符串是自己需要的超文本就可以了,前面还有一篇也是拼接样式的:Lodo ...