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]标记,因为位置可以为负数(绝对值很大的负数)。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=;
const int INF=0x3f3f3f3f;
char Map[N][N];
int n,m;
int sx,sy;
int dir[][]={,,,,,-,-,};
struct node
{
int x,y;
}vis[N][N];
bool bfs(int x,int y)
{
node now,next;
now.x=x;
now.y=y;
queue<node>q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(Map[(next.x%n+n)%n][(next.y%m+m)%m]!='#')
{
if(vis[(next.x%n+n)%n][(next.y%m+m)%m].x==INF)
{
vis[(next.x%n+n)%n][(next.y%m+m)%m].x=next.x;
vis[(next.x%n+n)%n][(next.y%m+m)%m].y=next.y;
//cout<<next.x<<' '<<next.y<<endl;
q.push(next);
}
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)
{
return true;
}
}
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
memset(vis,INF,sizeof(vis));
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='S')
sx=i,sy=j;
}
}
if(bfs(sx,sy))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return ;
}

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. 微信小程序- wx.request请求不到数据

    小程序官方文档手册 https://mp.weixin.qq.com/debug/wxadoc/dev/ 小程序开发问答社区 http://www.henkuai.com/forum.php wx.r ...

  2. 027-chown命令

    (1)只有文件主和超级用户才可以便用该命令.

  3. cxf的使用

    java的一个rest路径包含五个部分 1.容器路径,如tomcat的文件包名,jetty的context等 2.web.xml -配置cxf或者sevlet等 3.cxf.xml 4.具体的实现类中 ...

  4. Trove系列(二)—Trove 的架构和流程介绍

    Trove主要逻辑目前Trove支持用户创建一个数据库服务实例,在实例里可以创建多个数据库并进行管理.数据库服务实例目前通过Nova API来创建,然后同样通过Nova API创建一个Volume(未 ...

  5. uva1330 在一个大的矩阵中寻找面积最大的子矩阵

    大白书 P50页 #include <algorithm> #include <cstdio> using namespace std; ; int ma[maxn][maxn ...

  6. MySQL从删库到跑路(六)——SQL插入、更新、删除操作

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.插入数据 1.为表的所有字段插入数据 使用基本的INSERT语句插入数据要求指定表名称和插入到新记录的值. IN ...

  7. 2016-2017-2 《Java程序设计》第5周学习总结

    学号 2016-2017-2 <Java程序设计>第5周学习总结 教材部分学习内容总结 第八章: 一.语法与继承架构 使用try.catch: •执行流程 1.尝试执行try区块中程序代码 ...

  8. 【VS Hacks】定制VS

    # Hack 24   定制快捷键 VS能够做很多键盘的配置,其实在VS中目前已经发现有很多的快捷键了,但是在这个技巧篇里会学到如何创建新的快捷键,以及编辑已有的快捷键.VS中包含很多的命令,只有其中 ...

  9. pyDay2

    内容来自廖雪峰的官方网站 1.dict 为什么dict查找速度这么快?因为dict的实现原理和查字典是一样的.假设字典包含了1万个汉字,我们要查某一个字,一个办法是把字典从第一页往后翻,直到找到我们想 ...

  10. 深入浅出JVM

    这篇文章简要解析了JVM的内部结构.下面这幅图展示了一个典型的JVM(符合JVM Specification Java SE 7 Edition)所具备的关键内部组件. 上图展示的所有这些组件都将在下 ...