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模版,布布扣,bubuko.com

时间: 2024-10-12 21:27:32

dfs模版的相关文章

LeetCode: Permutation Sequcence 题解

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

Combination Sum_DFS

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will

[LeetCode] 547. Friend Circles 朋友圈

本题可以套用和 [LeetCode] 200. Number of Islands 岛屿的数量 与 [LeetCode 695] Max Area of Island 岛的最大面积一样的dfs模版,不同的是DFS的遍历路径不同,本题 采用的是类似十字遍历方式,具体见如下代码,本题同样可以使用BFS和并查集的方法解决,这里暂不讨论: 1 /* 2 * @Descripttion: 3 * @version: 4 * @Author: wangxf 5 * @Date: 2019-12-20 15:

Dancing Links [Kuangbin带你飞] 模版及题解

学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/7627862 2份模版 第一份精确覆盖 from POJ 3074 const int N = 9; const int MAXN = N * N * N + 10; const int MAXM = N * N * 4 + 10; const int MAXNODE = MAXN * 4 + MAXM +

HDU2389 Rain on your Parade(HK模版)

题意: 在一个二维坐标系上有n个人和m把伞,每个人都有自己的移动速度, 问有多少人可以在s min内移动到不同的雨伞处(不允许两个人共用一把伞). 思路: 由于nm太大(3000),匈牙利会超时,就用HK算法 整理了HK的模版 /* *********************************************** //Author :devil //Created Time :2016/5/10 23:13:51 //********************************

二分图、网络流模版总结

这个星期重新回去理解二分图和网络流的算法,觉得当初真的是有点傻萌,都不太了解就直接套模版…… 关于二分图 关于二分图的概念不想吐槽了,太多而且好多都好乱,看了很多个什么概念总结结果发现有的人把最大匹配叫最佳匹配.反正是看的我一脸蛋疼,决定还是抛开概念吧,看着像啥用啥得了. 匈牙利算法: 其实就是不断寻找增广路.那么什么是增广路呢? 既然是二分图,我们先定义X集和Y集.然后我们对X集里面的点一个一个来,拿出一个没用过的X集的点(简记为i),找所有和它相连的Y集的点(简记为j),如果这个Y集的点没用

网络流模版

Dinic递归版 struct Edge { int from, to, cap, flow; Edge(){} Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow){} }; int n, m, s, t; vector <Edge> edges; vector <int> G[maxn]; bool vis[maxn]; int d[maxn]; int cur[

树链剖分 模版

树链剖分 模版 #include<bits/stdc++.h> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; const int maxn=1000100; const int INF=(1<<29); int n; char op[20]; int u,v,w; vector<int> G[maxn]; int dep[maxn],son[maxn]

poj 1986 Distance Queries 带权lca 模版题

Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the