luogu_1363 幻想迷宫

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=3020;
int n,m;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
char a[N][N];
bool vis[N][N],vis2[N][N];

bool dfs(int x,int y){
    if(x==-1){
        if(dfs(n*2-1,y))return true;
        return false;
    }
    if(x==n*2){
        if(dfs(0,y))return true;
        return false;
    }
    if(y==-1){
        if(dfs(x,m*2-1))return true;
        return false;
    }
    if(y==m*2){
        if(dfs(x,0))return true;
        return false;
    }
    if(vis[x][y] || a[x%n][y%m]=='#')return false;
    if(vis2[x%n][y%m])return true;
    vis[x][y]=1;
    vis2[x%n][y%m]=1;
    if(dfs(x+1,y))return true;
    if(dfs(x-1,y))return true;
    if(dfs(x,y+1))return true;
    if(dfs(x,y-1))return true;
    return false;
}

int main(){
    while(cin>>n>>m){
        int startx,starty;
        memset(vis,0,sizeof(vis));
        memset(vis2,0,sizeof(vis2));
        memset(a,'#',sizeof(a));
        for(register int i=0;i<n;i++)
            for(register int j=0;j<m;j++){
                cin>>a[i][j];
                if(a[i][j]=='S')startx=i,starty=j;
            }
        if(dfs(startx,starty))printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

  

posted @ 2017-10-10 20:12  wqtnb_tql_qwq_%%%  阅读(92)  评论(0编辑  收藏  举报