题目大意

矩阵中各个方格都有颜色,判断是否有相同颜色的方块可以组成环。(原题链接: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. QTextEdit的使用

    import sys from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit,QVBoxLayout, QPushButton cla ...

  2. 8、Spring之基于注解的自动装配

    8.1.场景模拟 8.1.1.UserDao接口及实现类 package org.rain.spring.dao; /** * @author liaojy * @date 2023/8/5 - 18 ...

  3. vue中添加音频和视频

    视频播放功能 1. 安装vue-video-player npm install vue-video-player --save 或 yarn add vue-video-player --save ...

  4. 5.0 CRC32校验技术概述

    CRC校验技术是用于检测数据传输或存储过程中是否出现了错误的一种方法,校验算法可以通过计算应用与数据的循环冗余校验(CRC)检验值来检测任何数据损坏.通过运用本校验技术我们可以实现对特定内存区域以及磁 ...

  5. Linux离线安装Mysql-5.7

    1.背景描述 在真实业务场景下,Linux服务器一般位于内网,所以无法直接访问互联网资源: 特别是安装数据库的Linux服务器,在网络方面的管控只会更加严格: 因此,需要提前下载好相关资源,再传输到内 ...

  6. 为什么 Python 代码在函数中运行得更快?

    哈喽大家好,我是咸鱼 当谈到编程效率和性能优化时,Python 常常被调侃为"慢如蜗牛" 有趣的是,Python 代码在函数中运行往往比在全局范围内运行要快得多 小伙伴们可能会有这 ...

  7. JUC并发编程(2)—synchronized锁原理

    目录 乐观锁和悲观锁介绍 synchronized用法介绍 synchronized和ReentrantLock的区别 经典8锁问题案例 从字节码角度分析synchronized实现 synchron ...

  8. 算法——AcWing算法提高课中代码和题解

    文章目录 第一章 动态规划 (完成情况:64/68) 数字三角形模型 最长上升子序列模型 背包模型 状态机模型 状态压缩DP 区间DP 树形DP 数位DP 单调队列优化DP 斜率优化DP 第二章 搜索 ...

  9. 掌握Go类型内嵌:设计模式与架构的新视角

    本文深入探讨了Go语言中的类型内嵌特性,从基础概念到实际应用,以及相关的最佳实践.文章不仅讲解了如何在Go中实现和使用类型内嵌,还通过具体的代码示例展示了其应用场景和潜在陷阱.最后,文章总结了类型内嵌 ...

  10. 【v2v迁移】Xen2kvm 迁移-linux篇

    迁移环境: 源平台:华为FusionComputeV100R006C10SPC101 目标平台:基于KVM虚拟化的云平台,本文以原生的libvirt为例 虚拟机:centos 7.6 具体操作步骤: ...