Codeforces 197D - Infinite Maze
思路:bfs,如果一个点被搜到第二次,那么就是符合要求的。
用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜到的位置,用vis[(next.x%n+n)%n][(next.y%m+m)%m]标记,因为位置可以为负数(绝对值很大的负数)。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=;
const int INF=0x3f3f3f3f;
char Map[N][N];
int n,m;
int sx,sy;
int dir[][]={,,,,,-,-,};
struct node
{
int x,y;
}vis[N][N];
bool bfs(int x,int y)
{
node now,next;
now.x=x;
now.y=y;
queue<node>q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(Map[(next.x%n+n)%n][(next.y%m+m)%m]!='#')
{
if(vis[(next.x%n+n)%n][(next.y%m+m)%m].x==INF)
{
vis[(next.x%n+n)%n][(next.y%m+m)%m].x=next.x;
vis[(next.x%n+n)%n][(next.y%m+m)%m].y=next.y;
//cout<<next.x<<' '<<next.y<<endl;
q.push(next);
}
else if(next.x!=vis[(next.x%n+n)%n][(next.y%m+m)%m].x||next.y!=vis[(next.x%n+n)%n][(next.y%m+m)%m].y)
{
return true;
}
}
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
memset(vis,INF,sizeof(vis));
cin>>n>>m;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>Map[i][j];
if(Map[i][j]=='S')
sx=i,sy=j;
}
}
if(bfs(sx,sy))cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return ;
}
Codeforces 197D - Infinite Maze的更多相关文章
- CodeForces 196B Infinite Maze
Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [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 ...
- 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 ...
- 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 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- BCB 按钮添加背景图
使用控件:TBitBtn 位于 Additional分类 属性:GlyPh
- win10安装激活与关闭自动更新
1.安装时即使选择了不保留任何文件,也不会删除掉非c盘里的东西 2.安装后需要执行KMS10_Crack2激活下 3.关闭自动更新. 在服务中禁用 https://zhidao.baidu.com/q ...
- django登录功能(简单在POST请求)
第一 先在templates中创立index.html !DOCTYPE html> <head> <meta charset="UTF-8"> & ...
- CAScrollLayer
CAScrollLayer 对于一个未转换的图层,它的bounds和它的frame是一样的,frame属性是由bounds属性自动计算而出的,所以更改任意一个值都会更新其他值. 但是如果你只想显示一个 ...
- Linux 安装gcc、gcc-c++编译器
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) 方式一:yum安装 使用ISO制作yum源:Linux 使用系统ISO制作yum源 y ...
- Python实现在给定整数序列中找到和为100的所有数字组合
摘要: 使用Python在给定整数序列中找到和为100的所有数字组合.可以学习贪婪算法及递归技巧. 难度: 初级 问题 给定一个整数序列,要求将这些整数的和尽可能拼成 100. 比如 [17, 1 ...
- Python: collections.nametuple()--映射名称到序列元素
问题: 通过下标访问列表或者元组中元素 answer: collections.namedtuple()通过使用元组对象来解决这个问题 这个函数实际上是一个返回Python中标准元组类型子类的一个工 ...
- Kettle 学习导航帖整理
最近在学习Kettle,期间收集了很多帖子,在此整理汇总以备后续查询或分享,如果有更好的学习资源也欢迎在评论区留言,谢谢. Kettle入门: Kettle简介:百度百科https://baike.b ...
- Linux服务器配置---ntp
配置ntp ntp就是网络时间同步的服务,时间的准确性非常重要,很多数据在记录时都要知道准确的时间.网上有很多站点,一般国内会设置匹配中科院国家授时中心的时间. 1.安装ntp软件 [root ...
- Linux笔记 #07# 搭建机器学习环境
环境: Debian 8.8 64位, 同样适用 win10 基本步骤: 安装 Python 安装必要的库 测试 一.安装 Python 延续之前的 搭建 Python 环境 选取折中版本 Pytho ...