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
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- Kafka的Topic、Partition和Message
Kafka的Topic和Partition Topic Topic是Kafka数据写入操作的基本单元,可以指定副本 一个Topic包含一个或多个Partition,建Topic的时候可以手动指定Par ...
- idea 如何新建一个Maven项目并且写第一个servlet
使用idea已经有段时间了,但是一直没有自己亲自新建一个项目,从头开始写一个Servlet,今天就来学习一下,并且记一个笔记. 一. 1.首先,打开idea new-->Project 2.选择 ...
- android: Android水波纹点击效果
Android API 21及以上新增了ripple标签用来实现水波纹的效果.我们可以通过设置ripple背景来实现一些View点击效果. 该水波纹效果有两种:一种是有界的(点击后类似于一个矩形向四周 ...
- DataSource接口 Connection pooling(连接池
一.DataSource接口是一个更好的连接数据源的方法: JDBC1.0是原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource的实 ...
- 2018 遇到selenium.common.exceptions.WebDriverException问题
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PAT ...
- maven项目中,添加依赖后,出现"Dependency 'xxxx‘ not found"解决过程
转自:https://blog.csdn.net/lixld/article/details/82284269 idea中修改pom.xml文件,添加各种工程依赖的jar,一直没有问题, 但今天遇到问 ...
- Flutter修改应用图标及图标
应用名称是需要在 Android 和 iOS 中分别调整的:Android 是在 AndroidManifest.xml 中修改:iOS 则是在 Info.plist 中修改的: 如果需要更改应用的 ...
- Sqoop2 将hdfs中的数据导出到MySQL
1.进入sqoop2终端: [root@master /]# sqoop2 2.为客户端配置服务器: sqoop:000> set server --host master --port 120 ...
- 【linux学习笔记三】链接命令
链接命令:ln link =============华丽的分割线============= ln又有软链接和硬链接 //硬链接特征(不建议创建硬链接) 1.拥有相同的i节点和存储block块,可以看做 ...
- The underlying connection was closed: The connection was closed unexpectedly.
基础连接已经关闭: 连接被意外关闭. 基础连接已经关闭: 发送时发生错误 防火墙问题.或是杀毒软件,卫士之类的.(360 卸载 )