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
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- Spring AOP(通知、连接点、切点、切面)
一.AOP术语 通知(Advice) 切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.5种通知类型: 前置通知(Before): ...
- 在linux的用户空间操作gpio
1. 使能linux内核选项CONFIG_GPIO_SYSFS CONFIG_GPIO_SYSFS=y 2. 测试方法 2.1 关注/sys/class/gpio下的文件 --export/unexp ...
- Docs-.NET-C#-指南-语言参考-预处理器指令:#pragma(C# 参考)
ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#pragma(C# 参考) 1.返回顶部 1. #pragma(C# 参考) 2015/07/20 #pragma 为编译器给 ...
- selenium grid用法
一,selenium grid是什么? selenium是一个用于UI自动化测试的工具. selenium grid是selenium家族中的三大组件的一员.selenium grid有两个版本,gr ...
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 转 zabbix 优化方法 以及数据库查询方法 两则
###########sample 1 https://www.cnblogs.com/hanshanxiaoheshang/p/10304672.html (不错) 如何从zabbix server ...
- ASP.NET(C#)图片加文字、图片水印,神啊,看看吧
ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...
- 【Leetcode_easy】1051. Height Checker
problem 1051. Height Checker solution class Solution { public: int heightChecker(vector<int>&a ...
- 清除 sql server 记住密码
引用:https://www.cnblogs.com/zengbin/p/4307013.html SQL Server 2005 Management Studio %AppData%\Micros ...
- 时空卷积网络TCN
1.写在前面 实验表明,RNN 在几乎所有的序列问题上都有良好表现,包括语音/文本识别.机器翻译.手写体识别.序列数据分析(预测)等. 在实际应用中,RNN 在内部设计上存在一个严重的问题:由于网络一 ...