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 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- kibana增加验证
Kibana从5.5开始不提供认证功能,想用官方的认证X-Pack收费 ... 自己动手吧,用nginx的代理加apache生成的密码认证文件.环境:ubuntu16.04 安装nginxapt-ge ...
- O2O倒逼汽车行业改革:服务超越销售成重心
汽车作为越来越普及的出行工具,已经进入千家万户.与其他家具.家电.服饰等产品不同,从新车销售到售后保养.保险购买.维修乃至报废阶段,都能细分化,形成一个个潜力巨大的垂直领域.而随着互联网元素的渗入,汽 ...
- C++走向远洋——47(第十二周、运算符重载基础程序、阅读)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Java后端完整学习路线及资源记录
Java后端完整学习路线及资源记录 Java语法基础 书籍教程: 视频教程: IDEA的使用 视频教程:JAVA开发利器-IntelliJ IDEA使用教程 Servlet和Web基础 书籍教程: 视 ...
- 使用Taiko + Gauge进行自动化测试(一)
目录 初识Taiko 环境安装 尝试Taiko taiko 执行过程 结合Gauge编写用例 使用Gauge 总结 初识Taiko 先来了解一下什么是Taiko:"Taiko是一个免费的开源 ...
- 7-10 jmu-python-异常-学生成绩处理基本版 (15 分)
小明在帮老师处理数据,这些数据的第一行是n,代表有n行整数成绩需要统计.数据没有错误,则计算平均值(保留2位小数)并输出.数据有错误,直接停止处理,并且不进行计算. 注:该程序可以适当处理小错误,比如 ...
- JAVA GC算法详解
生存还是死亡 对象是否需要被垃圾收集器回收主要有两种方式:引用计数法和可达性分析算法 引用计数法 给对象添加一个引用计数器,每当有一个地方引用他的时候,计数器的数值就+1,当引用失效时,计数器就-1: ...
- 正式学习MVC 02
1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...
- C#编程_单线程IP地址解析
单线程IP地址解析 目标程序 界面如下图 设计方法:完成单个IP地址解析,循环调用方法,完成扫描. 注意用stopwatch计算时间. 实现思路 先知道怎么解析一个单个的IP地址. 用循环的方法解 ...
- 不要写很酷但同事看不懂的Java代码
你好呀,我是沉默王二,一个和黄家驹一样身高,和刘德华一样颜值的程序员.为了提高 Java 编程的技艺,我最近在 GitHub 上学习一些高手编写的代码.下面这一行代码(出自大牛之手)据说可以征服你的朋 ...