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 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- OAuth 白话简明教程 1.简述
转自:http://www.cftea.com/c/2016/11/6702.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
- 了解SpringBoot
一.SpringBoot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- python写一个密码生成器的类,要求有个类变量,统计一下一共生成过多少个密码。 要求有4个方法,1:构造方法 2 实例方法 3 类方法 4 静态方法
生成指定长度的随机数字密码 生成指定长度的随机字母密码 生成指定长度的随机数字和字母的混合 #encoding=utf-8 import random import string class pa ...
- 第一次使用crontab linux选择编辑器问题
第一次使用crontab linux选择编辑器问题 第一次使用crontab 时,会出现no crontab for root - using an empty one“Select a editor ...
- python之路----面向对象的多态特性
多态 多态指的是一类事物有多种形态 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): #同一类事物:动物 @abc.abstr ...
- C站投稿映兔源的方法
(因映兔源也不太稳定了,所以不建议映兔上传,正在找其他视频源代替映兔,另外等待C站大大们的webbt源)(20180226) 测试换文件格式后会不会失效,能坚持几天?http://www.cnblog ...
- htpasswd命令的使用
htpasswd的基本用法 htpasswd是Apache服务器中生成用户认证的一个工具,仅说明htpasswd的用法: htpasswd参数 -c 创建passwdfile.如果passwdfile ...
- 浅谈elasticsearch 集群
elasticsearch 集群 摘要: elasticsearch 集群 搭建elasticsearch的集群 现在假设我们有3台es机器,想要把他们搭建成为一个集群 基本配置 每个节点都要进行这样 ...
- 一个简单的JavaScript实例
1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- UVA302 John's trip(欧拉回路)
UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...