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 4AAAAABCAAAAA

output
Yes

input
3 4AAAAABCAAADA

output
No

input
4 4YYYRBYBYBBBYBBBY

output
Yes

input
7 6AAAAABABBBABABAAABABABBBABAAABABBBABAAAAAB

output
Yes

input
2 13ABCDEFGHIJKLMNOPQRSTUVWXYZ

output
No

读错题了,一直以为是要找矩形,比赛快结束才被队友提醒。DFS一下就可以,每个点记录一下它是从起点出发的第几个点,如果搜索的过程中遇到了走过的点,那么判断一下这两个点的差是否大于等于3,如果满足就说明形成了一个环。本来想的是,如果从某个点出发没找到,那么就说明和这个点连通的所有点都不可行,于是加入了一个剪枝,将这一连通分量减掉,但是似乎没什么作用,加和不加都是15ms,网上还有一种更炫的写法,就是把起点放在当前点的屁股后面,如果遇到了走过的点就找到,我有空试试,写出来的话就更新上来。

---------------------------------------------------------------------------------------------------------

深夜补发,刚才说的那个算法写了下,实际效果没想象的那么好,还没有我之前写的那个快,不过仔细想想,复杂度貌似是一样的。

不加剪枝:
#include <bits/stdc++.h>
using    namespace    std;const    int    UPDATE[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
char    MAP[55][55];
int    N,M;
struct    Node
{bool    vis = 0;int    n = 0;
}VIS[55][55];bool    dfs(int,int,int,char);
int    main(void)
{cin >> N >> M;for(int i = 1;i <= N;i ++)cin >> MAP[i] + 1;for(int i = 1;i <= N;i ++)for(int j = 1;j <= M;j ++){VIS[i][j].vis = VIS[i][j].n = 1;if(dfs(i,j,1,MAP[i][j])){puts("Yes");return    0;}VIS[i][j].vis = VIS[i][j].n = 0;}puts("No");return    0;
}bool    dfs(int x,int y,int sum,char color)
{for(int i = 0;i < 4;i ++){int    new_x = x + UPDATE[i][0];int    new_y = y + UPDATE[i][1];if(new_x > N || new_x < 1 || new_y > M || new_y < 1 || MAP[new_x][new_y] != color)continue;if(VIS[new_x][new_y].vis && sum - VIS[new_x][new_y].n + 1 >= 4)return    true;if(VIS[new_x][new_y].vis)continue;VIS[new_x][new_y].vis = true;VIS[new_x][new_y].n = sum + 1;if(dfs(new_x,new_y,sum + 1,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[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
char    MAP[55][55];
int    N,M;
struct    Node
{bool    vis = 0;int    n = 0;
}VIS[55][55];bool    dfs(int,int,int,char);
void    cut(int,int,char);
int    main(void)
{cin >> N >> M;for(int i = 1;i <= N;i ++)cin >> MAP[i] + 1;for(int i = 1;i <= N;i ++)for(int j = 1;j <= M;j ++){if(MAP[i][j] == '.')continue;VIS[i][j].vis = VIS[i][j].n = 1;if(dfs(i,j,1,MAP[i][j])){puts("Yes");return    0;}VIS[i][j].vis = VIS[i][j].n = 0;cut(i,j,MAP[i][j]);}puts("No");return    0;
}bool    dfs(int x,int y,int sum,char color)
{for(int i = 0;i < 4;i ++){int    new_x = x + UPDATE[i][0];int    new_y = y + UPDATE[i][1];if(new_x > N || new_x < 1 || new_y > M || new_y < 1 || MAP[new_x][new_y] != color)continue;if(VIS[new_x][new_y].vis && sum - VIS[new_x][new_y].n + 1 >= 4)return    true;if(VIS[new_x][new_y].vis)continue;VIS[new_x][new_y].vis = true;VIS[new_x][new_y].n = sum + 1;if(dfs(new_x,new_y,sum + 1,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 = 0;i < 4;i ++){int    new_x = x + UPDATE[i][0];int    new_y = y + UPDATE[i][1];if(new_x > N || new_x < 1 || new_y > M || new_y < 1 || 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[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int    N,M;
char    MAP[55][55];
bool    VIS[55][55];
int    BACK_X,BACK_Y;bool    dfs(int i,int j,char color);
int    main(void)
{cin >> N >> M;for(int i = 1;i <= N;i ++)cin >> MAP[i] + 1;for(int i = 1;i <= N;i ++)for(int j = 1;j <= M;j ++){VIS[i][j] = 1;if(dfs(i,j,MAP[i][j])){puts("Yes");return    0;}VIS[i][j] = 0;}puts("No");return    0;
}bool    dfs(int i,int j,char color)
{int    back_x = BACK_X;int    back_y = BACK_Y;for(int k = 0;k < 4;k ++){int    next_x = i + UPDATE[k][0];int    next_y = j + UPDATE[k][1];if(next_x > N || next_x < 1 || next_y > M || next_y < 1 ||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] = 1;if(dfs(next_x,next_y,color))return    true;VIS[next_x][next_y] = 0;BACK_X = back_x;BACK_Y = back_y;}return    false;
}

转载于:https://www.cnblogs.com/xz816111/p/4452166.html

CF Fox And Two Dots (DFS)相关推荐

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

    题目传送门 1 /* 2 DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 3 */ 4 #include <cstdio> 5 #include <io ...

  2. CF510B Fox And Two Dots

    题目描述 Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are play ...

  3. CF 732F Tourist Reform——v-SCC+dfs

    题目:http://codeforces.com/contest/732/problem/F 给无向图定向使得从每个点出发能去的点数最小值最大. SCC.点内部dfs定向.点间以siz最大的为起点反向 ...

  4. cf 723D Lakes in Berland(dfs)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. CodeForces Round #290 Div.2

    A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. 1 #include <cstdio> 2 3 int main() 4 { 5 //freopen(" ...

  6. 搜索专题-----bfs、dfs模板,栈,队列

    P1443 马的遍历 题目描述 有一个 n×m 的棋盘,在某个点 (x,y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步. 输入格式 输入只有一行四个整数,分别为 n,m,x,y. 输 ...

  7. 【搜索】搜刷刷题整理

    包含洛谷和vjudgekuangbin题目(慢慢更新中) 洛谷 1.马的遍历 题目链接:https://www.luogu.com.cn/problem/P1443 题意:给你一个n×m的棋盘,然后给 ...

  8. 深搜+回溯+广搜小结

    深搜 按照一定的顺序和规则,一直往深处走,直到走不通再返回,换一种路径重复上述步骤. 深搜一般可以找到问题的所有答案,但问题规模较大时,解集树的深度就会比较大并且比较宽,时间复杂度就会较高.与广搜相比 ...

  9. 暑假搜索专题1(搜索)补题

    A - Fox And Two Dots(CF510B) 最后突然发现有个特判,为此t了好几发 #include<stdio.h> #include<string.h> #in ...

最新文章

  1. ICRA 2021自动驾驶相关论文汇总 | 科研党看过来,全文干货
  2. Vue.Draggable 实现组件拖拽
  3. Java 函数式编程和 lambda 表达式
  4. linux无线网卡消失,linux下wpa/wpa2的无线网卡设置 [暂时还没有证实是否能用]
  5. 视觉SLAM十四讲学习笔记-第三讲-相似、仿射、射影变换和eigen程序、可视化演示
  6. 新华三助力公安构建新IT“警盾”
  7. 10-300-020-简介-架构-简介
  8. SpringBoot动态切换数据源-快速集成多数据源的启动器
  9. 再谈软件研发管理体系建设
  10. 详解自动驾驶安全软件开发流程
  11. 基于微信驾校考试小程序系统设计与实现 开题报告
  12. 背包客旅行札记-html
  13. 洛谷3356火星探险问题
  14. 论文相关-MATHTYPE字体对应
  15. 合肥工业大学计算机培养计划,合肥工业大学
  16. ELK - docker
  17. 根据月份,计算当月周数(非自然周)
  18. qt 设置进程优先级_如何设置最低优先级的进程?
  19. 修改人人商城服务器时间,修改收货地址 · 人人商城二次开发常用文档,超详细,微擎开发微擎二次开发【持续更新】 · 看云...
  20. Kafka原理+操作+实战

热门文章

  1. ACM字符串处理算法经典:字符串搜索
  2. winform程序的皮肤问题
  3. java设计模式学习3--Command Pattern[原创]
  4. ios获取区域服务器信息,ios获取服务器数据
  5. python驱动级模拟按键大师_AB叔_C#驱动级模拟按键操作
  6. html5 app 原理,html5打包成app应用的原理是什么?
  7. mysql临时表如何分页查询慢_面试官扎心一问:数据量很大,分页查询很慢,有什么优化方案?...
  8. 全局中断_【安全圈】微软更新造成Office 365等多个在线服务中断!
  9. java applet运行jmx,通过tomcat设置jvm及添加jmx远程访问、gc输出日志
  10. android百度地图获取定位信息吗,android使用百度地圖定位(獲取當前經緯度和地址信息)...