【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- [Atcoder Code Festival 2017 Qual B Problem F]Largest Smallest Cyclic Shift
题目大意:给你\(A\)个a,\(B\)个b,\(C\)个c,要你构造一个字符串,使它的最小循环表示法最大.求这个表示法.解题思路:不知道怎么证,但把a.b.c当做单独的字符串扔进容器,每次把字典序最 ...
- Centos 6/7 忘记root密码处理方法
1. centos 6.x 重置root密码 开机按esc 到下图 按 e 键进入编辑模式,通常选择第二项后按 e 键编辑此项 进入该编辑模式后,在quiet后面输入 simple 或者 1 然后回 ...
- 新手学python-Day3-模块
模块就是引入别人写的,官方写的工具库,就像扳手,钳子,电锯
- WEB开发兼容性---浏览器渲染模式—— document.compatMode
document.compatMode主要是用来判断浏览器采用何种方式渲染,它有两种可能的返回值:BackCompat和CSS1Compat,官方对其解释如下: BackCompat:标准兼容模式关闭 ...
- java.lang.RuntimeException: java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
这种情况可能是因为在设置数据库的时候,没有配置数据库的方言,导致sql语句无法被识别. 例如在配置Jfinal的配置文件的时候 如果不配置数据库的方言,默认下它是MySQL的,当使用oracle数据库 ...
- spring-boot-maven-plugin 插件的作用(转)
OM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件.在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一 ...
- Ruby中写换行
Ruby中写换行 print("Hello,\nRuby\n!\n") print("Hello, Ruby ! ") 这两个竟然是一样的:就是说,可以直接回车 ...
- 机器学习之&&Andrew Ng课程复习--- 聚类——Clustering
第十三章.聚类--Clustering ******************************************************************************** ...
- 数据共享之相互排斥量mutex
相互排斥量介绍 相互排斥量能够保护某些代码仅仅能有一个线程运行这些代码.假设有个线程使用相互排斥量运行某些代码,其它线程訪问是会被堵塞.直到这个线程运行完这些代码,其它线程才干够运行. 一个线程在訪问 ...
- 【SPOJ-GSHOP】Rama and Friends【贪心】【细节】
题意: 给出n个非严格递增的整数(可能有负数),必须操作k次.每次能够把当中一个数变为它的相反数,使得终于的数列和最大. 输出这个最大和. 考验怎样出坑数据卡自己的程序... #include < ...