题目链接:http://codeforces.com/problemset/problem/510/B

题目意思:给出 n 行 m 列只有大写字母组成的字符串。问具有相同字母的能否组成一个环。

  很容易知道要用到深搜。暴力搜索~~~

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
char g[maxn][maxn];
bool vis[maxn][maxn]; int dx[] = {, , , -};
int dy[] = {, , -, }; int n, m; bool dfs(int x, int y, int px, int py, char c)
{
vis[x][y] = ;
for (int i = ; i < ; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx == px && ty == py) // 和上一次走过的点冲突
continue;
if (tx >= && tx < n && ty >= && ty < m && g[tx][ty] == c) {
if (vis[tx][ty]) // 形成环
return ;
if (dfs(tx, ty, x, y, c))
return ;
}
}
return ;
} int main()
{
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = ; i < n; i++)
scanf("%s", g[i]);
memset(vis, , sizeof(vis)); bool flag = false;
for (int i = ; i < n && !flag; i++) {
for (int j = ; j < m && !flag; j++) {
if (!vis[i][j]) {
if (dfs(i, j, -, -, g[i][j])) {
flag = true;
break;
}
}
}
}
printf("%s\n", flag ? "Yes" : "No");
}
return ;
}

cgy4ever 的代码:

http://ideone.com/udz3bN

 #include <bits/stdc++.h>
using namespace std; int n, m;
string board[];
bool visited[][];
bool findCycle = false;
int dx[] = {, -, , };
int dy[] = {, , , -}; void dfs(int x, int y, int fromX, int fromY, char needColor)
{
if(x < || x >= n || y < || y >= m) return;
if(board[x][y] != needColor) return;
if(visited[x][y])
{
findCycle = true;
return;
}
visited[x][y] = true;
for(int f = ; f < ; f++)
{
int nextX = x + dx[f];
int nextY = y + dy[f];
if(nextX == fromX && nextY == fromY) continue;
dfs(nextX, nextY, x, y, needColor);
}
} int MAIN()
{
cin >> n >> m;
for(int i = ; i < n; i++)
cin >> board[i];
memset(visited, false, sizeof(visited));
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(!visited[i][j])
dfs(i, j, -, -, board[i][j]);
cout << (findCycle ? "Yes" : "No") << endl;
return ;
} int main()
{
#ifdef LOCAL_TEST
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios :: sync_with_stdio(false);
cout << fixed << setprecision();
return MAIN();
}

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

  1. CodeForces - 510B Fox And Two Dots (bfs或dfs)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. codeforces A. Fox and Box Accumulation 解题报告

    题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...

  3. Codeforces 510B Fox And Two Dots 【DFS】

    好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...

  4. codeforces C1. The Great Julya Calendar 解题报告

    题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...

  5. CF 510b Fox And Two Dots

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  6. codeforces B. Eugeny and Play List 解题报告

    题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...

  7. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

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

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  9. codeforces 505A. Mr. Kitayuta's Gift 解题报告

    题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...

随机推荐

  1. Highcharts属性中英文参照

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. Javascript 中我很想说说的 this

    this是每一个想要深入学习Javascript的人必过的一关,我为this看过很多书查过很多资料,虽然对this有了一定的了解并且也经常使用this,但是如果有人问我  this是什么呀? 我依旧不 ...

  3. List<List<double>> lsls = null; 根据double值来重新排序lsls...

    "确定:Node-data = (7,2).具体是:根据x维上的值将数据排序, 6个数据的中值(所谓中值,即中间大小的值)为7, 所以Node-data域位数据点(,).这样, 该节点的分割 ...

  4. Office 2010/2007 简繁体转换按钮不见了?

    注:此文章来自微软官方,原文链接:http://support.microsoft.com/kb/2459493/zh-tw 经测试可解决问题.——————————– 通常发生这样的问题,是由于繁简转 ...

  5. 摄像头/光驱故障:由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备。 (代码 19)

    原因:出现这条提示一般都是因为注册表错误引起的,重装驱动或应用程序无法解决. 设备管理器中相关设备-故障如图: 以下方法可以解决问题: 1.在任务栏上点“开始”菜单-“运行”,输入”regedit“ ...

  6. call() 和 apply() ----预定义的函数方法

  7. JSP中根据不同的条件显示不一样的格式

    在做项目中遇到这样的场景: 当查询到记录时,需要将记录的字段作为下拉列表,让用户选择使用,即显示的是下拉列表. 当没有查询到记录时,则让用户手工填写该值,即显示的是文本框. 前段jsp使用if标签如下 ...

  8. Android学习笔记函数

    //调用新的Activity Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent ...

  9. js图文讲解

       

  10. ajax 载入html后不能执行其中的js解决方法

    事件背景 有一个公用页面需要在多个页面调用,其中涉及到部分js已经写在了公用页面中,通过ajax加载该页面后无法执行其中的js. 解决思路 1. 采用附加一个iframe的方法去执行js,为我等代码洁 ...