xtu summer individual 3 C.Infinite Maze
2 seconds
256 megabytes
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.
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.
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
5 4
##.#
##S#
#..#
#.##
#..#
Yes
5 4
##.#
##S#
#..#
..#.
#.##
No
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的更多相关文章
- [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 ...
- CodeForces 196B Infinite Maze
Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Infinite Maze
从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点.这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远. 搜索的坐标(x,y),x的绝对值可能大于n,的 ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- 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 ...
随机推荐
- tsconfig.json No inputs were found in config file
Build:No inputs were found in config file '/tsconfig.json'. Specified 'include' paths were '["* ...
- 微信小程序开发常见的拉起外部地图软件进行导航的功能
<view class="dh" bindtap="mapNavigation" data-addr="{{address}}"> ...
- fscanf
fscanf (PHP 4 >= 4.0.1, PHP 5, PHP 7) fscanf — 从文件中格式化输入 说明 mixed fscanf ( resource $handle , str ...
- .net 音频转换 .amr 转 .mp3 (七牛转换法)
.amr 用于移动设备的音频,压缩比比较大,多用于人声.通话,效果还行!所以,移动设备多采用amr格式来进行录存!比较常见的例子:通话录音,微信语音以及录音等! 这个鬼,用两个字来形容,就是“蛋疼”: ...
- 【C#】为什么有可能会被多个线程修改的对象要加线程锁
例1.不用线程锁的情况下,两个线程对同一个变量进行加减操作 static void Main(string[] args) { Counter counter = new Counter(); var ...
- SpringBoot项目不占用端口启动
@EnableScheduling @SpringBootApplication public class Application { public static void main(String[] ...
- String的用法——构造方法
package cn.itcast_01; /* 字符串:就是多个字符组成的一串数据,也可以看成一个字符数组 *通过API,我们得知: A: 字符串字面值,如abc,也可以看做一个对象 B:字符串是常 ...
- Implicit Animations 默认动画 读书笔记
Implicit Animations 默认动画 读书笔记 Do what I mean, not what I say. Edna Krabappel, The Simpsons Part I ...
- XSS漏洞扫描经验分享
关于XSS漏洞扫描,现成的工具有不少,例如paros.Acunetix等等,最近一个项目用扫描工具没有扫出漏洞,但还是被合作方找出了几个漏洞.对方找出的漏洞位置是一些通过javascript.ajax ...
- 大数据开发学习之构建Hadoop集群-(0)
有多种方式来获取hadoop集群,包括从其他人获取或是自行搭建专属集群,抑或是从Cloudera Manager 或apach ambari等管理工具来构建hadoop集群等,但是由自己搭建则可以了解 ...