CF510B Fox And Two Dots
题目大意
矩阵中各个方格都有颜色,判断是否有相同颜色的方块可以组成环。(原题链接: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的更多相关文章
- 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 ...
- B. Fox And Two Dots
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- Fox And Two Dots
B - Fox And Two Dots Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- CF 510b Fox And Two Dots
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- 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 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
随机推荐
- html中的一些常用标签与标签属性
label for属性 定义和用法 for 属性规定 label 与哪个表单元素绑定. <span> <label for="username">用户账号& ...
- mysql-workbench-community报错解决办法
输入以下命令: sudo apt-get -f install 参考链接:https://www.jianshu.com/p/767c9a29b403
- centos7离线安装harbor
前言 harbor是一个docker私有仓库,基于docker官方的registry,提供GUI.权限控制.项目管理等功能. 安装harbor前,需要先安装docker和docker-compose ...
- WPF实现类似ChatGPT的逐字打印效果
背景 前一段时间ChatGPT类的应用十分火爆,这类应用在回答用户的问题时逐字打印输出,像极了真人打字回复消息.出于对这个效果的兴趣,决定用WPF模拟这个效果. 真实的ChatGPT逐字输出效果涉及其 ...
- 10、Mybatis之缓存
10.1.环境搭建 10.1.1.创建新module 创建名为mybatis_cache的新module,过程参考5.1节 10.1.2.创建Mapper接口和映射文件 package org.rai ...
- Codeforces 1462E2 Close Tuples (hard version)
题意 给一个长度为\(n\)的数组,取\(m\)个数字,其中最大值最小值相差不大于\(k\),问这种方式有多少种,答案\(\mod 10^9+7\). 分析 通过简单版本大概了解了这题要枚举最小值来判 ...
- WPF学习 - 用鼠标移动、缩放、旋转图片(1)
1. 需求 其实我的需求很简单.就是想做一个图片查看器,可以通过鼠标来平移.缩放.旋转图片. 2. 解决思路: WPF中的UIElement提供了RenderTransform属性,用于承载各种Tra ...
- 上位机使用JS SerialPort进行串口通信, 包含开发环境搭建和完整示例代码
在嵌入式开发中,我们经常需要使用上位机(PC)与一些电路模块进行通信, 用于获取一些传感器的数据,或者发送命令控制相应的电路模块.NodeJS目前支持使用SerialPort模块进行串口通信,本文主要 ...
- Vue源码学习(六):(支线)渲染函数中with(),call()的使用以及一些思考
好家伙, 昨天,在学习vue源码的过程中,看到了这个玩意 嘶,看不太懂,研究一下 1.上下文 这段出现vue模板编译的虚拟node部分 export function renderMixin( ...
- [Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取header时不区分大小写)
1 故事背景 最近项目上有个业务需求,翻译成技术需求,即:将request.headers中的几个header入参转换成request.body(pageRequest)中的内置参数. 为便于灵活配置 ...