#290 (div.2) B. Fox And Two Dots
来源:互联网 发布:淘宝联盟推广位爱分享 编辑:程序博客网 时间:2024/05/17 04:23
1.题目描述:点击打开链接
2.解题思路:本题利用DFS来解决。本题要求判断一个图中是否存在相同颜色的圈。显然需要利用DFS来寻找。那么该如何寻找呢?题目中已经告诉了我们如何判断一个圈。那么只用根据题意描述来写DFS即可。从没有搜索过的结点开始,每次都找与它相邻的且颜色相同的结点来扩展,此时为了防止重复扩展,需要在DFS参数列表中加上前驱结点。这样以来,一旦发现某一个结点曾经已经标记过,说明找到了一个圈。直接输出Yes并退出循环。
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream>#include<algorithm>#include<string>#include<sstream>#include<set>#include<vector>#include<stack>#include<map>#include<queue>#include<deque>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<functional>using namespace std;#define N 110int vis[N][N];char g[N][N];const int dx[] = { -1, 1, 0, 0 };const int dy[] = { 0, 0, -1, 1 };int n, m;bool inside(int x, int y){ return x >= 0 && x < n&&y >= 0 && y < m; }bool dfs(int x, int y, int prex, int prey, char c){vis[x][y] = 1;for (int i = 0; i < 4; i++){int xx = x + dx[i];int yy = y + dy[i];if (xx == prex&&yy == prey)continue;//如果发现正好等于前驱结点,跳过if (inside(xx, yy)&& g[xx][yy] == c){if (vis[xx][yy])return true;//找到了一个曾经标记过的结点,说明构成了一个圈if (dfs(xx, yy, x, y, c))return true;}}return false;}int main(){//freopen("t.txt", "r", stdin);while (~scanf("%d%d", &n, &m)){for (int i = 0; i < n; i++)scanf("%s", g[i]);memset(vis, 0, sizeof(vis));for (int i = 0; i < n;i++)for (int j = 0; j < m;j++)if (!vis[i][j]){if (dfs(i, j, -1, -1, g[i][j])){puts("Yes");goto x1;}}puts("No");x1:;}return 0;}
0 0
- Codeforces Round #290(Div.2) B.Fox And Two Dots
- B. Fox And Two Dots( Codeforces Round #290 (Div. 2))
- B. Fox And Two Dots Codeforces Round #290 (Div. 2)
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- #290 (div.2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
- Fox And Two Dots (Codeforces Round #290 (Div. 2)B)
- Codeforces Round #290 (Div. 2)B - Fox And Two Dots——并查集/搜索
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
- Codeforces Round #290 (Div. 2)B. Fox And Two Dots(dfs)
- B. Fox And Two Dots
- B. Fox And Two Dots
- B. Fox And Two Dots
- B. Fox And Two Dots
- CodeForces 510B Fox And Two Dots
- 编程之美 格格取数 C语言版本
- 阿! ! ! ! !暗能量守恒定律
- android基础知识
- KVM虚拟机IO处理过程(二) ----QEMU/KVM I/O 处理过程
- Emacs 降魔系列之制作 Python IDE
- #290 (div.2) B. Fox And Two Dots
- Android四大组件及其生命周期详解
- mongodb系列之六:java聚合函数
- T001-UT001-0016
- JS函数调用、修改全局变量的知识
- 言论字面意思记忆
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)
- C++ 成员函数成员变量 静态成员函数和变量
- 2-5-4 静态成员应用