CodeForces 510B DFS水题

题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素)

题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功。

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005

using namespace std;

char Map[MAX][MAX],op;//op记录当前字符

struct node
{
    int x,y;
}pre[MAX][MAX];//pre数组记录当前点的前驱节点
int n,m,vis[MAX][MAX],ok,v[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int check(int x,int y,int a,int b)
{
    if(a>=0 && a<n && b>=0 && b<m && (pre[x][y].x!=a ||pre[x][y].y!=b) && Map[a][b]==op)
        return 1;
    return 0;
}

void DFS(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int a=x+v[i][0];
        int b=y+v[i][1];
        if(check(x,y,a,b))
        {
            if(vis[a][b])
            {
                ok=1;
                return;
            }
            pre[a][b].x=x;
            pre[a][b].y=y;
            vis[a][b]=1;
            DFS(a,b);
        }
    }
    if(ok==1)
        return;
}

int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ok=0;
        memset(vis,0,sizeof(vis));
        for(i=0;i<MAX;i++)
        {
            for(j=0;j<MAX;j++)//初始化每个点的前驱点都是本身
            {
                pre[i][j].x=i;
                pre[i][j].y=j;
            }
        }
        for(i=0;i<n;i++)
            scanf("%s",Map[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(!vis[i][j])
                {
                    op=Map[i][j];
                    vis[i][j]=1;
                    DFS(i,j);
                }
                if(ok)
                    break;
            }
            if(ok)
                break;
        }
        if(ok)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

时间: 2024-10-20 17:37:31

CodeForces 510B DFS水题的相关文章

CodeForces 20B Equation 水题

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

CodeForces 705A Hulk (水题)

题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring

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 <algori

CodeForces 474B Worms (水题,二分)

题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 1e5 + 5; int a[maxn]; int main(){ int n, m; cin >> n; for(int i = 1; i <= n; +

codeforces 377A. Puzzles 水题

A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/337/A Description The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to p

poj 1979 Red and Black(dfs水题)

Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only

uva524(dfs水题)

题意: 把编号为1到n的珠子,串成手环,要求任意两个相邻的珠子和都为质数: 思路: 数据量只有16,打个质数表,直接dfs搜就行了: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int vis[32]; int viss[20]; int res[20],n; void init() { vis[1] = 1; for(int i = 2; i <= 31;

CodeForces 709A Juicer (水题, 模拟)

题意:给定 n 个桔子的大小,一个杯子的容积,一个最大限度,挨着挤桔子汁,如果大小大于限度,扔掉,如果不杯子满了倒掉,问你要倒掉多少杯. 析:直接按要求模拟就好,满了就清空杯子. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath&g