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 const int maxn = 50 + 5;
 8 char g[maxn][maxn];
 9 bool vis[maxn][maxn];
10
11 int dx[] = {0, 1, 0, -1};
12 int dy[] = {1, 0, -1, 0};
13
14 int n, m;
15
16 bool dfs(int x, int y, int px, int py, char c)
17 {
18     vis[x][y] = 1;
19     for (int i = 0; i < 4; i++)    {
20         int tx = x + dx[i];
21         int ty = y + dy[i];
22         if (tx == px && ty == py)    // 和上一次走过的点冲突
23             continue;
24         if (tx >= 0 && tx < n && ty >= 0 && ty < m && g[tx][ty] == c) {
25             if (vis[tx][ty])     // 形成环
26                 return 1;
27             if (dfs(tx, ty, x, y, c))
28                 return 1;
29         }
30     }
31     return 0;
32 }
33
34
35 int main()
36 {
37     while (scanf("%d%d", &n, &m) != EOF) {
38         for (int i = 0; i < n; i++)
39             scanf("%s", g[i]);
40         memset(vis, 0, sizeof(vis));
41
42         bool flag = false;
43         for (int i = 0; i < n && !flag; i++) {
44             for (int j = 0; j < m && !flag; j++) {
45                 if (!vis[i][j]) {
46                     if (dfs(i, j, -1, -1, g[i][j])) {
47                         flag = true;
48                         break;
49                     }
50                 }
51             }
52         }
53         printf("%s\n", flag ? "Yes" : "No");
54     }
55     return 0;
56 }

cgy4ever 的代码:

http://ideone.com/udz3bN

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 int n, m;
 5 string board[51];
 6 bool visited[51][51];
 7 bool findCycle = false;
 8 int dx[] = {1, -1, 0, 0};
 9 int dy[] = {0, 0, 1, -1};
10
11 void dfs(int x, int y, int fromX, int fromY, char needColor)
12 {
13     if(x < 0 || x >= n || y < 0 || y >= m) return;
14     if(board[x][y] != needColor) return;
15     if(visited[x][y])
16     {
17         findCycle = true;
18         return;
19     }
20     visited[x][y] = true;
21     for(int f = 0; f < 4; f++)
22     {
23         int nextX = x + dx[f];
24         int nextY = y + dy[f];
25         if(nextX == fromX && nextY == fromY) continue;
26         dfs(nextX, nextY, x, y, needColor);
27     }
28 }
29
30 int MAIN()
31 {
32     cin >> n >> m;
33     for(int i = 0; i < n; i++)
34         cin >> board[i];
35     memset(visited, false, sizeof(visited));
36     for(int i = 0; i < n; i++)
37         for(int j = 0; j < m; j++)
38             if(!visited[i][j])
39                 dfs(i, j, -1, -1, board[i][j]);
40     cout << (findCycle ? "Yes" : "No") << endl;
41     return 0;
42 }
43
44 int main()
45 {
46     #ifdef LOCAL_TEST
47         freopen("in.txt", "r", stdin);
48         freopen("out.txt", "w", stdout);
49     #endif
50     ios :: sync_with_stdio(false);
51     cout << fixed << setprecision(16);
52     return MAIN();
53 }

时间: 2024-08-13 18:22:57

codeforces 510B. Fox And Two Dots 解题报告的相关文章

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 505A. Mr. Kitayuta&#39;s Gift 解题报告

题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母,使得新得到的字符串成为回文串. /**************************************(又到自我反省时刻) 做的时候,通过添加一个单位使得长度增加1,找出中点,检验前一半的位置,找出对称位置替换成对应的前一半位置的字符,然后原字符串剩下的部分追加到后面,再判断回文.但是由于

CF 510b Fox And Two Dots

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 different uppercase Latin characters to express differen

Codeforces 130A - Testing Pants for Sadness(解题报告)

Testing Pants for SadnessCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 103A Description The average miner Vaganych took refresher courses. As soo

codeforces 499A.Inna and Pink Pony 解题报告

题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解为跳到t+1:  2.直接跳过 x 分钟,如果player在第 t 分钟,则可以跳到 t+x.问恰好可以看完 n 部电影的最少观看时间.观看一部电影表示 li, li+1, li+2, ..., ri-1, ri 的时间都要覆盖到. 一开始做的时候想得太简单了,确实需要每部电影的所有时间,但是如果不

codeforces 556B. Case of Fake Numbers 解题报告

题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每按一次 button,每个齿轮都会有且仅有一个活动的tooth,对于偶数编号的齿轮,它下一个活动tooth是紧挨着当前tooth的逆时针方向的那个:而奇数编号的齿轮,下一个活动tooth是顺时针的那个.给出每个齿轮的活动tooth,问通过按下有限次的button,问能不能得到一个0,1,...,n-

CodeForces 501B(STL_H题)解题报告

题目链接:http://codeforces.com/problemset/problem/501/B -------------------------------------------------------------------------------- 题意:N个改名操作,要求输出最开始和最终的名字 思路:利用map的操作,读取输入之后,查找是否在key中,如果不在key中,建立新的关系.如果在key中,建立新的key-value对,擦除旧的key-value对.最终通过迭代器输出k

CodeForces 915C(DFS_E题)解题报告

题目链接:http://codeforces.com/problemset/problem/915/C --------------------------------------------------------------------------------- 题意:给你两个数 a和 b,可以打乱 a每位数的顺序,让你求满足 小于等于b 的最大值.. 思路:首先,观察到数值比较大,所以采用字符串读入的方式进行处理,通过比较字典序,交换不同位数是的保证每一次操作都是最优解. 代码: #inc

CodeForces 689B(BFS_B题)解题报告

题目链接:http://codeforces.com/problemset/problem/689/B -------------------------------------------------------------------------------- 题意:在一条直线上有n个城市,相邻两个城市之间的花费为1,但是有捷径,可以到捷径所在的点,而且只需要1的花费,捷径不能前往之前的城市. 思路:经典的,bfs打板直接过.唯一要注意的是这种情况,如4-7有捷径,所以有可能到6的最短路径为