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),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
随机推荐
- Linux 系统之Systemd
子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...
- LintCode- 删除排序数组中的重复数字
题目描述: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 给出数组A =[1,1 ...
- Protel 99SE铺铜问题总结
一.PCB电路板放置铺铜有什么作用? 散热: 屏蔽 抗干扰 pcb板子带有寄生电容: 提高板子强度: 美观: 增加被抄板的难度,尤其是覆铜+黑油. 二.PROTEL不规则铺铜的方法: 1.先要知道 ...
- JS于,子类调用父类的函数
概要 JS虽然没有直接有面向对象的特性,但还是能prototype为了模拟面向对象的特性,如继承和多态.而大多数面向对象的语言(例如C++.Java等一下)相比,JS为了实现面向对象还是有点繁琐,抽象 ...
- Android 中文API (68) —— BluetoothClass.Service
前言 本章内容是 android.bluetooth.BluetoothClass.Service,为Android蓝牙部分的章节翻译,版本为 Android 2.3 r1,翻译来自中山大学的&q ...
- Android SDK三种更新失败及其解决方法
更新Android SDK,居然失败了三次. 1.第一次失败 出现Failed to fetch URL错误提示 Failed to fetch URL https://dl-ssl.google.c ...
- Aop编程--注解与xml的实现
一.注解方式 1.首先引入spring对于aop编程的jar支持包,spring框架没有的包请自行在网上下载. aopalliance-alpha1.jar aspectjrt.jar aspectj ...
- 鼠标放上去图片慢慢变大js 或 变大
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java之对象序列化和反序列化
一.对象序列化和反序列化存在的意义: 当你创建对象,只要你需要,他就一直存在,但当程序结束,对象就会消失,但是存在某种情况,如何让程序在不允许的状态,仍然保持该对象的信息.并在下次程序运行的时候使用该 ...
- java键盘录入
System.out:标准输出设备(默认是:控制台) System.in:标准输入设备(默认是:键盘) --------------------- InputStream in = System.in ...