P1363 Phantom Labyrinth [search]

Title Description

(LHX and WD meow star who work together to repel the invasion Wang star who, unfortunately, before Wang star who retreat to the illusion they create a maze.)

WD: whining, swollen do ah ......

LHX: momo ... we will be able to go out!

WD: 嗯, + U + U!

Description Description

Phantom labyrinth can be considered infinite, but it consists of a plurality of N * M matrix repeats. '' Matrix in some places the road, with representation; in some places the wall, with the '#' indicates. LHX position where WD and represented by 'S'. I.e. for maze a point (x, y), if (x mod n, y mod m) is or 'S', then place the road '.'; If (x mod n, y mod m) is' # ', then this place is wall. LHX and WD can move in four directions, of course, can not be moved to the wall.

Please tell LHX and WD, whether they can out of the maze of illusion (if they come from a starting point at infinity, we think that we can go out). If not, LHX had to start a program to destroy the castle ...... of course, not a last resort, he did not want to do. . .

Resolve

Obviously, for such a labyrinth infinite, since it has a fractal characteristic, as long as the unit matrix from an arbitrary position of a point to the same position (relative position of the unit matrix) matrix of another unit, then the other point of the matrix you can reach, it means being able to go out.

It looks like the problem is not data matrix card case path to go into the ring, but I do not know nest nest like a long time. . .

For another case, and no ring, if we go back to the location of a once passed through point (relative to the infinite maze), indicating able to go.

Therefore, we must record: come to a position of relative unit matrix of coordinates and relative coordinates the entire maze, and this position is gone.

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define N 1510
using namespace std;
int n,m;
char a[N][N];
int v[3][N][N];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
inline bool dfs(int x,int y,int px,int py)
{
    if(v[0][px][py]&&(v[1][px][py]!=x||v[2][px][py]!=y)) return 1;
    if(v[0][px][py]&&v[1][px][py]==x&&v[2][px][py]==y) return 0;
    v[0][px][py]=1,v[1][px][py]=x,v[2][px][py]=y;
    for(int i=0;i<4;++i){   
        int nx=x+dir[i][0],ny=y+dir[i][1];
        int mx=(px+dir[i][0]+n)%n,my=(py+dir[i][1]+m)%m;//防止数组溢出
        if(a[mx][my]=='#') continue;
        if(dfs(nx,ny,mx,my)) return 1;
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
        memset(v,0,sizeof(v));
        memset(a,0,sizeof(a));
        for(int i=0;i<n;++i)
            scanf("%s",a[i]);
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
                if(a[i][j]=='S'){
                    if(dfs(i,j,i,j)) printf("Yes\n");
                    else printf("No\n");
                    break;
                }
    }
    return 0; 
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11346529.html