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
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- Statement.setQueryTimeout(seconds)在家中环境的再次试验 证明此语句还是有效的
对比实验:https://www.cnblogs.com/xiandedanteng/p/11955887.html 这次实验的环境是T440p上安装的Windows版Oracle11g,版本为: O ...
- python 设计模式之桥接模式 Bridge Pattern
#写在前面 前面写了那么设计模式了,有没有觉得有些模式之间很类似,甚至感觉作用重叠了,模式并不是完全隔离和独立的,有的模式内部其实用到了其他模式的技术,但是又有自己的创新点,如果一味地认为每个模式都是 ...
- Nessus更新到8.6.0
Nessus更新到8.6.0 此次更新,变化主要有以下几点:(1)加强过期提醒.购买链接,并且允许用户启用关闭提醒功能.(2)Nessus基础班和试用版中,导出的报告默认带有水印.(3)Nessus企 ...
- [转]EXCEL截取字符串中某几位的函数——LeftMIDRight及Find函数的使用
原文地址:http://blog.sina.com.cn/s/blog_3f136a180102ymq5.html EXCEL截取字符串中某几位的函数 ——Left MID Right及Find函数的 ...
- vs2017在前端页面使用F12无法转到js脚本函数定义
这样设置后就可以正常使用了
- 算法习题---5-5复合词(UVa10391)
一:题目 输入一系列由小写字母组成的单词.输入已按照字典序排序,且不超过120000个.找出所有的复合词,即恰好由两个单词连接而成的单词 (一)样例输入 a alien born less lien ...
- [Scikit-learn] 1.4 Support Vector Regression
SVM算法 既可用于回归问题,比如SVR(Support Vector Regression,支持向量回归) 也可以用于分类问题,比如SVC(Support Vector Classification ...
- jQuery根据style筛选元素
<div style="display:block;"> <input/> </div> <div style="display ...
- 【2B设计】途牛商旅全案设计
整体设计方案: 途牛商旅全案设计 https://www.uisdc.com/tuniu-business-travel
- 文件描述符FD的含义/文件句柄
使用sudo lsof -nP -iTCP -sTCP:LISTEN查看占用端口的程序;因为 lsof 需要访问核心内存和各种文件,所以必须以 root 用户的身份运行它才能够充分地发挥其功能 概念 ...