dfs

#include <stdio.h>

#include <string.h>

char Map[16][16];

int mv[16][16];

//mv[i][j] == 0 没有被访问

//mv[i][j] == 1 已经被访问

struct N

{

int x,y;

} ;

int jx[] = { 0,-1, 0, 1};

int jy[] = { 1, 0,-1, 0};

int Min;

void dfs(int x,int y,int n,int m,int ans)

{

if(ans >= Min)

{

return ;

}

if(Map[x][y] == 'Y')

{

if(ans < Min)

{

Min = ans;

}

return ;

}

N f;

for(int i = 0; i < 4; ++i)

{

f.x = x + jx[i];

f.y = y + jy[i];

if(0 <= f.x && f.x < n && 0 <= f.y && f.y < m && mv[f.x][f.y] == 0 && Map[f.x][f.y] != '#')

{

mv[f.x][f.y] = 1;

dfs(f.x,f.y,n,m,ans+1);

mv[f.x][f.y] = 0;

}

}

}

int main()

{

int n,m,i,j;

while(scanf("%d %d",&n,&m) != EOF)

{

memset(mv,0,sizeof(mv));

for(i = 0; i < n; ++i)

{

scanf("%*c%s",Map[i]);

}

for(i = 0; i < n; ++i)

{

for(j = 0; j < m; ++j)

{

if(Map[i][j] == 'X')

break;

}

if(j != m)

break;

}

Min = (1<<20);

dfs(i,j,n,m,0);

if(Min == (1<<20))

{

printf("-1\n");

}

else

{

printf("%d\n",Min);

}

}

return 0;

}

dfs模版的更多相关文章

  1. DFS 算法总结

    DFS 算法总结 这篇文章会对DFS进行一个总结,列举的题目则是从LeetCode上面选的: 适用场景: 有三个方面,分别是输入数据.状态转换图.求解目标: 输入数据:如果是递归数据结构,如单链表,二 ...

  2. [LeetCode 题解]: Permutation Sequcence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [LeetCode 题解]: Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. 尼姆博弈+SG函数

    博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...

  5. How far away ?(LCA)dfs和倍增模版

    How far away ? Tarjan http://www.cnblogs.com/caiyishuai/p/8572859.html Time Limit: 2000/1000 MS (Jav ...

  6. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  7. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  8. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  9. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

随机推荐

  1. element的form表单中如何一行显示多el-form-item标签

    效果图: HTML代码: <script src="//unpkg.com/vue/dist/vue.js"></script> <script sr ...

  2. iOS - 开发中加载本地word/pdf文档说明

    最近项目中要加载一个本地的word/pdf等文件比如<用户隐私政策><用户注册说明>,有两种方法加载 > 用QLPreviewController控制器实现 步骤 : & ...

  3. 180623、Git新建远程分支和删除

    Git新建远程分支和删除 现在我在master分支上,工作目标是干净的,也没有需要commit的: $ git branch * master release $ git status On bran ...

  4. Flask----目录结构

    以此结构为例,这个小项目是<Flask Web开发:基于python的web应用开发实战>第一部分结束后的代码框架 第一层 有app.tests.migrations三个文件夹和confi ...

  5. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  6. [No0000FC]C# 预处理器指令

    预处理器指令指导编译器在实际编译开始之前对信息进行预处理. 所有的预处理器指令都是以 # 开始.且在一行上,只有空白字符可以出现在预处理器指令之前.预处理器指令不是语句,所以它们不以分号(;)结束. ...

  7. 远程登录oracle 12.2数据库报错ORA-28040解决办法

    今天新安装的oracle 12.2.0.1数据库,通过本地sqlplus远程登录12c数据库报错ora-28040,如下: ORA-28040: No matching authentication ...

  8. PHP之后期静态绑定

    PHP后期静态绑定的(late static bindings) 理解PHP延迟静态绑定 static::中的static其实是运行时所在类的别名,并不是定义类时所在的那个类名.这个东西可以实现在父类 ...

  9. php测试for/while/foreach循环速度对比

    对比代码先行贴上,有疑问或者有不同见解的希望可以提出,大家共同进步: //-------------------------------------$k=0;$checkTime = ['for'=& ...

  10. js实现数字千分位

    /** * * @param num * @param precision * @param separator * @returns {*} *=========================== ...