CF510B Fox And Two Dots(搜索图形环)
2 seconds
256 megabytes
standard input
standard output
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:
- These k dots are different: if i ≠ j then di is different from dj.
- k is at least 4.
- All dots belong to the same color.
- For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.
Determine if there exists a cycle on the field.
The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output "Yes" if there exists a cycle, and "No" otherwise.
3 4
AAAA
ABCA
AAAA
Yes
3 4
AAAA
ABCA
AADA
No
4 4
YYYR
BYBY
BBBY
BBBY
Yes
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
Yes
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
No
In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).
【题意】
给定n*m矩阵,看是否有相同的字符可以构成一个环
【分析】
爆搜~
注意:1、构成环至少需要4个字符
2、注意判断字符的来路
【代码】
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=105;
int n,m,ans,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char mp[N][N];bool vis[N][N]={0};
void dfs(int x,int y,int px,int py,int step){
vis[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx<1||ny<1||nx>n||ny>m||mp[nx][ny]!=mp[x][y]) continue;
if(!vis[nx][ny]) dfs(nx,ny,x,y,step+1);
else{
if((nx!=px||ny!=py)&&step>=4){puts("Yes");exit(0);}
}
}
}
inline void Init(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
}
inline void Solve(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!vis[i][j]){
dfs(i,j,0,0,1);
}
}
}
puts("No");
}
int main(){
Init();
Solve();
return 0;
}
CF510B Fox And Two Dots(搜索图形环)的更多相关文章
- CF Fox And Two Dots (DFS)
Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 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 - 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 ...
- B. Fox And Two Dots
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Fox And Two Dots
B - Fox And Two Dots Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- D - Fox And Two Dots DFS
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- CF 510b Fox And Two Dots
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
随机推荐
- 通过NAT转发实现私网对外发布信息
我们可以在防火墙的外部网卡上绑定多个合法IP地址,然后通过ip映射使发给其中某一个IP地址的包转发至内部某一用户的WWW服务器上,然后再将该内部WWW服务器响应包伪装成该合法IP发出的包. 具体的IP ...
- 安卓开发笔记——TabHost组件(一)(实现底部菜单导航)
什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用windows操作系统的时候,经常见到如图所示的图形界面. TabHost选项卡,说到这个组件, ...
- 和我一起学《HTTP权威指南》——连接管理
连接管理 1.TCP连接 几乎所有的HTTP通信都是由TCP/IP承载的. 浏览网页时客户端执行的操作: 如浏览http://www.joes-hardware.com:80/power-tools. ...
- 6 云计算系列之Nova安装与配置
preface 上面安装好了glance,下面就开始部署nova计算服务了. nova组件介绍 首先介绍下nova各个组件. api 用来接收和响应外部的请求唯一途径,支持Openstack api, ...
- wget 下载指定url路径下的 指定类型的(全部)文件
wget http://mirrors.ustc.edu.cn/ubuntu/pool/main/g/gcc-4.4/ -r -np -nd -A .deb -e robots=offwget htt ...
- Unity3D编辑器之不实例化Prefab获取删除更新组件
原文链接:http://www.xuanyusong.com/archives/3727
- ubuntu14下搭建svn
1.安装 查看是否安装 svn help 安装了卸载 sudo apt-get remove --purge subversion 安装 sudo apt-get update sudo apt-ge ...
- [RN] 03 - Resource Collection & AWS Auth
那些资源 一.三个例子 iReading Bilibili-React-Native ZhiHuDaily-React-Native 二.IM 例子 1. React Native Socket.io ...
- 教你解锁被锁住的苹果mac电脑的文件跟文件夹,同时也可删除被锁的文件跟文件夹(转)
在Mac OSX 下无法删除的文件可大概分为下列三种情形 1.档案(夹)被锁定 2.文件正在使用中 3.没有权限的档案(夹) 一.「 为什么档案会被锁定 」 1.个人自行替档案加上 2.在拷贝或是整理 ...
- 【一步步学OpenGL 21】 -《聚光灯光源》
教程 21 聚光灯光源 原文: http://ogldev.atspace.co.uk/www/tutorial21/tutorial21.html CSDN完整版专栏: http://blog.cs ...