题目链接: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. 思维导图-javascript(转)

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  2. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  3. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...

  4. 《深入浅出WPF》笔记四

    1.WPF资源分布:数据库.资源文件.WPF对象资源.变量2.每个WPF的界面都具有一个名为Resources的属性,其类型为ResourceDictionary,以键值对的形式存储资源.3.检索资源 ...

  5. PHP Socket实现websocket(二)Socket函数

    PHP socket函数是调用系统的的Socket函数,可以参考C语言的socket函数. Socket函数:http://php.net/manual/en/book.sockets.php 服务器 ...

  6. laravel中间件-----------middleware

    middleware中间件 是访问到达服务器后在被对应的路由处理之前所经过的一层过滤层,故称中间件. 中间件是存放在app\http\middleware中,需要定一个 handle 处理方法,在ha ...

  7. time_t 获取的是UCT时间,有时差

    int main() { time_t nowTime; time(&nowTime);//获取当前时间(世界时间)//这种写法也一样nowTime=time(NULL) ; //如果要转化为 ...

  8. php实现图片缩放功能类

    http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package applicatio ...

  9. Unity3d用户手册用户指南 电影纹理(Movie Texture)

    http://www.58player.com/blog-2327-952.html 电影纹理(Movie Texture) 注意:这只是专业/高级功能.   桌面 电影纹理是从视频文件创建的动画纹理 ...

  10. iOS中多线程原理与runloop介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...