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. Unity3D Shader水波效果

    水波效果 Shader "Custom/WaterWave" { Properties { _MainTex ("Base (RGB)", 2D) = &quo ...

  2. 【ReviewBoard】安装与配置

    0. 引言 环境:Ubuntu 14.04 Server(虚拟机) 这篇文章里说的是review board官方的安装方式,bitnami出了针对win/linux的集成安装包,用它可能简单点,没有尝 ...

  3. Scriter CSS

    transition: height(quart-out,1.0s,quart-in); transform:rotate(50deg); http://www.terrainformatica.co ...

  4. day_5.28 py网络编程

    端口 socket简介: socket为一个类   s接收的是返回的对象引用 2018-5-28 15:52:47 开始进行网络编程 udp 套接字 encode() 编码 decode() 解码 ' ...

  5. es7 class装饰器

    文档http://es6.ruanyifeng.com/#docs/decorator ts文档 https://www.tslang.cn/docs/handbook/decorators.html ...

  6. 拓展 NLog 优雅的输送日志到 Logstash

    在上上篇博客通过对aspnetcore启动前配置做了一些更改,以及对nlog进行了自定义字段,可以把请求记录输送到mysql,正式情况可能不会这么部署.因为近期也在学习elk,所以就打算做一个实例,结 ...

  7. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  8. C常用的字符串函数实现

    /** 查找字符串 source 中 是否有指定的子串出现,如果有返回第一个匹配的字符 @param source 源 @param chars 目标 @return 返回值 */ char *fin ...

  9. ArcEngine二次开发,TOCControl控件上使用contextMenuStrip

    右键菜单,在二次开发中很实用,以前没用过,最近通过一本书了解到,一直想找这么一个控件来用. 一般的控件,将contextMenuStrip控件拖到所依托的控件上,然后输入自己想要的几个功能.  在所依 ...

  10. SHOW PROCESSLIST shows which threads are running 查看线程 解决瓶颈

    小结: 1.查看全部线程: https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html MySQL 8.0 Reference Manu ...