题目链接: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. MySQL创建一个用户,指定一个数据库 授权

    Mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -ppassworduse mysql;insert into user(h ...

  2. shell学习之路:流程控制(while)

    while循环: 介绍:while循环是不定循环,也称作条件循环.只要条件判断成立,循环就会一直继续执行,直到条件判断不成立,循环才会停止,这就是和for的固定循环不太一样了. while [ 条件判 ...

  3. SAS学习笔记<一>

    三个周末的SAS课程宣告结束, 总结下来 第一周的统计原理简介 第二周/第三周讲解SAS的基本操作. 总体下来,对自己的知识结构有了一个新的梳理,对比大学时期,某个老师一上来就教我们SAS编程,而未考 ...

  4. JQuery-EasyUI DataGrid CRUD

    ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页! 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号. ...

  5. 【C语言入门教程】3.1 程序的 3 种基本结构

    程序设计是一个问题求解的过程,解决问题的步骤可看作是程序的控制结构.简单地说,程序的运行过程就是数据输入.数据处理.数据输出 3 个步骤.其中,数据处理过程是否快捷和准确,主要依赖于程序控制结构的设计 ...

  6. MySQL Cluster 配置文件(config.ini)详解

    MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...

  7. servlet之session添加和移除的两种方式

    Java Session 介绍 一.添加.获取session 1.项目结构 2.jar包 3.web.xml文件 <?xml version="1.0" encoding=& ...

  8. canvas对象arc函数的使用-遁地龙卷风

    (-1)写在前面 我用的是chrome49 <canvas id="lol" height="300"></canvas> (1)详细介 ...

  9. javaweb 解决将ajax返回的内容用document.write写入,FireFox一直加载的问题

    在document.write方法后加上document.close就解决了, 想知道原理的小伙伴可以继续看 浏览器在解析html时会打开一个流,这是用document.write中写入,是加入当解析 ...

  10. BZOJ2599——[IOI2011]Race

    0.题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 1.分析:水题一道,一波树分治就好 我们可以发现这个题的K是比较小的,才100w,那么我们可以树分治一下,在遍历每一棵子树的 ...