Fox And Two Dots codeforces 510B(DFS)

http://codeforces.com/problemset/problem/510/B

题意:问你能否用相同的字母构成一个环。

分析:每一个都直接从它自身开始,看看到最后是否还能回到它本身。(注意:需要最少4个点)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 100;

typedef long long LL;

char maps[maxn][maxn];
int n, m, flag;
int dir[4][2]= {{0,1}, {1,0}, {0,-1}, {-1,0}};

struct node
{
    int step;
}v[maxn][maxn];

void DFS(int x, int y, int p, int q)
{
    for(int i=0; i<4; i++)
    {
        int nx = dir[i][0]+x;
        int ny = dir[i][1]+y;

        if(nx==p && ny==q && v[x][y].step>=4)
        {
            flag = 1;
            return ;
        }

        if(nx>=0 && nx<n && ny>=0 && ny<m && !v[nx][ny].step && maps[nx][ny]==maps[p][q])
        {
            v[nx][ny].step = v[x][y].step+1;
            DFS(nx, ny, p, q);
        }
    }

    return ;
}
int main()
{
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        memset(maps, 0, sizeof(maps));
        for(int i=0; i<n; i++)
            scanf("%s", maps[i]);

         flag = 0;

        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                memset(v, 0, sizeof(v));
                if(!v[i][j].step)
                {
                    v[i][j].step = 1;
                    DFS(i, j, i, j);
                }

                if(flag)  break;
            }

            if(flag)  break;
        }

        if(flag)  printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

时间: 2024-10-16 08:04:14

Fox And Two Dots codeforces 510B(DFS)的相关文章

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

CodeForces 510B DFS水题

题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring>

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

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

Codeforces 405E DFS

这个题目要求把一个无向连通图里面的所有边,分成 两个一对,只能出现一次,而且一对边必须是连在一起的,点可以复用  但边不可复用 可解条件很易得,因为图是连通的,只要边数为偶数即可. 一开始我借着做欧拉回路的方法,直接DFS暴搜,沿路做标记,遇到未标记的连续两条边 输出即可 不过 事实证明这个算法是错的 暴搜能成立只是建立在图上的边可以存在很多个边对里,但肯定有图不满足这种条件 其实解决方法也就是在DFS的基础上对特殊边进行下考虑即可 即每次对某个点,对子节点进行dfs,如果发现子节点下面有落单的

Codeforces 510B Fox And Two Dots 【DFS】

好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <bits/stdc++.h> #define Max(a,b) (((a) > (b)) ? (a) : (b)) #define Min(a,b) (

CodeForces 510 B. Fox And Two Dots(DFS 啊)

题目链接:http://codeforces.com/problemset/problem/510/B Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m cells, like this: Each cell contains a dot that has some color. We will use diff

codeforces 510B. Fox And Two Dots 解题报告

题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易知道要用到深搜.暴力搜索--- 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 cons

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>