【codeforces 196B】Infinite Maze
【题目链接】:http://codeforces.com/problemset/problem/196/B
【题意】
给你一个n*m的棋盘;
然后你能够无限复制这个棋盘;
在这个棋盘上你有一个起点s;
然后问你,你能不能从这个起点s开始一直走无限远;
【题解】
考虑两个不同棋盘上的点(x1,y1),(x2,y2)(即复制出的另外一个棋盘);
假设他们在第一个棋盘上对应的投影x%n,y%m是相同的;
且它们都能由起点S到达;
即S能到达这两个点;
则可知;
可以从S到达其中的一个点(x1,y1);
然后从那个点再到起点,然后再到另外一个棋盘上的(x2,y2),然后再一直重复上述步骤,再回到(x1,y1)再到(x2,y2)..就能无限延伸了;
必要性是靠直觉的吧;
感觉就是要能一直从一个点然后又回到这个点,才能一直扩展嘛;
做法就是dfs(x,y);
看看vis[x%n][y%m]存的是不是(x,y);
不是的话就输出有解;
(vis[x%n][y%m]和(x,y)就是上面对应的(x1,y1)和(x2,y2) )
否则结束这一层递归;
【Number Of WA】
1
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int M = 1500+10;
bool a[M][M],bo[M][M];
pii vis[M][M];
int n,m,x,y;
char s[M];
void out(){
cout <<"Yes"<<endl;
exit(0);
}
void dfs(int x,int y){
int tx = (x%n+n)%n,ty = (y%m+m)%m;
if (!a[tx][ty]) return;
if (bo[tx][ty]){
if (vis[tx][ty].fi !=x || vis[tx][ty].se != y)
out();
return;
}
else
bo[tx][ty] = true,vis[tx][ty] = mp(x,y);
rep1(i,1,4){
int tx = x+dx[i],ty = y+dy[i];
dfs(tx,ty);
}
}
int main(){
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> m;
rep1(i,0,n-1){
cin >>s;
rep1(j,0,m-1){
if (s[j]=='#')
a[i][j] = 0;
else{
a[i][j] = 1;
if (s[j]=='S'){
x = i,y = j;
}
}
}
}
dfs(x,y);
cout <<"No"<<endl;
return 0;
}
【codeforces 196B】Infinite Maze的更多相关文章
- 【CodeForces 622A】Infinite Sequence
题意 一个序列是, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5....这样排的,求第n个是什么数字. 分析 第n个位置属于1到k,求出k,然后n-i*(i-1)/ ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【POJ 3026】Borg Maze
id=3026">[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值 ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 750D】New Year and Fireworks
time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【20.51%】【codeforces 610D】Vika and Segments
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- vue无缝滚动的插件开发填坑分享
写插件的初衷 1.项目经常需要无缝滚动效果,当时写jq的时候用用msClass这个老插件,相对不上很好用. 2.后来转向vue在vue-awesome没有找到好的无缝滚动插件,除了配置swiper可以 ...
- tp框架 JS里面获取session
var var_name="{:session('xxxxx')}"; 用大括号 这个方法可以获取session
- OpenLDAP 2.4.44 安装 + phpLDAPadmin 安装
网上太多的例子搞 OpenLDAP + phpLDAPadmin 但是没有一个能让你成功配置起来.蛋疼得很.记录一下过程 系统版本:centos7.4 软件版本:2.4.44 超级重要,版本不对 ...
- Java採用JNI调用VC++生成的dll(Java与C++交互)
应项目需求,须要android调用java,java再调用C++实现android一个图片匹配的功能,我们作为java组须要和C++和Android进行交互.以下是java和C++採用JNI的方式进行 ...
- JBoss AS 7之文件夹结构(The Return Of The King)
1.2 JBoss As 7体系结构 以下介绍一下JBoss的体系结构,详细的文件夹结构. 假设熟悉曾经JBoss版本号的人,一定会发现JBoss AS 7与之前的JBoss的文件夹结构有了非常大的不 ...
- Java虚拟机的类载入机制
Java虚拟机类载入过程是把Class类文件载入到内存.并对Class文件里的数据进行校验.转换解析和初始化,终于形成能够被虚拟机直接使用的java类型的过程. 在载入阶段,java虚拟机须要完毕下面 ...
- UVA - 1476 Error Curves 三分
Error Curves Josephina is a clever girl and addicted to Machi ...
- c:\Windows\System32\drivers\etc\hosts的作用
c:\Windows\System32\drivers\etc\hosts 是域名解析文件. 可以直接用记事本打开.将IP地址重定向. 格式为:ip地址-空格-域名 可以将一个域名重新定向到一个IP ...
- SSRS参数不能默认全选的解决方法
解决方法选自<SQL Server 2008 R2 Reporting Services 报表服务>一书,亲测有效. 注意:参数默认值如果是字符串需要类型转换 =CStr("AL ...
- 模仿百度首页“元宵节汤圆”动图,并实现360°不停旋转(CSS3的animation动画效果)
模仿百度首页“元宵节汤圆”动图,并实现360°不停旋转(CSS3的animation动画效果) 效果图: 切图地址: https://ss1.bdstatic.com/5eN1bjq8AAUYm2zg ...