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
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- you are not allowed to push code to protected branches on this project(转)
.. 图 1-1 报错:failed to push some refs to 'http://*******.git'. 一痛瞎踅摸之后,远程控制电脑,在H电脑上,重新建立了一个test项目,之后走 ...
- Dart运算符条件判断类型转换
/* 1.Dart运算符: 算术运算符 + - * / ~/ (取整) %(取余) 关系运算符 == != > < >= <= 逻辑运算符 ! && || 赋值 ...
- shell编程系列3--命令替换
shell编程系列3--命令替换 命令替换 命令替换总结 方法1 `command` 方法2 $(command) 例子1: 获取系统的所有用户并输出 for循环能以空格.换行.tab键作为分隔符 [ ...
- Linux下文件乱码问题
通常编码会在windows环境下进行,当把windows下的代码拷贝到linux环境时,会出现 error: “build.sh /bin/bash^M: 坏的解释器:没有那个文件或目录” 等等类似问 ...
- Qt QLabel加载图片
QLabel加载图片 //在对应的控件中显示图片 void qm_img::DisplayImg(cv::Mat imgParam, QLabel *labelParam) { if (!imgPar ...
- Linux系统调优——CPU(一)
(1).系统调优思路 性能优化就是找到系统处理中的瓶颈以及去除这些的过程,性能优化其实是对OS 各子系统达到一种平衡的定义.具体步骤如下: 1. 系统的运行状况: CPU -> MEM -& ...
- (二十三)IDEA 构建一个springboot工程,以及可能遇到的问题
一.下载安装intellij IEDA 需要破解 二.创建springboot工程 其他步骤省略,创建好的工程结构如下图: 三.配置springoboot工程 3.1 如上图src/main目录下只有 ...
- 【CSS3练习】transform 2d变形实例练习
transform 2d变形实例练习:练习了旋转 倾斜 缩放的功能 <!DOCTYPE html> <html lang="en"> <head> ...
- 有关_meta内容(持续更新)
假设在models里创建了一个类:UserInfo model.UserInfo._meta.app_label #获取该类所在app的app名称 model.UserInfo._meta.model ...
- [DFS]排队(间隔排列)-C++
Description 小Q是班长.在校运动会上,小Q班要进行队列表演.小Q要选出2*N名同学编队,每人都被编上一个号,每一个从1到N的自然数都被某2名同学佩戴,现在要求将他们排成一列,使两个编号为1 ...