197D - Infinite Maze

思路:bfs,如果一个点被搜到第二次,那么就是符合要求的。

用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜到的位置,用vis[(next.x%n+n)%n][(next.y%m+m)%m]标记,因为位置可以为负数(绝对值很大的负数)。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=;
  4. const int INF=0x3f3f3f3f;
  5. char Map[N][N];
  6. int n,m;
  7. int sx,sy;
  8. int dir[][]={,,,,,-,-,};
  9. struct node
  10. {
  11. int x,y;
  12. }vis[N][N];
  13. bool bfs(int x,int y)
  14. {
  15. node now,next;
  16. now.x=x;
  17. now.y=y;
  18. queue<node>q;
  19. q.push(now);
  20. while(!q.empty())
  21. {
  22. now=q.front();
  23. q.pop();
  24. for(int i=;i<;i++)
  25. {
  26. next.x=now.x+dir[i][];
  27. next.y=now.y+dir[i][];
  28. if(Map[(next.x%n+n)%n][(next.y%m+m)%m]!='#')
  29. {
  30. if(vis[(next.x%n+n)%n][(next.y%m+m)%m].x==INF)
  31. {
  32. vis[(next.x%n+n)%n][(next.y%m+m)%m].x=next.x;
  33. vis[(next.x%n+n)%n][(next.y%m+m)%m].y=next.y;
  34. //cout<<next.x<<' '<<next.y<<endl;
  35. q.push(next);
  36. }
  37. else if(next.x!=vis[(next.x%n+n)%n][(next.y%m+m)%m].x||next.y!=vis[(next.x%n+n)%n][(next.y%m+m)%m].y)
  38. {
  39. return true;
  40. }
  41. }
  42. }
  43. }
  44. return false;
  45. }
  46. int main()
  47. {
  48. ios::sync_with_stdio(false);
  49. cin.tie();
  50. memset(vis,INF,sizeof(vis));
  51. cin>>n>>m;
  52. for(int i=;i<n;i++)
  53. {
  54. for(int j=;j<m;j++)
  55. {
  56. cin>>Map[i][j];
  57. if(Map[i][j]=='S')
  58. sx=i,sy=j;
  59. }
  60. }
  61. if(bfs(sx,sy))cout<<"Yes"<<endl;
  62. else cout<<"No"<<endl;
  63. return ;
  64. }

Codeforces 197D - Infinite Maze的更多相关文章

  1. CodeForces 196B Infinite Maze

    Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. [CodeForces - 197D] D - Infinite Maze

    D - Infinite Maze We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wal ...

  3. Infinite Maze CodeForces - 196B

    We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...

  4. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  5. Infinite Maze

    从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点.这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远. 搜索的坐标(x,y),x的绝对值可能大于n,的 ...

  6. 【codeforces 196B】Infinite Maze

    [题目链接]:http://codeforces.com/problemset/problem/196/B [题意] 给你一个n*m的棋盘; 然后你能够无限复制这个棋盘; 在这个棋盘上你有一个起点s; ...

  7. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces 123 E Maze

    Discription A maze is represented by a tree (an undirected graph, where exactly one way exists betwe ...

  9. Codeforces 377 A Maze【DFS】

    题意:给出n*m的矩阵,矩阵由'.'和'#'组成,再给出k,表示需要在'.'处加k堵墙,使得剩下的'.'仍然是连通的 先统计出这个矩阵里面总的点数'.'为sum 因为题目说了一定会有一个解,所以找到一 ...

随机推荐

  1. C# 开发圆角控件(窗体)

    最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能.其中方法三既适应于控件,也适应于窗体. 先上传效果图: 方法一: 增加命名空间:using  ...

  2. js数组之迭代器方法

    迭代器方法:对数组中的每一个元素应用一个函数,可以返回一个值,一组值或者一个新的数组.说的什么啊这是,根本听不懂.实践 不生成新数组的迭代器的方法: <html> <head> ...

  3. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...

  4. select,radio,checkbox兼容性

  5. bzoj2733: [HNOI2012]永无乡 启发式合并

    地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec   ...

  6. linux下如何进入单用户模式

    忘记密码时,我们可以通过进入单用户模式修改密码. 进入单用户模式的方式: 1. 启动服务器时,按 e 键进入引导选择界面.注意:可能需要多次按 e 键切换几个个界面后,才能进入选择界面. 2. 选择以 ...

  7. java使用反射给对象属性赋值的两种方法

    java反射无所不能,辣么,怎么通过反射设置一个属性的值呢? 主程序: /** * @author tengqingya * @create 2017-03-05 15:54 */ public cl ...

  8. Salty Fish 结对学习心得体会及创意照 (20165211 20165208)

    小组结对学习心得体会及创意照 在阅读了软件工程讲义 3 两人合作(2) 要会做汉堡包和现代软件工程讲义 3 结对编程和两人合作后,加之对于这几周组队学习的感悟,我们对于组队学习的一些感悟和想法如下: ...

  9. C# 获取当前IIS请求地址

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  10. python中的迭代器和生成器学习笔记总结

    生成器就是一个在行为上和迭代器非常类似的对象.   是个对象! 迭代,顾名思意就是不停的代换的意思,迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次“迭代”,而 ...