CodeForces - 510B Fox And Two Dots (bfs或dfs)
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).
题意:找同一种颜色的环
这题麻烦的地方在于走过的路被标记了,那怎么判断有环呢?
其实记录步数就可以了,bfs和dfs道理是一样的
bfs做法:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node
{
int x,y;
};
bool v[][];
int book[][];
char a[][];
int d[][]={{-,},{,},{,-},{,}};
queue<node>q;
int main()
{
int n,m;
cin>>n>>m;
memset(v,,sizeof(v));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
bool f=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(!v[i][j])
{ memset(book,,sizeof(book));
v[i][j]=;
while(!q.empty()) q.pop();
node b;
b.x=i;
b.y=j;
book[i][j]=;
q.push(b);
while(!q.empty())
{
node b=q.front();
q.pop();
for(int k=;k<;k++)
{
int xx=b.x+d[k][];
int yy=b.y+d[k][];
if(xx<||yy<||xx>n||yy>m||a[xx][yy]!=a[i][j]) continue;
v[xx][yy]=;
node c;
c.x=xx;
c.y=yy;
if(book[xx][yy]>=book[b.x][b.y])//如果当前走过去格子有步数且的步数比当前这个格子还要大,说明已经走过了,形成了环。
{ f=;break;
}
if(book[xx][yy])continue;
q.push(c);
book[xx][yy]=book[b.x][b.y]+;//保存路的步数
}
if(f) break;
}
if(f) break;
}
if(f) break;
}
if(f) break;
}
if(f) cout<<"Yes";
else cout<<"No";
return ; }
dfs也是同样道理:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
char a[][];
bool book[][];
int v[][];
int z[];
int n,m;
int d[][]={{-,},{,},{,},{,-}};
bool f=;
int si,sj;
void dfs(char c,int x,int y)
{ for(int i=;i<;i++)
{
int xx=x+d[i][];
int yy=y+d[i][];
if(xx<||yy<||xx>n||yy>m) continue;
if(v[xx][yy]!=&&v[x][y]-v[xx][yy]>)//走过去的格子已经有值了且比现在走的格子还大不止1,大1可能是之前走过来的
{
f=;
return;
}
if(a[xx][yy]==c&&v[xx][yy]==)
{
v[xx][yy]=v[x][y]+;
book[xx][yy]=;
dfs(c,xx,yy);
if(f) return;
v[xx][yy]=; }
}
} int main()
{
scanf("%d %d",&n,&m);
memset(z,,sizeof(z));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(!book[i][j])
{
z[a[i][j]-'A']=;
memset(v,,sizeof(v));
book[i][j]=;
si=i;
sj=j;
v[i][j]=;
dfs(a[i][j],i,j);
if(f) break;
}
}
if(f) break;
}
if(f) cout<<"Yes";
else cout<<"No";
return ;
}
最新文章
- Java 中的 Filter 过滤器详解
- cf730e
- 日期String相互转换
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
- css3 transition属性变化与animation动画的相似性以及不同点
- 停止Java线程,小心interrupt()方法
- PHP学习笔记十九【析构函数】
- JSP简单的练习-用户登记表
- 升级版本后报这个异常 : org.springframework.beans.factory.NoUniqueBeanDefinitionException
- DevOps之服务-监控工具
- 前端入门10-JavaScript语法之对象
- 面向对象的 __slots__
- Java利用POI读取Excel
- Mysql知识点个人整理
- Cookie的存储、获取、删除操作
- exp自动备份在bat中不执行
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
- SSRF攻击-运用gopher协议构造POST包--emmmm(http://10.112.68.215:10004/index.php?action=login)
- JS Async Callback
- 【译】Kafka学习之路
热门文章
- Request对象介绍(客户端到服务器)
- springmvc中url-url-pattern /和/*的区别
- 【P2774】方格取数问题(贪心+最大流,洛谷)
- 【转】Vue.js:轻量高效的前端组件化方案
- JAVA学习前十天:小结、面向对象之”扑克牌“例子
- [Kafka] - Kafka内核理解:消息存储机制
- java: Comparable比较器,定义二叉操作类
- PostMan使用教程(1)
- 《Advanced Bash-scripting Guide》学习(十五):测试坏的链接文件(broken link)
- java常用封装方法