B. Infinite Maze
time limit per test   

2 seconds

memory limit per test   

256 megabytes

input standard   input
output 

standard   output

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).

Sample test(s)
Input
5 4
##.#
##S#
#..#
#.##
#..#
Output
Yes
Input
5 4
##.#
##S#
#..#
..#.
#.##
Output
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的地图拼成的一个无限平面,

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const int dir[][] = {,-,,,-,,,};
struct point{
int x,y;
};
char mp[maxn][maxn];
int vs[maxn][maxn][],r,c,sx,sy;
bool vis[maxn][maxn];
queue<point>q;
int mod(int u,bool rc){
if(rc) {u = u%r;if(u < ) u += r;}
else{u = u%c;if(u < ) u += c;}
return u;
}
bool bfs(){
while(!q.empty()) q.pop();
point pt = (point){sx,sy};
q.push(pt);
int i,j,x,y,mx,my;
memset(vs,,sizeof(vs));
while(!q.empty()){
point cur = q.front();
q.pop();
for(i = ; i < ; i++){
x = dir[i][]+cur.x;
y = dir[i][]+cur.y;
mx = mod(x,true);
my = mod(y,false);
if(mp[mx][my] == '#') continue;
if(vis[mx][my]){
if(x != vs[mx][my][] || y != vs[mx][my][]) return true;
}else{
vis[mx][my] = true;
vs[mx][my][] = x;
vs[mx][my][] = y;
q.push((point){x,y});
}
}
}
return false;
}
int main(){
int i,j;
while(~scanf("%d%d",&r,&c)){
for(i = ; i < r; i++){
scanf("%s",mp[i]);
for(j = ; j < c; j++)
if(mp[i][j] == 'S'){
sx = i;sy = j;
}
}
bfs()?puts("Yes"):puts("No");
}
return ;
}

xtu summer individual 3 C.Infinite Maze的更多相关文章

  1. [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 ...

  2. Codeforces 197D - Infinite Maze

    197D - Infinite Maze 思路:bfs,如果一个点被搜到第二次,那么就是符合要求的. 用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜 ...

  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. CodeForces 196B Infinite Maze

    Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Infinite Maze

    从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点.这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远. 搜索的坐标(x,y),x的绝对值可能大于n,的 ...

  6. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  8. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  9. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

随机推荐

  1. find missing conjunction, why?

    find . -name *.c find: missing conjunction, why? SHELL会把*.c直接扩展成当前工作目录的多个.c文件,所以必须用单引号'*.c'或者/*.c进行转 ...

  2. 容器API

  3. 分享一个实用任意路数PWM函数

    一.什么是PWM? 1.科普一下什么是PWM,嘿嘿,莫闲啰嗦,好好看看,可能大多数人听过,但可能没详细了解过,至此不妨花费几分钟,详细了解哈,PWM中文译名为:脉冲宽度调制,即控制电路在输出频率不变的 ...

  4. [转]深入浅出WPF(7)——数据的绿色通道,Binding

    本文转自:http://liutiemeng.blog.51cto.com/120361/95273 小序: 怎么直接从2蹦到7啦?!啊哦,实在是不好意思,最近实在是太忙了,忙的原因也非常简单——自己 ...

  5. AJPFX总结方法重载与方法重写的区别

    方法重载在同一个类中,可以出现同名方法,但是这些同名方法的参数列表必须不同,这样定义方法叫做方法重载.方法重载的特点重载的注意事项重载与返回值无关重载与具体的变量标识符无关重载只与方法名与参数相关重载 ...

  6. P3372 【模板】线段树 1 区间查询与区间修改

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  7. Xilinx HLS

    Xilinx 的高层次综合(High Level Synthesis, HLS)技术是将C/C++/SystemC软件语言转换成Verilog或VHDL硬件描述语言的技术.现已应用在SDAccel,S ...

  8. UI常用字体定义和继承的实例,ResearchKitCode

    #import <UIKit/UIKit.h> @interface UIFont (APCAppearance) + (UIFont*) appRegularFontWithSize: ...

  9. Javaweb学习笔记7—JDBC技术

      今天来讲javaweb的第7阶段学习. JDBC技术,关于JDBC本篇博客只介绍了它的一部分,后面博客会更加深入探讨. 老规矩,首先先用一张思维导图来展现今天的博客内容.   ps:我的思维是用的 ...

  10. [Github筆記] 清除所有 Commit 紀錄

    # 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...