题目大意

矩阵中各个方格都有颜色,判断是否有相同颜色的方块可以组成环。(原题链接:CF510B Fox And Two Dots

输入:

第一行:\(n\), \(m\),表示矩阵的行和列

接下来\(n\)行: 输入矩阵

输出:

如果有环则输出:\(Yes\), 否则输出:\(No\);

样例:

输入样例:\(*1\)

3 4

AAAA

ABCA

AAAA

输出样例: \(*1\)

Yes


输入样例:\(*2\)

3 4

AAAA

ABCA

AADA

输出样例: \(*2\)

No

思路:

其实就是判断连通块,然后再加个条件能否搜到初始点。

不过要注意的是:

1.不能搜当前点的上一个状态,即用两个变量记录一下即可,如果是就\(continue\)即可

剩下的就是常规\(dfs\)了,还有就是可以提前记录一下每个字母的数量,如果小于\(4\)那么可以直接不用搜了。

代码:

#include <iostream>

using namespace std;

const int N = 55;
int n, m;
char g[N][N];
bool st[N][N], flag;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int cnt[27]; void dfs(int x, int y, char temp, int lastX, int lastY)
{
st[x][y] = true;
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i], yy = y + dy[i];
if(xx == lastX && yy == lastY) continue; //上个点
if(st[xx][yy] && g[xx][yy] == temp) flag = 1; //说明搜到
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && g[xx][yy] == temp && !st[xx][yy]) dfs(xx, yy, temp, x, y);
}
return;
} int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> g[i][j];
cnt[g[i][j] - 'A']++;
}
} for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(!st[i][j] && cnt[g[i][j] - 'A'] >= 4) dfs(i, j, g[i][j], 0, 0);
if(flag == 1)
{
cout << "Yes" << endl;
return 0;
}
}
}
if(!flag) cout << "No" << endl;
return 0;
}

CF510B Fox And Two Dots的更多相关文章

  1. 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 ...

  2. B. Fox And Two Dots

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 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 ...

  4. Fox And Two Dots

    B - Fox And Two Dots Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  5. CF510B Fox And Two Dots(搜索图形环)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. 17-比赛2 F - Fox And Two Dots (dfs)

    Fox And Two Dots CodeForces - 510B ================================================================= ...

  8. CF 510b Fox And Two Dots

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  9. D - Fox And Two Dots DFS

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  10. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

随机推荐

  1. Django: 后台常用操作

    指定状态码 return JsonResponse(data, status=201) Djano删除数据库 删除对应数据库后,删除对应文件 删除对应的记录 Django后台管理 创建超级管理员 py ...

  2. Swiper.vue?56a2:132 Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.

    错误代码 解决方案 删除div标签.修改后,如下所示:

  3. Centos安装 Apache Benchmark

    检查依赖包是否安装 1 rpm -qa|grep apr-util 2 3 rpm -qa|grep yum-utils 输出信息: 1 apr-util-1.5.2-6.el7.x86_64 2 y ...

  4. 安装 配置 正向 解析 DNS方法

    安装 配置 正向 解析 DNS方法 1,安装dhcp [root@localhost ~]#yum install bind* -y 2,关闭防火墙和selinux [root@localhost ~ ...

  5. Hi3798MV200 恩兔N2 NS-1 (三): 制作 Ubuntu rootfs

    目录 Hi3798MV200 恩兔N2 NS-1 (一): 设备介绍和刷机说明 Hi3798MV200 恩兔N2 NS-1 (二): HiNAS海纳思使用和修改 Hi3798MV200 恩兔N2 NS ...

  6. [Python]树基础

    关于树 树是一种数据结构,由n个有限节点组成的一个具有层次关系的集合.二叉树则是每个节点最多有两个子树的树结构.二叉树一般有以下性质: 二叉树第k层上的节点数目最多为 \(2^{k-1}\) 深度为 ...

  7. 论文解读(TAT)《 Transferable Adversarial Training: A General Approach to Adapting Deep Classifiers》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Transferable Adversarial Training: A General Approach ...

  8. Oracle数据库经纬度坐标查询优化与结果错误原因分析、SQL中WKT超长文本字符串处理

    目录 一.Oracle几何空间数据对象和其他数据库的差异 二.Oracle查询一个经纬度坐标是否在边界内部 2.1 查询条件 2.2 查询结果错误,似乎是仅做了MBR匹配 2.3 错误原因 2.4 解 ...

  9. 想了解API接口,这一篇就够了

    API(Application Programming Interface)接口,对于大多数人来说可能还比较陌生,但实际上我们每天都在与它打交道.无论是使用手机上的应用程序,还是在网上购物,都少不了A ...

  10. dotnet SemanticKernel 入门 自定义变量和技能

    本文将告诉大家如何在 SemanticKernel 框架内定义自定义的变量和如何开发自定义的技能 本文属于 SemanticKernel 入门系列博客,更多博客内容请参阅我的 博客导航 自定义变量是一 ...