Codeforces Round #290 (Div. 2) B (dfs)
题目链接: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)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- 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 ...
- 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 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- Codeforces Round #290 (Div. 2) _B找矩形环的三种写法
http://codeforces.com/contest/510/status/B 题目大意 给一个n*m 找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...
随机推荐
- spring mvc头
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- GstAppSink简介
Description Appsink is a sink plugin that supports many different methods for making the application ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- zabbix监控系统性能采集指标
监控项目 详细内容 ...
- VLAN虚拟局域网技术(二)-计算机网络
本文主要知识来源于学校课程,部分知识来自于H3C及思科中国公司网页技术手册,未经许可,禁止转载.如需转载,请联系作者并注明出处. 本节主要是总结一些思科的VLAN组中的私有协议:DTP和VTP. 1. ...
- apace搭建站点
Listen 127.0.0.1:3310<VirtualHost *:3306> ServerName 127.0.0.1:3306 DocumentRoot "F:/Baid ...
- matlab之boundary()函数
j = boundary(x,y,0.1); %这个函数是求一堆(平面)点的边界,它不是凸包,这里的边界可以凹陷,第三个参数0.1代表以松散的形式画出边界,1是以最紧凑的形式画出边界. 下面这个是官网 ...
- 转载h5问题总结
判断微信浏览器 function isWeixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger ...
- jsp参数传递
jsp参数传递 jsp中四种传递参数的方法 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf=&q ...