hzau 1208 Color Circle(dfs)

1208: Color Circle

Time Limit: 1 Sec  Memory Limit: 1280 MB
Submit: 289  Solved: 85
[Submit][Status][Web Board]

Description

There are colorful flowers in the parterre in front of the door of college and form many beautiful patterns. Now, you want to find a circle consist of flowers with same color. What should be done ?

Assuming the flowers arranged as matrix in parterre, indicated by a N*M matrix. Every point in the matrix indicates the color of a flower. We use the same uppercase letter to represent the same kind of color. We think a sequence of points d1, d2, … dk makes up a circle while:

1. Every point is different.

2. k >= 4

3. All points belong to the same color.

4. For 1 <= i <= k-1, di is adjacent to di+1 and dk is adjacent to d1. ( Point x is adjacent to Point y while they have the common edge).

N, M <= 50. Judge if there is a circle in the given matrix.

Input

There are multiply test cases.

In each case, the first line are two integers n and m, the 2nd ~ n+1th lines is the given n*m matrix. Input m characters in per line.

Output

Output your answer as “Yes” or ”No” in one line for each case.

Sample Input

3 3
AAA
ABA
AAA

Sample Output

Yes

HINT

dfs

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int MAXN = 55;
 5
 6 int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},};
 7 char g[MAXN][MAXN];
 8 int depth[MAXN][MAXN];
 9 bool vis[MAXN][MAXN];
10 int n, m;
11 char bg;
12 bool circle;
13
14 void init()
15 {
16     memset(g, ‘\0‘, sizeof(g));
17     memset(depth, 0, sizeof(depth));
18     memset(vis, false, sizeof(vis));
19     circle = false;
20 }
21
22 bool check(int r, int c)
23 {
24     if (r < 0 || r >= n) {
25         return false;
26     }
27     if (c < 0 || c >= m) {
28         return false;
29     }
30     return true;
31 }
32
33 void dfs(int r, int c, int d)
34 {
35     if (circle) {
36         return;
37     }
38     int i;
39     int r2, c2;
40     for (i = 0; i < 4; ++i) {
41         r2 = r + dir[i][0];
42         c2 = c + dir[i][1];
43         if (!check(r2, c2)) {//越界
44             continue;
45         }
46         if (g[r][c] != bg) {//不同
47             continue;
48         }
49         if (!vis[r2][c2]) {//没访问过
50             vis[r2][c2] = true;
51             depth[r2][c2] = d + 1;
52             dfs(r2, c2, d + 1);
53             depth[r2][c2] = 0;
54             vis[r2][c2] = false;
55         } else if (d - depth[r2][c2] + 1 >= 4) {//找到环
56             circle = true;
57             return;
58         }
59     }
60 }
61
62 int main()
63 {
64     int i, j;
65
66     while (~scanf("%d%d", &n, &m)) {
67         //init();
68         for (i = 0; i < n; ++i) {
69             scanf("%s", g[i]);
70         }
71         circle = false;
72         for (i = 0; i < n; ++i) {
73             for (j = 0; j < m; ++j) {
74                 bg = g[i][j];
75                 vis[i][j] = true;
76                 depth[i][j] = 1;
77                 dfs(i, j, 1);
78                 depth[i][j] = 0;
79                 vis[i][j] = false;
80                 if (circle) {
81                     break;
82                 }
83             }
84             if (circle) {
85                 break;
86             }
87         }
88         if (circle) {
89             printf("Yes\n");
90         } else {
91             printf("No\n");
92         }
93     }
94
95     return 0;
96 }
时间: 2024-10-13 00:17:09

hzau 1208 Color Circle(dfs)的相关文章

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows

HDU 4669 Mutiples on a circle(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4669 题意:给出一个长度为n的数字环A和数字m.问有多少子串(连续)使得这些子串的数字拼在一起是m的倍数? 思路:首先计算A[1]和A[n]不在一起的.这个简单,只要记录f[i][j]表示到第i个数字余数为j的个数即可,然后: 接着计算a[1]和a[n]在一起的情况. 我们首先预处理end[i]表示数字子串A[i,n]连在一起对m的余数.然后枚举i从[1,n-1],那么数字设数字串[1,i]的长度为