题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素)

题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功。

  1. #include<cstdio>
  2. #include<stdio.h>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<cstring>
  8. #include<vector>
  9. #include<queue>
  10. #define INF 0x3f3f3f3f
  11. #define MAX 1005
  12.  
  13. using namespace std;
  14.  
  15. char Map[MAX][MAX],op;//op记录当前字符
  16.  
  17. struct node
  18. {
  19. int x,y;
  20. }pre[MAX][MAX];//pre数组记录当前点的前驱节点
  21. int n,m,vis[MAX][MAX],ok,v[][]={{,},{-,},{,},{,-}};
  22.  
  23. int check(int x,int y,int a,int b)
  24. {
  25. if(a>= && a<n && b>= && b<m && (pre[x][y].x!=a ||pre[x][y].y!=b) && Map[a][b]==op)
  26. return ;
  27. return ;
  28. }
  29.  
  30. void DFS(int x,int y)
  31. {
  32. for(int i=;i<;i++)
  33. {
  34. int a=x+v[i][];
  35. int b=y+v[i][];
  36. if(check(x,y,a,b))
  37. {
  38. if(vis[a][b])
  39. {
  40. ok=;
  41. return;
  42. }
  43. pre[a][b].x=x;
  44. pre[a][b].y=y;
  45. vis[a][b]=;
  46. DFS(a,b);
  47. }
  48. }
  49. if(ok==)
  50. return;
  51. }
  52.  
  53. int main()
  54. {
  55. int i,j;
  56. while(scanf("%d%d",&n,&m)!=EOF)
  57. {
  58. ok=;
  59. memset(vis,,sizeof(vis));
  60. for(i=;i<MAX;i++)
  61. {
  62. for(j=;j<MAX;j++)//初始化每个点的前驱点都是本身
  63. {
  64. pre[i][j].x=i;
  65. pre[i][j].y=j;
  66. }
  67. }
  68. for(i=;i<n;i++)
  69. scanf("%s",Map[i]);
  70. for(i=;i<n;i++)
  71. {
  72. for(j=;j<m;j++)
  73. {
  74. if(!vis[i][j])
  75. {
  76. op=Map[i][j];
  77. vis[i][j]=;
  78. DFS(i,j);
  79. }
  80. if(ok)
  81. break;
  82. }
  83. if(ok)
  84. break;
  85. }
  86. if(ok)
  87. printf("Yes\n");
  88. else
  89. printf("No\n");
  90. }
  91. return ;
  92. }

CodeForces 510B DFS水题的更多相关文章

  1. Tree Requests CodeForces - 570D (dfs水题)

    大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...

  2. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  3. 【wikioi】1229 数字游戏(dfs+水题)

    http://wikioi.com/problem/1229/ 赤裸裸的水题啊. 一开始我认为不用用完全部的牌,以为爆搜会tle.. 可是我想多了. 将所有状态全部求出,排序后暴力判断即可. (水题有 ...

  4. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  5. DFS水题 URAL 1152 False Mirrors

    题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...

  6. CodeForces 705A Hulk (水题)

    题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...

  7. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

  8. CodeForces 705B (训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...

  9. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

随机推荐

  1. grunt学习随笔

    1 grunt 安装  全局安装 npm install -g grunt-cli 2 配置好package.json 和 Gruntfile 文件,这两个文件必须位于项目根目录下. 2.1packa ...

  2. JS的一些常见验证代码

    1//檢查空串  2function isEmpty(str){  3 if((str == null)||(str.length == 0)) return (true);  4 else retu ...

  3. Mysql授权远程登录

    在命令行输入如下命令即可: Grant all privileges on *.* to ' with grant option; 再执行 flush privileges

  4. 第二次冲刺spring会议(第一次会议)

    [例会时间]2014/5/4  21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇 内部测试版发布时间5月11日 ...

  5. spring之json数据的接受和发送

    配置spring对json的注解方式. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springf ...

  6. python--sorted函数和operator.itemgetter函数

    1.operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1 ...

  7. 重复T次的LIS的dp Codeforces Round #323 (Div. 2) D

    http://codeforces.com/contest/583/problem/D 原题:You are given an array of positive integers a1, a2, . ...

  8. mysql字符集问题

    背景:数据库表信息乱码问题 影响:数据库连接初始化中断 原因:init_connect参数设置问题,参数为不可执行语句. 1.1 DB字符集参数 #数据库中的字符集设置(以下全部为修改过后的结果) m ...

  9. Linux内核协议栈 NAT性能优化之FAST NAT

    各位看官非常对不起,本文是用因为写的,如果多有不便敬请见谅 代码是在商业公司编写的,在商业产品中也不能开源,再次抱歉   This presentation will highlight our ef ...

  10. php练习1

    <?php$conn=mysqli_init();if(!conn){ echo "mysqli_init error"; exit(0);}$ret=mysqli_real ...