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++ ...
随机推荐
- Angular5+ 自定义表单验证器
Angular5+ 自定义表单验证器 Custom Validators 标签(空格分隔): Angular 首先阐述一下遇到的问题: 怎样实现"再次输入密码"的验证(两个cont ...
- pringMvc-使用原生api
在springMvc有时候需要使用原生的api: @RequestMapping(value="/testApi") public String testApi(HttpServl ...
- 如何在Chrome development tool里查看C4C前台发送的请求细节
我们可以在Chrome development tool的network tab里观察到从前台UI发送到后台的HTTP请求: 更多Chrome Development Tool的使用工具请查看我的博客 ...
- IOS 照片浏览器总结(思想步骤)
1. 界面分析========================================1> 需要读取或修改属性的控件需要设置属性// 序号标签// 图片// 图片描述// 左边按钮// ...
- doppia代码结构
代码地址:https://bitbucket.org/rodrigob/doppia/src stereo_matching下的几个目录相当于这几篇论文中求stixel的几个步骤 cost_volum ...
- php new self()关键字的用法
今天开框架源码,发现有用到new self()的用法 有点不懂 在网上查了一下,给大家说一下: 在类中 self的用法 和this的用法差不多 , php new self() 一般在类内部使用 ...
- 【luogu P4462 [CQOI2018]异或序列】 题解
题目链接:https://www.luogu.org/problemnew/show/P4462 ax+ax-1+...+ay = cntx+cnty 这样把一段序列变成两段相加跑莫队. #inclu ...
- javascript操作Date对象
Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. 参数形式有以下5种: new Dat ...
- html基础用法(上)
html的定义: html超文本标记语言,标准通用标记语言下的一个应用. “超文本”就是指页面内可以包含图片,链接,甚至音乐,程序等非文字语言. 超文本标记语言的结构包括“头”部分(head),和“主 ...
- vue2高仿饿了么app
Github地址: https://github.com/ccyinghua/appEleme-project 一.构建项目所用: vue init webpack appEleme-project ...