Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (x, y) is a wall if and only if cell is a wall.
In this problem is a remainder of dividing number a by number b.
The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y), provided that the cell he goes to is not a wall.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy used to cyclically tile the plane.
Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point.
The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.
Output
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
Examples
5 4
##.#
##S#
#..#
#.##
#..#
Yes
5 4
##.#
##S#
#..#
..#.
#.##
No
Note
In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.
In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.
题意:给了一个迷宫,迷宫中只有路的地方可以走,在迷宫的四周也都布满了一样的迷宫, 问可不可以一直无限的走下去
思路:在走迷宫的时候用两个坐标来标记,一个是实际的坐标(可能会有负的,可能会有超过n和m的),还有一个是对应到题目给的迷宫上的坐标,设走的点的实际的坐标是nx和ny,那么这个坐标对应在题目给的迷宫里面的坐标的就是(nx%n+n)%n和(ny%m+m)%m,在搜索的时候通过记录的实际的坐标点的结构体来判断这个点的对应在题目给的图里面有没有对应的,如果有的话就说明这个点已经走过了,如果在走过的情况下并且已经走过两次了,那么就说明这个是可以无限的走的,没有的话那么就要把这个实际的坐标点放进去,用来判断接下去会不会走过这些点
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include <set>
#include<queue>
#include<map>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = ;
const ll mod = 1e9+;
const double eps = 1e-; struct node
{
int x,y;
bool operator == (const node &a) const {
return x==a.x && y==a.y;
}
};
int n,m;
char a[maxn][maxn];
node vis[maxn][maxn];
int v[maxn][maxn];
int dx[] = {,-,,};
int dy[] = {,,,-};
bool bfs(int sx,int sy)
{
memset(v,,sizeof v);
queue<node>que;
v[sx][sy] = true;
vis[sx][sy] = node{sx,sy};
que.push(node{sx,sy});
while(que.size())
{
node p = que.front();
que.pop();
for(int i=;i<;i++)
{
int nx = p.x + dx[i];
int ny = p.y + dy[i];
int rx = (nx%n+n)%n;
int ry = (ny%m+m)%m;
if(a[rx][ry] == '#')
continue;
node next = node {nx,ny};
if(v[rx][ry])
{
if(!(next == vis[rx][ry]))
return true;
}
else
{
v[rx][ry] = true;
vis[rx][ry] = next;
que.push(node{nx,ny});
}
}
}
return false;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
cin>>a[i];
int sx,sy;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j] == 'S')
{
sx = i;
sy = j;
}
}
}
if(bfs(sx,sy))
puts("Yes");
else
puts("No");
return ;
}
Infinite Maze CodeForces - 196B的更多相关文章
- CodeForces 196B Infinite Maze
Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [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 ...
- Codeforces 197D - Infinite Maze
197D - Infinite Maze 思路:bfs,如果一个点被搜到第二次,那么就是符合要求的. 用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜 ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Infinite Maze
从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点.这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远. 搜索的坐标(x,y),x的绝对值可能大于n,的 ...
- 【codeforces 196B】Infinite Maze
[题目链接]:http://codeforces.com/problemset/problem/196/B [题意] 给你一个n*m的棋盘; 然后你能够无限复制这个棋盘; 在这个棋盘上你有一个起点s; ...
- V - Infinite Prefixes CodeForces - 1295B math
天哪!!菜到家啦. 数学+思维. 首先求出一个周期内cnt0-cnt1=c的个数,如果C=0,那么只要在一个周期内有前缀等于x,那么答案就是-1,否则答案就是0 如果C!=0,列一下方程x=t*c+a ...
- Codeforces Round #124 (Div. 2)
A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...
- HZNU 2019 Summer training 6 -CodeForces - 622
A - Infinite Sequence CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...
随机推荐
- Excel数据导入Sql Server,部分数字为Null
在Excel中,我们时常会碰到这样的字段(最常见的就是电话号码),即有纯数字的(如没有带区号的电话号码),又有数字和其它字符混合 (如“区号-电 话号码”)的数据,在导入SQLServer过程中,会发 ...
- C语言学习之第一个程序
#include<stdio.h> int main() { printf("This is my first program!\n"); ; } 基本格式大致如下: ...
- DevExpress控件水印文字提示 z
ButtonEdit.Properties.NullValuePrompt = "提示"; ButtonEdit.Properties.NullValuePromptShowFor ...
- github 专案介绍 – Python 范例:透过互动式的 Jupyter 和数学解释流行的机器学习演算法
对于机器学习有兴趣,不少人应该会先从 Andrew Ng ( 吴恩达 ) 的机器学习课程开始,但是吴恩达的课程是使用 octave 这个工具当作练习.这个 github 项目包含使用 Python 实 ...
- Linux--LAMP平台搭建
LAMP平台搭建 准备工作 rpm -e php php-cli php-ldap php-commn php-mysql --nodeps 删除php相关依赖软件 rpm -ivh zlib-dev ...
- selenium+python 连接数据库
import MySQLdb connet=MySQLdb.connect( host='localhost', port='8808', user='amdin', password='** ...
- 我的HTML总结之常用基础便签
HTML:是Hyper Text Markup Language(超级文本标记语言)的缩写,HTML不是一种程序,只是一种控制网页中数据显示的标识语言. HTML由一组标签组成. HTML的基本结构 ...
- 插上翅膀,让Excel飞起来——xlwings(一)
python操作Excel的模块,网上提到的模块大致有:xlwings.xlrd.xlwt.openpyxl.pyxll等,他们提供的功能归纳起来有两种:一.用python读写Excel文件,实际上就 ...
- 一个SAP开发人员的2017总结
今年的生活如此丰富多彩,不写一点什么怎么对得起这过去的一年? 就当记流水账,若干年之后回来看一定很有意思. 1月 有幸成为SAP Community上2位来自China的SAP mentor之一: 3 ...
- luogu4566 [Vani有约会]雨天的尾巴
题目 线段树合并的板子题目了,写一写对线段树合并的理解 首先线段树合并就是把一大堆权值线段树合并起来的算法 尽管复杂度看起来并不是非常科学,但是确是非常优秀的\(O(nlogn)\) 主要的写法两种 ...