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 因为题目说了一定会有一个解,所以找到一 ...
随机推荐
- C# 开发圆角控件(窗体)
最近在做卡片视图的程序,要求将控件做成带有圆角的效果,下面是我在网上查找的资料,经过测试,确定可以实现功能.其中方法三既适应于控件,也适应于窗体. 先上传效果图: 方法一: 增加命名空间:using ...
- js数组之迭代器方法
迭代器方法:对数组中的每一个元素应用一个函数,可以返回一个值,一组值或者一个新的数组.说的什么啊这是,根本听不懂.实践 不生成新数组的迭代器的方法: <html> <head> ...
- cc150 --链表中倒数第k个节点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个. public class Solution { public Li ...
- select,radio,checkbox兼容性
- bzoj2733: [HNOI2012]永无乡 启发式合并
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec ...
- linux下如何进入单用户模式
忘记密码时,我们可以通过进入单用户模式修改密码. 进入单用户模式的方式: 1. 启动服务器时,按 e 键进入引导选择界面.注意:可能需要多次按 e 键切换几个个界面后,才能进入选择界面. 2. 选择以 ...
- java使用反射给对象属性赋值的两种方法
java反射无所不能,辣么,怎么通过反射设置一个属性的值呢? 主程序: /** * @author tengqingya * @create 2017-03-05 15:54 */ public cl ...
- Salty Fish 结对学习心得体会及创意照 (20165211 20165208)
小组结对学习心得体会及创意照 在阅读了软件工程讲义 3 两人合作(2) 要会做汉堡包和现代软件工程讲义 3 结对编程和两人合作后,加之对于这几周组队学习的感悟,我们对于组队学习的一些感悟和想法如下: ...
- C# 获取当前IIS请求地址
using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...
- python中的迭代器和生成器学习笔记总结
生成器就是一个在行为上和迭代器非常类似的对象. 是个对象! 迭代,顾名思意就是不停的代换的意思,迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次“迭代”,而 ...