Codeforces 510B Fox And Two Dots 【DFS】
好久好久,都没有写过搜索了,看了下最近在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】的更多相关文章
- 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 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- Codeforces 445 A DZY Loves Chessboard【DFS】
题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...
- Codeforces 475 B Strongly Connected City【DFS】
题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...
- codeforces 750D New Year and Fireworks【DFS】
题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进. 问在网格中,有多少网格至少被烟花经过一次? 题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方 ...
- 【dfs】codeforces Journey
http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...
- 【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
随机推荐
- mybatis获取插入的语句主键(自增主键)
<insert id="insertUser" parameterType="User"> <selectKey keyProperty=&q ...
- 帝国cms修改[!--show.listpage--]分页页码所生成的html标签
在使用帝国cms系统时,我们用[!--show.page--]和[!--show.listpage--]来生成页码 其中[!--show.listpage--]所生成的html页码代码为: <a ...
- 27_Blog Reader
这个App是用来读取 Official Google Blog 的内容,然后显示出来. 用了新建工程时用了 Master-Detail Application 这个模板.用了Core Data用来存储 ...
- python OptionParser模块
Python中强大的选项处理模块. #!/usr/bin/python from optparse import OptionParser parser = OptionParser() parser ...
- 命令查询分离原则Command-query separation principle
在UML和模式应用一书中,发送给Die的roll消息之后跟随着第二个消息getFaceValue用于提取其新的faceValue,特别是:roll()方法是void的,没有返回值,例如: public ...
- mget 同时获取
mget 同时获取: http://192.168.32.81:9200/ _mget POST { "docs" :[ { "_index":"li ...
- AlertDialog弹出时背景明暗程度调整
今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...
- hdoj 1532 Drainage Ditches(最大网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路分析:问题为最大网络流问题,给定一个有向图,需要求解该有向图的最大网络流,使用Edmonds ...
- iOS开发--汉字转成没有声调也没有空格的拼音
//汉字转成没有声调也没有空格的拼音- (NSString *)transformToPinYin:(NSString *)wordStr { NSMutableString *mutable ...
- STL之vector(向量)
C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的,这里详细介绍一下C++ Vector的相关应用技巧及基本内容: Construct vector #include ...