以前用迷宫dfs搜索的形参都是m(m = row * 列数 + col),然后依次搜索
*****************************弱************蒟蒻****************************************
船新版本:
(1)形参就是行号和列号
(2)vis[x][y][0]记录该点的访问值的row,vis[x][y][1]记录该点访问值的col,初始化INF
(3)算得modn的行号tx和modm的列号ty
(4)如果该点不能走则返回0
(5)如果该点能走,但访问值不为初始值INF,即已经走过了
若该点的访问值为x和y,那么返回0
若该点的访问值不为x和y,那么则认为到达了无穷远,返回1
(6)否则记录访问值,继续从4个方向扩展
*********************************************************************************************
//dfs搜迷宫船新版本
#include <cstdio>
using namespace std;
const int maxn = 2000;
const int INF = 99999999;
int n, m, S;
int vis[maxn][maxn][2];
int a[maxn][maxn];
char str[maxn];
int dx[4]={-1, 1, 0, 0};
int dy[4]={0, 0, -1, 1};
bool dfs(int x, int y)
{//如果一个位置的访问值不等于当前x和y的值则到达无穷大
int tx = (x % n + n) % n;
int ty = (y % m + m) % m;
if(a[tx][ty]) return 0;
if(vis[tx][ty][0] != INF)
return vis[tx][ty][0]!=x || vis[tx][ty][1]!=y;
vis[tx][ty][0] = x;//访问值记录
vis[tx][ty][1] = y;
for(int i = 0; i < 4; ++i)
if(dfs(x+dx[i], y+dy[i]))
return 1;
return 0;
}
int main()
{
while(~scanf("%d %d", &n, &m))
{
int i, j, sx, sy;
for(i = 0; i < n; ++i)
{
scanf("%s", str);
for(j = 0; j < m; ++j)
{
vis[i][j][0] = INF;//初始值
if(str[j] == 'S')
{
a[i][j] = 0;
sx = i;
sy = j;
}
else if(str[j] == '#') a[i][j] = 1;
else a[i][j] = 0;
}
}
if(dfs(sx, sy)) printf("Yes\n");
else printf("No\n");
}
return 0;
}