#290 (div.2) B. Fox And Two Dots

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 110
int 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;
}
时间: 2024-10-01 06:05:20

#290 (div.2) B. Fox And Two Dots的相关文章

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

#290 (div.2) D. Fox And Jumping

1.题目描述:点击打开链接 2.解题思路:本题利用扫描与维护解决.根据题意,能够走到所有的格子,一定是挑选出来的牌的步数的最大公约是1,这点很好理解.因为ax+by=1意味着只要有a个x和b个y就可以凑出来步数1.这样以来,只需要利用map来存储所有的公约数对应的最小费用即可.初始时刻base[0]=0,接下来就是从前往后扫描一遍这n个数,然后依次更新base中的每一个最大公约是对应的最小费用即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<i

#290 (div.2) C. Fox And Names

1.题目描述:点击打开链接 2.解题思路:本题利用拓扑排序解决.本题要求出一个a~z的排列,使得所有名字按照这样的"字典序"是逐渐增加的.显然这里存在着字母之间的大小关系,容易联想到拓扑排序. 那么该如何来排序呢?先思考一下简单的情况,假设姓名s,t是相邻的两个名字,如果s是t的一个前缀,那么跳过即可:反之如果t是s的前缀,那么肯定是无解的.如果不是以上这种情况,那么首个不相同的位置处的两个字母就可以连一条边,最终构建出一个有向图.最后利用拓扑排序的模板,为26个英文字母从后向前排序即

B. Fox And Two Dots Codeforces Round #290 (Div. 2)

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m ce

Fox And Two Dots

B - Fox And Two Dots Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cel

Codeforces Round #290 (Div. 2) 解题报告 A.B.C.D.

A - Fox And Snake 模拟. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using na

Codeforces Round #290 (Div. 2) b

/** * @brief Codeforces Round #290 (Div. 2) b * @file a.cpp * @author mianma * @created 2015/02/04 15:17 * @edited 2015/02/04 15:17 * @type brute * @note */ #include <fstream> #include <iostream> #include <string> #include <cstring>

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

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m ce