CF Fox And Two Dots (DFS)
2 seconds
256 megabytes
standard input
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:
- These k dots are different: if i ≠ j then di is different from dj.
- k is at least 4.
- All dots belong to the same color.
- 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.
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 "Yes" if there exists a cycle, and "No" otherwise.
3 4
AAAA
ABCA
AAAA
Yes
3 4
AAAA
ABCA
AADA
No
4 4
YYYR
BYBY
BBBY
BBBY
Yes
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
Yes
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
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)的更多相关文章
- 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 ...
- D - Fox And Two Dots DFS
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- 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 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- B. Fox And Two Dots
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Fox And Two Dots
B - Fox And Two Dots Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF 510b Fox And Two Dots
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- Codeforces 510B Fox And Two Dots 【DFS】
好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...
随机推荐
- 第二百三十九天 how can I 坚持
去看了个电影,消失的凶手,乐视会员送的电影票,有点虐心,不过看着感觉还挺不错. 下了班就去看了,也没有吃饭,不过没感觉到饿,回来吃了份泡面,还喝了袋冰凉的酸奶,现在已经感觉肚子有点疼了,哎.. 哲学是 ...
- 设置结点的ID为固定ID
https://www.java.net//forum/topic/jxta/jxta-community-forum/how-initialize-pse-jxse-27 ————————————— ...
- 关于block以及__bridge的一些笔记
问题概要 _block是否是一个OC对象? __bridge相关. _block是否是一个OC对象? 结论 一般来说,block可以看做一个OC对象,但是在编译器底层,block又可以被细分为bloc ...
- C#学习笔记(十四):GC机制和弱引用
垃圾回收(GC) 垃圾回收即Garbage Collector,垃圾指的是内存中已经不会再使用的对象,通过收集释放掉这些对象占用的内存. GC以应用程序的root为基础,遍历应用程序在Heap上动态分 ...
- 瑞丽的SQL-基于窗体的排名计算
在SQL Server中,窗体被定义为用户指定的一组行. 之所以要提出窗体这个概念,由于这种基于窗体或分区的又一次计算在实际工作应用范围比較广泛.比如.假设我们要对每一个班级中的学生按成绩进行排序,在 ...
- C++ 中复杂的声明
1.方法也是有类型的,方法的类型由返回类型和形参表决定.比如int F (int)的类型就是去掉方法名,int (int). 2.对于方法类型,在返回类型和形参表之间,加上一个名称F,就表示一个特定的 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Android编译提示ImportError: No module named bz2的解决办法
在安装node.js时提示ImportError: No module named bz2.很明显这个python中没有装bz2的库导致的.解决方法:sudo apt-get install libb ...
- 如何在Ubuntu 13.04中升级到 GNOME 3.8
如何在Ubuntu 13.04中升级到 GNOME 3.8 添加 GNOME 3 PPA(Personal Package Archives) 在你进一步浏览之前,确认你正在运行的是Ubuntu 13 ...
- Metadata Lock原理8
http://www.kancloud.cn/taobaomysql/monthly/67141 MySQL· 5.7优化·Metadata Lock子系统的优化 背景 引入MDL锁的目的,最初是为了 ...