zoukankan      html  css  js  c++  java
  • NOI 题库 1792

    1792  迷宫

    描述
    一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行。同时当Extense处在某个格点时,他只能移动到东南西北(或者说上下左右)四个方向之一的相邻格点上,Extense想要从点A走到点B,问在不走出迷宫的情况下能不能办到。如果起点或者终点有一个不能通行(为#),则看成无法办到。
    输入
    第1行是测试数据的组数k,后面跟着k组输入。每组测试数据的第1行是一个正整数n (1 <= n <= 100),表示迷宫的规模是n * n的。接下来是一个n * n的矩阵,矩阵中的元素为.或者#。再接下来一行是4个整数ha, la, hb, lb,描述A处在第ha行, 第la列,B处在第hb行, 第lb列。注意到ha, la, hb, lb全部是从0开始计数的。
    输出
    k行,每行输出对应一个输入。能办到则输出“YES”,否则输出“NO”。
    样例输入
    2
    3
    .##
    ..#
    #..
    0 0 2 2
    5
    .....
    ###.#
    ..#..
    ###..
    ...#.
    0 0 4 0
    
    样例输出
    YES
    NO
    ———————————————————分割线————————————————————

    #include "bits/stdc++.h"
    
    using namespace std ;
    const int maxN = 1100 ; 
    
    char mp[ maxN ][ maxN ] ;
    bool limit [ maxN ][ maxN ] ;
    
    int destination_x , destination_y ; 
    bool Get_Target ; 
    
    
    void DFS ( const int N , const int xi , const int yi ) {
            if ( Get_Target ) return ;
            if ( xi == destination_x && yi == destination_y ) {
                    Get_Target = true ; 
                    cout << "YES" << endl ; 
                    return ; 
            }
            if ( mp[ xi ][ yi ] == '#' ) return ;
            if ( !limit[ xi ][ yi ] ) return ;
            else {
                    mp[ xi ][ yi ] = '#' ;        
                    if ( limit[ xi - 1 ][ yi ] ) DFS ( N , xi - 1 , yi ) ;
                    if ( limit[ xi + 1 ][ yi ] ) DFS ( N , xi + 1 , yi ) ;
                    if ( limit[ xi ][ yi - 1 ] ) DFS ( N , xi , yi - 1 ) ;
                    if ( limit[ xi ][ yi + 1 ] ) DFS ( N , xi , yi + 1 ) ;
            }
    } 
    
    void Re_Init ( int n ) {
            for ( int i=0 ; i<n ; ++i ) {
                    for ( int j=0 ; j<n ; ++j ) {
                            limit [ i ][ j ] = true ; 
                    }
            }
    }
    
    int main ( ) {
            int T ; 
            scanf ( "%d" , &T ) ;
            while ( T-- ) {
                    int N , start_x , start_y ;
                    Get_Target = false ;
                    scanf ( "%d" , &N ) ;
                    Re_Init ( N ) ;
                    getchar ( ) ;
                    for ( int i=0 ; i<N ; ++i ) {
                            for ( int j=0 ; j<N ; ++j ) {
                                    mp[ i ][ j ] = getchar ( ) ;
                            }
                            getchar ( ) ;
                    }
                    scanf ( "%d%d%d%d" , &start_x , &start_y , &destination_x , &destination_y ) ; 
                    if ( mp[ start_x ][ start_y ] == '#' || mp[ destination_x ][ destination_y ] == '#' ) {cout << "NO" << endl ;continue ;} 
                    DFS ( N , start_x , start_y ) ;
                    if ( !Get_Target ) cout << "NO" << endl ; 
                    memset ( limit , false , sizeof ( limit ) ) ;
            }
            return 0;
    }
    View Code

    2016-10-18 16:57:44

     

    ()

  • 相关阅读:
    BZOJ 3626: [LNOI2014]LCA(树链剖分+离线处理)
    python备用
    STL的使用。。备忘
    DP专题
    任务
    hdu 网络流题集
    hdu KM匹配题集
    hdu 差分约束题集
    hdu 2sat题集
    Codeforces Round #261 (Div. 2)
  • 原文地址:https://www.cnblogs.com/shadowland/p/5973989.html
Copyright © 2011-2022 走看看