Fox And Two Dots
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 different colors.

The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:

  1. These k dots are different: if i ≠ j then di is different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

Input

The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.

Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.

Output

Output "Yes" if there exists a cycle, and "No" otherwise.

Sample test(s)
input
3 4
AAAA
ABCA
AAAA
output
Yes
input
3 4
AAAA
ABCA
AADA
output
No
input
4 4
YYYR
BYBY
BBBY
BBBY
output
Yes
input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
output
Yes
input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
output
No

读错题了,一直以为是要找矩形,比赛快结束才被队友提醒。
DFS一下就可以,每个点记录一下它是从起点出发的第几个点,如果搜索的过程中遇到了走过的点,那么判断一下这两个点的差是否大于等于3,如果满足就说明形成了一个环。本来想的是,如果从某个点出发没找到,那么就说明和这个点连通的所有点都不可行,于是加入了一个剪枝,将这一连通分量减掉,但是似乎没什么作用,加和不加都是15ms,网上还有一种更炫的写法,就是把起点放在当前点的屁股后面,如果遇到了走过的点就找到,我有空试试,写出来的话就更新上来。 --------------------------------------------------------------------------------------------------------- 深夜补发,刚才说的那个算法写了下,实际效果没想象的那么好,还没有我之前写的那个快,不过仔细想想,复杂度貌似是一样的。 不加剪枝:
#include <bits/stdc++.h>
using namespace std; const int UPDATE[][] = {{,},{,-},{,},{-,}};
char MAP[][];
int N,M;
struct Node
{
bool vis = ;
int n = ;
}VIS[][]; bool dfs(int,int,int,char);
int main(void)
{
cin >> N >> M; for(int i = ;i <= N;i ++)
cin >> MAP[i] + ;
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
{
VIS[i][j].vis = VIS[i][j].n = ;
if(dfs(i,j,,MAP[i][j]))
{
puts("Yes");
return ;
}
VIS[i][j].vis = VIS[i][j].n = ;
}
puts("No"); return ;
} bool dfs(int x,int y,int sum,char color)
{
for(int i = ;i < ;i ++)
{
int new_x = x + UPDATE[i][];
int new_y = y + UPDATE[i][];
if(new_x > N || new_x < || new_y > M || new_y < ||
MAP[new_x][new_y] != color)
continue; if(VIS[new_x][new_y].vis && sum - VIS[new_x][new_y].n + >= )
return true;
if(VIS[new_x][new_y].vis)
continue; VIS[new_x][new_y].vis = true;
VIS[new_x][new_y].n = sum + ;
if(dfs(new_x,new_y,sum + ,color))
return true;
VIS[new_x][new_y].vis = false;
VIS[new_x][new_y].n = sum;
} return false;
}

加了剪枝:

#include <bits/stdc++.h>
using namespace std; const int UPDATE[][] = {{,},{,-},{,},{-,}};
char MAP[][];
int N,M;
struct Node
{
bool vis = ;
int n = ;
}VIS[][]; bool dfs(int,int,int,char);
void cut(int,int,char);
int main(void)
{
cin >> N >> M; for(int i = ;i <= N;i ++)
cin >> MAP[i] + ;
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
{
if(MAP[i][j] == '.')
continue;
VIS[i][j].vis = VIS[i][j].n = ;
if(dfs(i,j,,MAP[i][j]))
{
puts("Yes");
return ;
}
VIS[i][j].vis = VIS[i][j].n = ;
cut(i,j,MAP[i][j]);
}
puts("No"); return ;
} bool dfs(int x,int y,int sum,char color)
{
for(int i = ;i < ;i ++)
{
int new_x = x + UPDATE[i][];
int new_y = y + UPDATE[i][];
if(new_x > N || new_x < || new_y > M || new_y < ||
MAP[new_x][new_y] != color)
continue; if(VIS[new_x][new_y].vis && sum - VIS[new_x][new_y].n + >= )
return true;
if(VIS[new_x][new_y].vis)
continue; VIS[new_x][new_y].vis = true;
VIS[new_x][new_y].n = sum + ;
if(dfs(new_x,new_y,sum + ,color))
return true;
VIS[new_x][new_y].vis = false;
VIS[new_x][new_y].n = sum;
} return false;
} void cut(int x,int y,char cor)
{
for(int i = ;i < ;i ++)
{
int new_x = x + UPDATE[i][];
int new_y = y + UPDATE[i][];
if(new_x > N || new_x < || new_y > M || new_y < ||
MAP[new_x][new_y] != cor || VIS[new_x][new_y].vis)
continue;
MAP[new_x][new_y] = '.';
cut(new_x,new_y,cor);
}
}

网上的算法:

#include <bits/stdc++.h>
using namespace std; const int UPDATE[][] = {{,},{,-},{,},{-,}};
int N,M;
char MAP[][];
bool VIS[][];
int BACK_X,BACK_Y; bool dfs(int i,int j,char color);
int main(void)
{
cin >> N >> M;
for(int i = ;i <= N;i ++)
cin >> MAP[i] + ;
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
{
VIS[i][j] = ;
if(dfs(i,j,MAP[i][j]))
{
puts("Yes");
return ;
}
VIS[i][j] = ;
}
puts("No"); return ;
} bool dfs(int i,int j,char color)
{
int back_x = BACK_X;
int back_y = BACK_Y;
for(int k = ;k < ;k ++)
{
int next_x = i + UPDATE[k][];
int next_y = j + UPDATE[k][]; if(next_x > N || next_x < || next_y > M || next_y < ||
MAP[next_x][next_y] != color)
continue;
if(VIS[next_x][next_y] && next_x != BACK_X && next_y != BACK_Y)
{
return true;
}
if(VIS[next_x][next_y])
continue; BACK_X = i;
BACK_Y = j;
VIS[next_x][next_y] = ;
if(dfs(next_x,next_y,color))
return true;
VIS[next_x][next_y] = ;
BACK_X = back_x;
BACK_Y = back_y;
} return false;
}

CF Fox And Two Dots (DFS)的更多相关文章

  1. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  2. D - Fox And Two Dots DFS

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

  3. 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 ...

  4. 17-比赛2 F - Fox And Two Dots (dfs)

    Fox And Two Dots CodeForces - 510B ================================================================= ...

  5. B. Fox And Two Dots

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

  6. Fox And Two Dots

    B - Fox And Two Dots Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  7. CF510B Fox And Two Dots(搜索图形环)

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

  8. CF 510b Fox And Two Dots

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

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

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

随机推荐

  1. Row Border in DataGrid 表格边框

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 转】Maven学习总结(四)——Maven核心概念

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4051819.html 感谢! 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中 ...

  3. [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画

    A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮   code s ...

  4. ASP.NET Web Api返回对象类型为JSON还是XML

    在Umbraco平台上开发过程中,我用WebApi返回JSON result给前端 前端使用React调用这个web api来获取JSON result 我写的web api方法是返回JSON 类型的 ...

  5. HDU 3911 Black And White (线段树区间合并 + lazy标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 给你n个数0和1,m个操作: 0操作  输出l到r之间最长的连续1的个数 1操作  将l到r之间 ...

  6. JS 的点点滴滴

    1. ||含义 : 返回第一个有效值 eg : <script type="text/javascript"> var a=""; var c = ...

  7. Unix: How to Install BerkeleyDB From Source

    http://www.masaokitamura.com/2010/07/23/unix-how-to-install-berkeleydb-from-source/ This documentati ...

  8. UOJ #148. 【NOIP2015】跳石头 二分

    #148. [NOIP2015]跳石头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/148 Descripti ...

  9. 第八讲:HTML5中canvas实现小球击打小方块游戏

    源码:http://download.csdn.net/detail/liumingm900913/7469969 游戏开发流程: 1.创建画布: 将画布放在div标签里面,这样能够控制画布居中的位置 ...

  10. X64 Win7(win2008)连接SqlServer2005慢的解决办法

    问题描述:数据库版本:SQL SERVER 2005数据库安装环境: Win 2003 X64 客户端环境:Win 2008 x64连接工具:ODBC或ado.net测试连接时间:4-6秒 客户端环境 ...