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 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- Android常用权限permission列表摘录
一个Android应用程序需要权限才能调用某些android系统的功能:一个android应用也可能被其他应用调用,因此也需要声明调用自身所需要的权限.除了平时常用的权限记得比较熟悉,还有很多的权限一 ...
- js实现轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- UVM中的driver组件
一般UVM环境中的Driver组件,派生自uvm_driver. uvm_dirver派生自uvm_component. class uvm_driver #(type REQ = uvm_sequ ...
- java常用功能
1.复制文件 private void fileChannelCopy(File source, File target) throws IOException { FileInputStream f ...
- java的时间处理
采用joda.time库 gradle,可以简化calendar的 compile "joda-time:joda-time:2.7" 例子:http://blog.csdn.ne ...
- JSON自动生成相关类
开源项目地址:https://jsonclassgenerator.codeplex.com/SourceControl/latest 太好用了,这个
- 新版.Net开发必备十大工具(转)
Snippet Compiler Snippet Compiler是一个基于 Windows 的小型应用程序,你可以通过它来编写.编译和运行代码.如果你具有较小的代码段,并且你不想创建完整的 Visu ...
- Linux命令: 查找文件中的字符串
①cat filename | grep 'string' ②编辑模式查找,/string, 依次敲入下面的命令 vim filename e i ESC /string 从光标位置开始往后查找第一个 ...
- python六剑客:map()、lambda()、filter()、reduce()、推导类表、切片
一:map():映射 map()有两个参数,一个函数,一个序列,序列中每一个元素都会做为参数传给前边的函数,然后生成新的列表, 第二个参数必须用一个序列:元祖,列表,字符串 >>> ...
- jquery一句话实现快速搜索功能
jquery一句话实现快速搜索功能 //快捷搜索公共方法,其中obj为显示行的子节点function filter(obj, filterNameValue){ $(obj).hide().filte ...