传送门:QAQ
题意:给你一张图,让你从图的起点开始走,走过的地方会崩塌,但是会有相同的图可以拼接起来,问你是否可以在这些途中无限走。
思路:如果在一张新图上走到之前旧图走过的点,那就可以无限走了。
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
char ch[1510][1510];
int dx[5] = { 1,0,-1,0 };
int dy[5] = { 0,1,0,-1 };
int xx, yy;
int n, m;
int fla;
struct inst {
int x;
int y;
inst() {}
inst(int x, int y) :x(x), y(y) {}
};
inst vis[1510][1510];
void bfs() {
queue<inst>q;
q.push(inst(xx, yy));
vis[xx][yy].x = xx;
vis[xx][yy].y = yy;
while (!q.empty()) {
inst nd = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x1 = nd.x+dx[i];
int y1 = nd.y+dy[i];
int x2 = x1%n;
int y2 = y1%m;
if (x2 < 0) x2 += n;
if (y2 < 0) y2 += m;
if (vis[x2][y2].x == -inf && vis[x2][y2].y == -inf && ch[x2][y2] != '#') {
vis[x2][y2].x = x1;
vis[x2][y2].y = y1;
q.push(inst(x1, y1));
//printf("%d %d\n", x1, y1);
}
else if ((vis[x2][y2].x != x1||vis[x2][y2].y != y1)&&ch[x2][y2] != '#' && (x1<0 || x1 >= n || y1<0 || y1 >= m)) {
fla = 1;
return;
}
}
}
return;
}
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++) {
for (int z = 0; z <= m; z++) {
vis[i][z].x = vis[i][z].y = -inf;
}
}
fla = 0;
for (int i = 0; i < n; i++) {
scanf("%s", ch[i]);
for (int z = 0; z < m; z++) {
if (ch[i][z] == 'S') {
xx = i;
yy = z;
}
}
}
bfs();
if (fla) {
printf("Yes\n");
}
else printf("No\n");
return 0;
}