CodeForces 196B Infinite Maze
2 seconds
256 megabytes
standard input
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的迷宫,在一个平面上有无限个这种迷宫,每个迷宫的上下左右都是完全相同的这种n*m的迷宫(方向和里面的布局都完全一样),也就是说以起始点为原点,所有(x,y)与(x%n,y%m)的内容(不包括‘s’)是相同的(模了数,迷宫在平面上周期性排列,若x<0时要(x%n+n)%n,y<0时同理),比如在这个迷宫走到了边界位置,如果另一个迷宫对应位置没有墙,就可以从另一个迷宫下边界进入,问是否能一直走下去使得离出发点的欧拉距离无限远
思路:以原点为坐标,若能走到无限远处,则在途中必定会在不同的迷宫中经过相同的对应点(有点类似同余模,把当前坐标模了之后可以对应相同的坐标),所以问题就转换为是否能找到不同的坐标有相同的对应点,若有则可以走到无限远,否则就不行

再放个test25的样例,这个要输出Yes
12 12
##.#######.#
#..#......S#
#.#..#######
..#.###.....
##..##..####
#..##..#####
..##..#.....
###..##.####
##..#...####
#..##.######
..##..####..
##...#####.#
#include<bits/stdc++.h>
using namespace std;
const int amn=2e3,inf=0x3f3f3f3f;
int n,m;
char mp[amn][amn];
bool f;
int dt[][]={{,},{,-},{,},{-,}},stx,sty;
struct node{
int x, y;
};
node used[amn][amn];
queue<node> q;
void bfs(int sx,int sy){
node a;
a.x=sx;
a.y=sy;
used[sx][sy].x=sx;
used[sx][sy].y=sy;
q.push(a);
int x,y,dx,dy;
while(q.size()){
a=q.front();q.pop();
for(int i=;i<;i++){
dx=a.x+dt[i][];
dy=a.y+dt[i][];
x=(dx%n+n)%n,y=(dy%m+m)%m; ///把负数坐标变为正数 x=(x%n+n)%n;负数取模会输出负数这时加上模数就是正数的对应值如-6%3=0,(3-0)%3=0,-5%3=-2,(3-2)%3=1,-4%3=-1,(3-1)%3=2...
//cout<<x<<' '<<y<<endl;
if(mp[x][y]=='#')continue;
if(used[x][y].x==inf){
used[x][y].x=dx;
used[x][y].y=dy;
node po;
po.x=dx;
po.y=dy;
q.push(po);
}
else if(used[x][y].x!=dx||used[x][y].y!=dy){ ///如果当前坐标模出来后能对应一个不同的坐标,则是可以走到无限远的
f=;
return ;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>mp[i][j];
if(mp[i][j]=='S'){stx=i,sty=j;}
used[i][j].x=inf;
used[i][j].y=inf;
}
}
f=;
bfs(stx,sty);
if(f)printf("Yes\n");
else printf("No\n");
}
/***
给出一个n*m的迷宫,在一个平面上有无限个这种迷宫,每个迷宫的上下左右都是完全相同的这种n*m的迷宫(方向和里面的布局都完全一样),
也就是说以起始点为原点,所有(x,y)与(x%n,y%m)的内容(不包括‘s’)是相同的(模了数,迷宫在平面上周期性排列,若x<0时要(x%n+n)%n,y<0时同理),
比如在这个迷宫走到了边界位置,如果另一个迷宫对应位置没有墙,就可以从另一个迷宫下边界进入,问是否能一直走下去使得离出发点的欧拉距离无限远 以原点为坐标,若能走到无限远处,则在途中必定会在不同的迷宫中经过相同的对应点(有点类似同余模,把当前坐标模了之后可以对应相同的坐标),所以问题就转换为
是否能找到不同的坐标有相同的对应点,若有则可以走到无限远,否则就不行
***/
CodeForces 196B Infinite Maze的更多相关文章
- 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 - 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 ...
- 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; ...
- codeforces 622A Infinite Sequence
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 123 E Maze
Discription A maze is represented by a tree (an undirected graph, where exactly one way exists betwe ...
- Codeforces 377 A Maze【DFS】
题意:给出n*m的矩阵,矩阵由'.'和'#'组成,再给出k,表示需要在'.'处加k堵墙,使得剩下的'.'仍然是连通的 先统计出这个矩阵里面总的点数'.'为sum 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- Oracle介绍
Published: 2016-11-08 22:15:00 In Data Mining. tags: SQL 版本与配置 企业版 标准版 个人版 事务性数据表 分析型数据表 PL/SQL 配置 控 ...
- 修改android项目sdk版本
1.右键单击项目--->properties---->Resource----->Android在Project Bulid Target对话框中选择你需要的Android版本.2. ...
- USB小白学习之路(12) Cy7c68013A固件之Slave FIFO(转)
Cy7c68013固件之Slave FIFO 转自:http://blog.csdn.net/zengshaoqing/article/details/53053539 选择SlaveFIFO传输方式 ...
- LeetCode---二叉树3-总结例题
二叉树-总结例题 1-从中序与后序遍历序列构造二叉树 给定二叉树的后序遍历和二叉树的中序遍历 想法: 先根据后序遍历的最后一个元素构造根节点 寻找根节点在中序遍历中的位置 递归构建根节点的左右子树 / ...
- 神奇的background——绘制图形
相信大家在平时工作中少不了会被要求在某些元添加一些特殊的背景图片,这时候通常就拿起ps就是切切切.不说这种方式麻烦,有ui给你切好的情况已经不错,没有的就有自己动手.还可能有需要切一整张超大图的情况. ...
- 【30分钟学完】canvas动画|游戏基础(6):坐标旋转探究
前言 本篇主要讲坐标旋转及其应用,这是编程动画必不可少的技术. 阅读本篇前请先打好前面的基础. 本人能力有限,欢迎牛人共同讨论,批评指正. 坐标旋转 模拟场景:已知一个中心点(centerX,cent ...
- 钉钉小程序不用canvas在后端绘图前端用image标签获取图片的实践
公司的需求要用电子员工卡代替用了N久的工作证,在各种场合刷二维码来代替刷卡.在钉钉小程序里实现.感觉这回又要躺坑里了. 钉钉小程序第一次做.我这个自封的GDI+大神才不要想用钉钉jsapi的方式用ca ...
- 关于java性能优化细节方面的建议
在Javva程序中,性能问题的大部分原因并不在于Java语言,而是程序本身,养成一个良好的编码习惯非常重要,能够显著地提升程序性能.下面来聊聊该方面的建议: 1.尽量在合适的场合使用单例: 所谓单例, ...
- 必备技能六、Vue框架引入JS库的正确姿势
在Vue.js应用中,可能需要引入Lodash,Moment,Axios,Async等非常好用的JavaScript库.当项目变得复杂庞大,通常会将代码进行模块化拆分.可能还需要跑在不同的环境下,比如 ...
- Generator的异步编程
对比下常用的异步处理的方案: 1,回调 我们常说的 “回调地狱”,就是多个异步操作时候,代码多重嵌套,异步之前形成强耦合,如果修改一处,其他地方也是跟着修改.(callback hell). 2,pr ...