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++ ...
随机推荐
- redis缓存技术在java中的应用
https://blog.csdn.net/zhh1072773034/article/details/78538012
- Spyder更改默认工作路径已经文件路径
打开spyder,选择菜单栏中的Tools--->Preferences--->Current working directory 然后选择最下面的单选按钮The following ...
- Angular 5.x 学习笔记(1) - 模板语法
Angular 5.x Template Syntax Learn Note Angular 5.x 模板语法学习笔记 标签(空格分隔): Angular Note on github.com 上手 ...
- June 07th 2017 Week 23rd Wednesday
Failure is the condiment that gives success its flavor. 失败是让成功变美味的调味料. There are kinds of flavors in ...
- 最重要的“快捷键” IntelliJ IDEA
转载:http://www.youmeek.com/intellij-idea-part-iii-hotkeys-explain/ @IntelliJ IDEA第三部分视频教程:最重要的“快捷键”专讲 ...
- C语言 条件编译(if )
#include <stdio.h> #define NUM -1 int main(int argc, const char * argv[]) { #if NUM > 0 pri ...
- UOJ 35 后缀数组
后缀数组裸题,求排名第1~n的后缀,想相邻后缀的最长公共前缀. 集训队模板就是硬lO(∩_∩)O哈哈~ #include <cstdio> #include <cmath> # ...
- LA 4987 背包
题意: 有n个施工队,给定他们的位置,有m个防空洞,给定位置,求将施工队放到m个防空洞里面,最少的总距离? n<=4000 分析: dp[i][j] 前 i 个施工队,放到前 j 个防空洞里面的 ...
- 【洛谷P3811】[模板]乘法逆元
乘法逆元 题目链接 求逆元的三种方式: 1.扩欧 i*x≡1 (mod p) 可以化为:x*i+y*p=1 exgcd求x即可 inline void exgcd(int a,int b,int &a ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...