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 ...
随机推荐
- subordinate clause/从句
What Is a Subordinate Clause? (with Examples)A subordinate clause (or dependent clause) is a clause ...
- 搞懂MapReduce
MapReduce的主要思想就是将计算任务分发至多台计算机(slave),然后master综合计算机结果.所以就涉及到多台计算机通信和同步的问题,这个应该由hadoop完成,把环境配置好后就像单机操作 ...
- Linux之vmware安装
环境准备 mac 用 vmware fusion 虚拟机 和 iterm2 远程连接工具, ...
- [Asp.net]绝对路径和相对路径
目录 绝对路径 相对路径 总结 绝对路径 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径.比如:E:\新概念英语\新版新概念英语第二册课文PDF.pdf.以Web 站点根目录为参考基础的目录路径 ...
- Android开发-基本概念(申明:来源于网络)
Android开发-基本概念(申明:来源于网络) 地址:http://blog.csdn.net/iwanghang/article/details/53505926
- 将多个图片合并到一个TIF文件里(非 GDAL) 优化版
不知道为什么,网上对TIF的操作的资料少得可怜,包括CodeProject上都没有找到多少,在网上大多用GDAL,但这个东西,对只想做个合并图片的功能来说,实在是牛刀杀鸡,(9个DLL要带全,相当的恐 ...
- WPF .NET 4.0 OpenClipboard 失败 (异常来自 HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)) BUG解决
在项目 App.xaml 文件下 => Application 节点=> 添加 DispatcherUnhandledException="Application_Dispa ...
- ASP.NET 前端Ajax获取数据并刷新
控制器中↓ /// <summary> /// 根据ID来进行展示数据 /// </summary> /// <param name="instru_id&qu ...
- [No0000BB]ReSharper操作指南4/16-配置ReSharper代码快修与导航
代码问题的快速修复 ReSharper可以帮助您立即修复设计时检测到的大部分代码问题.就像按Alt+Enter突出显示的代码问题一样简单,并选择合适的方法来解决问题或改进次优代码. GIF 应用快速修 ...
- hadoop内存配置方案
Configuration File Configuration Setting Value Calculation 8G VM (4G For MR) yarn-site.xml ...