好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = =

数据量很小,爆搜一下也可以过

额外注意的就是防止往回搜索需要做一个判断。

Source code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <bits/stdc++.h>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ; char mp [][];
int n,m;
int Dir [][] = {{,},{,},{-,},{,-}};
int Vis [][]; int Fit(int x , int y){
return x >= && x < n && y >= && y < m;
} int Dfs (int x, int y, int px, int py, char c){
Vis[x][y] = ;
for(int i = ; i < ; ++i){
int dx = x + Dir[i][];
int dy = y + Dir[i][];
if(dx == px && dy == py) continue;
if (Fit (dx , dy) && mp[dx][dy] == c){
if (Vis[dx][dy]){
return ;
}
if (Dfs (dx ,dy, x, y, mp[dx][dy])){
return ;
}
}
}
return ;
} int main(){
int i ,j ;
while(cin >> n >> m){
memset(Vis, , sizeof(Vis));
for(i = ; i < n; ++i){
for(j = ; j < m; ++j){
cin >> mp[i][j];
}
}
for(i = ; i < n; ++i){
for(j = ; j < m; ++j){
if(!Vis[i][j]){
if(Dfs(i, j, -, -, mp[i][j])){
cout << "Yes" << endl;
return ;
}
}
}
}
cout << "No" << endl;
}
return ;
}

Codeforces 510B Fox And Two Dots 【DFS】的更多相关文章

  1. CodeForces - 510B Fox And Two Dots (bfs或dfs)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

  3. Codeforces 445 A DZY Loves Chessboard【DFS】

    题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...

  4. Codeforces 475 B Strongly Connected City【DFS】

    题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...

  5. codeforces 750D New Year and Fireworks【DFS】

    题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进. 问在网格中,有多少网格至少被烟花经过一次? 题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方 ...

  6. 【dfs】codeforces Journey

    http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...

  7. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  8. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  9. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

随机推荐

  1. Python学习之字符串函数

    下面是在看python核心编程中序列字符串中提到的一些函数,根据自己的学习理解总结了下,方便日后用到的时候查看.    1.string.capitalize() 把字符串的第一个字符大写 例子:   ...

  2. jQuery validate和form插件配套使用

    参考 官网http://jqueryvalidation.org/documentation/ 博客http://www.cnblogs.com/buzzlight/archive/2010/06/3 ...

  3. ***EF中的问题(复习的同学可略过)

    1.当类中出现两个导航属性时,需使用额外代码说明类之间的关系. [ForeignKey("Id")] [InverseProperty("Id"]

  4. bootstrap读书笔记

    引入bootstrap.js或单个插件的js文件 若引入单个插件的js文件,注意插件之间的依赖关系 data属性api data属性的api很方便,但我们也可以选择关闭这个功能:$(document) ...

  5. 读书笔记: nodejs API 参考

    >> bufferBuffer对象是全局对象Buffer支持的编码方式:ascii, utf8, base64, binarynew Buffer(size)new Buffer(arra ...

  6. 自定义标签(TagSupport )

    转载:http://zhuhuide2004.iteye.com/blog/555737 这个图太好了,拿下来,标注一下:

  7. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  8. Three Swaps DFS

    E. Three Swaps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. [Leetcode]-containsNearbyDuplicate

    //题目: //给定一个整数数组与一个整数k,当且存在两个不同的下标i和j满足nums[i] = nums[j]而且| i - j | <= k时返回true.否则返回false. #inclu ...

  10. Objective-c 协议(protocol)

    协议的作用类似地C++中对抽象基类的多重继承.类似于Java中的接口(interface)的概念.   协议是多个类共享方法的列表,协议中列出的方法在本类中并没有相应实现,而是别的类来实现这些方法. ...