Infinite Maze
从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点。这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远。
搜索的坐标(x,y),x的绝对值可能大于n,的绝对值可能大于m,(x,y)对应在基础地图的位置为rx=(nx%n+n)%n,ry=(ny%m+m)%m
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 numbera 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 andm (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.
Sponsor
1 #include <stdio.h>
2 #include <string.h>
3 #include <algorithm>
4 #include <queue>
5 using namespace std;
6 #define N 2000
7 #define inf 0x3f3f3f3f
8 int a[N][N],b[N][N];
9 int f[4][2]={1,0,0,1,0,-1,-1,0};
10 char s[N][N];
11 int n,m,x,y;
12 struct node{
13 int x,y;
14 }u,v;
15 bool dfs()
16 {
17 queue<node> q;
18 u.x=x;
19 u.y=y;
20 q.push(u);
21 while(!q.empty())
22 {
23 u=q.front(); q.pop();
24 for(int i=0;i<4;i++)
25 {
26 v.x=u.x+f[i][0];
27 v.y=u.y+f[i][1];
28 int tx=(v.x%n+n)%n;
29 int ty=(v.y%m+m)%m;
30 if(s[tx][ty]!='#')
31 {
32 if(a[tx][ty]==inf)
33 {
34 a[tx][ty]=v.x;
35 b[tx][ty]=v.y;
36 q.push(v);
37 }
38 else{
39 if(a[tx][ty]!=v.x||b[tx][ty]!=v.y) return 1;
40 }
41 }
42 }
43 }
44 return 0;
45 }
46 int main()
47 {
48 while(~scanf("%d%d",&n,&m)&&(n+m))
49 {
50 for(int i=0;i<n;i++)
51 {
52 scanf("%s",s[i]);
53 for(int j=0;j<m;j++)
54 {
55 if(s[i][j]=='S')
56 {
57 x=i,y=j;
58 }
59 a[i][j]=inf;
60 b[i][j]=inf;
61 }
62 }
63 if(dfs()) printf("Yes\n");
64 else printf("No\n");
65 }
66 }
Infinite Maze的更多相关文章
- [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是取模过后的值)这个点第一次被搜 ...
- Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 196B Infinite Maze
Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 【codeforces 196B】Infinite Maze
[题目链接]:http://codeforces.com/problemset/problem/196/B [题意] 给你一个n*m的棋盘; 然后你能够无限复制这个棋盘; 在这个棋盘上你有一个起点s; ...
- Codeforces Round #124 (Div. 2)
A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...
- Meandering Through the Maze of MFC Message and Command Routing MFC消息路由机制分析
Meandering Through the Maze of MFC Message and Command Routing Paul DiLascia Paul DiLascia is a free ...
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
随机推荐
- int和Integer的区别?包装类?装箱?拆箱?
int和Integer的区别: 1) int是基本数据类型,直接存储的数值,默认是0; 2) Integer 是int的包装类,是个对象,存放的是对象的引用,必须实例化之后才能使用,默认是null; ...
- mysql高并发解决方案
mysql高并发的解决方法有: 优化SQL语句,优化数据库字段,加缓存,分区表,读写分离以及垂直拆分,解耦模块,水平切分等. 高并发大多的瓶颈在后台,在存储mysql的正常的优化方案如下: (1)代码 ...
- keycloak集群化的思考
目录 简介 keycloak中的集群 load balancing负载均衡 暴露客户端IP地址 sticky sessions 和 非sticky sessions shared databases ...
- 基于 MPI 的快速排序算法的实现
完整代码: #include <iostream> #include <cstdlib> #include <ctime> #include <algorit ...
- Openstack Nova 添加计算节点(六.一)
Openstack Nova 添加计算节点(六.一) # 重要的两点: 1 时间同步 2 yum 源 # 安装软件: yum install openstack-selinux openstack-n ...
- 内存性能测试 Memtester+mbw
Memtester简单介绍 Memtester主要是捕获内存错误和一直处于很高或者很低的坏位, 其测试的主要项目有随机值,异或比较,减法,乘法,除法,与或运算等等. 通过给定测试内存的大小和次数, 可 ...
- CTFshow萌新赛-萌新福利
下载链接文件 拿到show.bin文件 使用010Editor工具打开文件 做取反操作 取反后可以看到 把show.bin改为show.m4a 使用音频播放软件播放,即可得到flag
- 集成多种协议、用于 USBC 端口的快充协议芯片IP2723
1. 特性 快充规格 集成 QC4/QC4+输出快充协议 - 兼容 QC2.0/QC3.0 - 支持 Class B 电压等级 集成 FCP 输出快充协议 集成 SCP 输出快充协议 集成 ...
- Django实现文件在本地的存储和读取
需求介绍:将图片存入本地的电脑文件夹中,将图片的路径保存到数据库,然后通过数据库的路径读取文件: 1.文件的存入: 前端文件: <form class="form-horizontal ...
- 2021年官网下载各个版本JDK最全版与官网查阅方法
版本说明 1.安装部署JDK (1)环境 (2)官网下载JDK 由于官网的地址会随着时间的修改而更改修改下载地址,现在讲述下通用的界面操作下载JDK,以后JDK收费更严重,估计就只能下载开源的了. A ...