dfs模版
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模版的更多相关文章
- DFS 算法总结
DFS 算法总结 这篇文章会对DFS进行一个总结,列举的题目则是从LeetCode上面选的: 适用场景: 有三个方面,分别是输入数据.状态转换图.求解目标: 输入数据:如果是递归数据结构,如单链表,二 ...
- [LeetCode 题解]: Permutation Sequcence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode 题解]: Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 尼姆博弈+SG函数
博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...
- How far away ?(LCA)dfs和倍增模版
How far away ? Tarjan http://www.cnblogs.com/caiyishuai/p/8572859.html Time Limit: 2000/1000 MS (Jav ...
- PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
随机推荐
- [原]openstack-kilo--issue(二十二) 虚拟机的vnc console图像调用错误
[问题点] 在打开node compute 上vm的vnc console窗口时候发现vm1-compute1调用的是vm1-controller上的vnc图像 =================== ...
- SQL Server 2012 R2升级到SQL Server 2016无法正常启动数据库服务解决方案
原定周末把公司的TFS升级到2018,由于TFS 2018需要SQL Server至少是2016以上版本,所以还需要将原来的SQL Server 2012 R2一并升级.今天早上负责的同事告诉我升级失 ...
- mysql的安装和配置
1.mydql的安装 重装wind7系统之后,mysql软件自动卸载了.现在学习需要使用mysql,还是要安装mysql.我首先通过下载安装包的方式安装,按照教程,但是出现缺失.dll文件,我下载安装 ...
- day_4_27 py
''' 2018-4-27 19:57:29 其实这些都是讲的类和对象的 self(在定义方法的时候默认的参数)就相当于java里面的this关键字, this.name=name class 类名: ...
- I - Crossing River
A group of N people wishes to go across a river with only one boat, which can at most carry two pers ...
- Linux 下文件
Linux下文件的三个时间(Atime,Mtime,Ctime) [root@node ~]# stat install.log File: `install.log' Size: Blocks: ...
- POJ 2442 - Sequence - [小顶堆][优先队列]
题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...
- [No0000154]详解为什么32位系统只能用4G内存.
既然是详解, 就从最基础的讲起了. 或者1来存储数据的, 所以Bit实际上可以看成存放1个二进制数字的1个位置.也就是说bit只有2种值, 0 或者 1, 所以1个bit能存放1个布尔类型的值(boo ...
- CSS:元素类型
元素分类 在CSS中,html中的标签元素大体分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 块状元素 <div>.<p>.<h1>...& ...
- app相关的一些网站
https://www.qimai.cn 排名排行关键字原aso100