zoukankan      html  css  js  c++  java
  • [LuoguP1363]幻想迷宫

    [LuoguP1363]幻想迷宫(Link

    现在有一个迷宫,从迷宫边界的任意一点可以走到对面,即:若都是路面,则可以从((1, i))走到((N, i))。其余情况依旧。问是否可以从指定的起点((Sx, Sy))走到无限远的距离。

    简化题目中说了什么叫做走到无限远的距离。那么第一种思路就是计算如果某一个边界点可以到达与之相对应的边界点,那么我们就说可以走到无限远的距离。但是仔细思考之后我们发现这个显然不行,因为它没有考虑在路途中可以进行边界传送。列如下面的样例:

    Pic

    我们从起点开始向右走一个距离,然后接着往上也是可以走的,但是上述的方法就不会考虑。因此这么搜显然是不对的。那么我们考虑建立一个新的图:将原图扩大(8)块。也就是变成了:

    Pic2

    那么我们就知道怎么搞了。按照原来的思路我们从起点开始深搜,然后碰到一个点,他不是原图中的点,但是还原到原图的位置时,我们发现它已经走过了,那么就说明可以走到无限远。但是如果我们是要一直往上走3次,4次呢?那么直接存图显然就是不可取的,既然是有可能无限,那么想到唯一不会爆内存并且可行的方法就是取模。

    我们在搜索的时候记录(4)个变量分别为(X, Y, TrueX, TrueY)。后两个表示真实坐标,前两个表示还原到原图中的坐标位置,也就是取模后的。

    当我们走到一个点的时候如果我们发现(Visit[X][Y] == 1)并且(X != TrueX|| Y != TrueY),那么说明这个点被走了第二遍。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std ;
    typedef long long LL ;
    const int MAXN = 1510 ;
    const int MAXM = 1510 ;
    const int Inf = 0x7fffffff ;
    int N, M, Map[MAXN][MAXM], Sx, Sy ;
    struct VISIT{
    	int V, X, Y ;
    }F[MAXN][MAXM] ;
    bool Gone ;
    int Dx[5] = {0, 1, -1, 0, 0} ;
    int Dy[5] = {0, 0, 0, 1, -1} ;
    
    inline int Read() {
    	int X = 0, F = 1 ; char ch = getchar() ;
    	while (ch > '9' || ch < '0') F = (ch == '-' ? - 1 : 1), ch = getchar() ;
    	while (ch >= '0' && ch <= '9') X=(X<<1)+(X<<3)+(ch^48), ch = getchar() ;
    	return X * F ;
    }
    
    inline void Dfs(int X, int Y, int TrueX, int TrueY) {	
    	if (Gone) return ;
    	if (F[X][Y].V && ((F[X][Y].X != TrueX) || (F[X][Y].Y != TrueY))) {
    		Gone = 1 ; return ;
    	}
    	F[X][Y].V = 1 ; F[X][Y].X = TrueX ; F[X][Y].Y = TrueY ;
    	for (int i = 1 ;  i <= 4 ; i ++) {
    		int NextX = (X + Dx[i] + N - 1) % N + 1;
    		int NextY = (Y + Dy[i] + M - 1) % M + 1;
    		int NextXX = TrueX + Dx[i] ;
    		int NextYY = TrueY + Dy[i] ;
    		if (Map[NextX][NextY]) {
    			if (! F[NextX][NextY].V || F[NextX][NextY].X != NextXX || F[NextX][NextY].Y != NextYY)
    				Dfs(NextX, NextY, NextXX, NextYY) ;
    		}
    	}
    }
    
    int main() {
    	while (cin >> N >> M) {
    		memset(Map, 0, sizeof(Map)) ;
    		memset(F, 0, sizeof(F)) ;
    		Gone = 0 ;
    		for (int i = 1 ; i <= N ; i ++)
    		for (int j = 1 ; j <= M ; j ++) {
    			char ch ; cin >> ch ;
    			if (ch == '#') continue ;
    			else Map[i][j] =  1 ;
    			if (ch == 'S') Sx = i, Sy = j ;
    		} 
    		Dfs(Sx, Sy, Sx, Sy) ;
    		puts(Gone ? "Yes" : "No") ;
    	}
    }
    
  • 相关阅读:
    HDOJ 5090 Game with Pearls 二分图匹配
    hdu4360 spfa+分割点
    分布式高级(十三)Docker Container之间的数据共享
    [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
    [Ramda] Create an Array From a Seed Value with Ramda's unfold
    [Flow] Declare types for application
    [Flow] The Fundamentals of Flow
    [Angular] Some performance tips
    [Ramda] Rewrite if..else with Ramda ifElse
    [SVG] Add an SVG as an Embedded Background Image
  • 原文地址:https://www.cnblogs.com/sue_shallow/p/P1363.html
Copyright © 2011-2022 走看看