题目链接: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. 开启unity3D的学习之路

    2014年5月11号.我開始了我的Unity3D的学习之路.我将在此记录我学习过程中各个进程,这样在将来的某天,自己忘记了某部分内容时.也能够回过头来复习一下.

  2. 使用 Nginx 提升网站访问速度(转)

    Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Ig ...

  3. selector + drawable 多状态图形

    select_drawble.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...

  4. hadoop —— MapReduce例子 (数据去重)

    参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...

  5. css中字体大小在不同浏览器兼容性问题

    css中使用font-size设定字体大小,不同浏览器的字体height一样,但是width不同,比如在火狐和谷歌中,font-size:20px,字体的高度变为20px,但是谷歌的字体宽度比火狐长 ...

  6. super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

    super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...

  7. Stop logging "internal dummy connection" in Apache

    Apache 2.x keeps child processes alive by creating internal connections which appear in the log file ...

  8. Can't load AMD 64-bit .dll on a IA 32-bit platform错误

    将tomcat的bin目录下的tcnative-1.dll文件删除.就可以了.

  9. BZOJ4889:[TJOI2017]不勤劳的图书管理员

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  10. java计算两个时间相差(天、小时、分钟、秒)

    public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...