题目大意

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

    VUE组件简单实例 <!DOCTYPE html> <html> <head> <title> </title> </head> ...

  2. LabVIEW图形化的AI视觉开发平台(非NI Vision)VI简介

    前言 今天想和大家分享的是:仪酷LabVIEW AI视觉工具包的VI简介,如介绍内容有误,欢迎各位朋友们帮忙纠正~ 一.AI视觉工具包VI简介 已经安装好的AI工具包位于程序框图-函数选板-Addon ...

  3. nlp入门(四)新闻分类实验

    源码请到:自然语言处理练习: 学习自然语言处理时候写的一些代码 (gitee.com) 数据来源: 搜狗新闻语料库 由于链接失效,现在使用百度网盘分享 链接:https://pan.baidu.com ...

  4. Vue项目打包后放到SpringBoot项目里注意点

  5. 深入理解 Flutter 图片加载原理

    前言 随着Flutter稳定版本逐步迭代更新,京东APP内部的Flutter业务也日益增多,Flutter开发为我们提供了高效的开发环境.优秀的跨平台适配.丰富的功能组件及动画.接近原生的交互体验,但 ...

  6. 数据库中limit 和 offset 使用区别

    题:查找最晚入职员工的所有信息 1,SELECT * FROM employees ORDER BY hire_date DESC LIMIT 0,1; 解:对列hire_date分组后升序,从下标( ...

  7. Flutter系列文章-Flutter应用优化

    当涉及到优化 Flutter 应用时,考虑性能.UI 渲染和内存管理是至关重要的.在本篇文章中,我们将通过实例深入讨论这些主题,展示如何通过优化技巧改进你的 Flutter 应用. 代码性能优化 1. ...

  8. 升级java11后,maven命令打包报错

    一.问题 升级java11后,maven命令打包报错: mvn clean package -Dmaven.test.skip=true [ERROR] Failed to execute goal ...

  9. C# winform 无边框窗口 移动

    给自己留个笔记, 在用wke做界面的时候. 往往需要把winform窗口设置成无边框 但是WebUI也需要移动窗口, 所以才把以前在易语言中用的方法翻译过来使用 第零步: 设置无边框窗口 form属性 ...

  10. (数据科学学习手札154)geopandas 0.14版本新特性一览

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在前两天,Python生态中 ...