题目链接:http://codeforces.com/problemset/problem/510/B

题意:判断图中是否有某个字母成环

思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环

代码:

 #include <bits/stdc++.h>
#define MAXN 60
using namespace std; int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n;
int dir[][]={, , , , -, , , -};
bool flag=false; void dfs(int x, int y, int direction){
if(flag){
return;
}
for(int i=; i<; i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<n&&yy>=&&yy<m){
if(i+direction!=&&vis[xx][yy]&&mp[xx][yy]==mp[x][y]){ //***若下一个数字与当前数字相同且已经搜过,则存在满足题意的环,注意别往回的方向搜了
flag=true;
return;
}else if(!vis[xx][yy]&&mp[xx][yy]==mp[x][y]){
vis[xx][yy]=;
dfs(xx, yy, i);
}
}
}
} int main(void){
char ch;
cin >> n >> m;
for(int i=; i<n; i++){
for(int j=; j<m; j++){
cin >> ch;
mp[i][j]=ch-'A'+;
}
}
for(int i=; i<n; i++){
for(int j=; j<m; j++){
if(!vis[i][j]){
vis[i][j]=;
dfs(i, j, );
if(flag){
cout << "Yes" << endl;
return ;
}
}
}
}
cout << "No" << endl;
return ;
}

Codeforces Round #290 (Div. 2) B (dfs)的更多相关文章

  1. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  2. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  3. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  4. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  5. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  8. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

  9. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

随机推荐

  1. Android tab页制作

    全屏启动页 activity_launcher <?xml version="1.0" encoding="utf-8"?> <Relativ ...

  2. webpack为什么加载不了css?

    原文地址: https://segmentfault.com/q/1010000005099261 这个app是用react写的. webpack的loader设置是这样的 module:{ load ...

  3. Mockito @BeforeClass @BeforeMethod @BeforeTest 的生命周期

    @BeforeClass---@AfterClass 类实例化前, 被执行, 主要用于设置环境变量等, 与SpringTestContext结合用的时候要注意, 这种情况下@autowire的bean ...

  4. npm使用淘宝镜像安装包

    npm使用registry这个属性指定仓库,因此配置这个属性即可.修改npm配置属性的几种方法详见官方文档. 这里只贴出修改registry的方法,以下三种任意一种即可: 修改~/.npmrc文件(没 ...

  5. PHP闭包详解

    匿名函数 提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() { }; //带结束符 可以看 ...

  6. IntelliJ IDEA 同时启动多个Tomcat实例端口是会冲突

  7. 三款功能强大代码比较工具Beyond compare、DiffMerge、WinMerge

    我们经常会遇到需要比较同一文件的不同版本,特别是代码文件.如果人工去对比查看,势必费时实力还会出现纰漏和错误,因此我们需要借助一些代码比较的工具来自动完成这些工作.这里介绍3款比较流行且功能强大的工具 ...

  8. physoft.net网站暂停 www.physoft.cn 正式开通 (菲烁科技, physoft)

    physoft.net原本计划以开源代码为主体,由于各种原因代码未能开源.基于这些代码,physoft成立了 菲烁(重庆)科技有限公司 ( www.physoft.cn) ,专注于工业级双目视觉测量, ...

  9. tf.stack和tf.unstack

    import tensorflow as tf a = tf.constant([1,2,3]) b = tf.constant([4,5,6]) c1 = tf.stack([a,b],axis=0 ...

  10. star score

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...