zoukankan      html  css  js  c++  java
  • OpenJudge 2790 迷宫

    1.链接地址:

    http://bailian.openjudge.cn/practice/2790/

    2.题目:

    总时间限制:
    3000ms
    内存限制:
    65536kB
    描述
    一天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
    

    3.思路:广搜,注意起点和终点如果不通则直接为NO

    4.代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <queue>
     4 
     5 using namespace std;
     6 
     7 struct POINT
     8 {
     9     int x;
    10     int y;
    11 };
    12 
    13 int idx_x[] = {-1,0,1,0};
    14 int idx_y[] = {0,1,0,-1};
    15 
    16 int main()
    17 {
    18     //freopen("C://input.txt","r",stdin);
    19 
    20     int i,j;
    21 
    22     int k;
    23     cin >> k;
    24 
    25     while(k--)
    26     {
    27         int n;
    28         cin >> n;
    29 
    30         bool **arr = new bool*[n];
    31         for(i = 0; i < n; ++i) arr[i] = new bool[n];
    32 
    33         char ch;
    34         for(i = 0; i < n; ++i)
    35         {
    36             for(j = 0; j < n; ++j)
    37             {
    38                 cin >> ch;
    39                 if(ch == '.') arr[i][j] = true;
    40                 else arr[i][j] = false;
    41             }
    42         }
    43 
    44         int ha,la,hb,lb;
    45         cin >> ha >> la >> hb >> lb;
    46 
    47         if(arr[ha][la] == false || arr[hb][lb] == false) {cout << "NO" << endl; continue;}
    48         queue<POINT> q_point;
    49         POINT point;
    50         point.x = la;
    51         point.y = ha;
    52         q_point.push(point);
    53         arr[point.y][point.x] = false;
    54 
    55         bool flag = false;
    56         while(!q_point.empty())
    57         {
    58             point = q_point.front();
    59             q_point.pop();
    60             for(i = 0; i < 4; ++i)
    61             {
    62                 POINT p2;
    63                 p2.x = point.x + idx_x[i];
    64                 p2.y = point.y + idx_y[i];
    65 
    66                 if(p2.y  == hb && p2.x == lb) {flag = true;break;}
    67                 else if(p2.x >= 0 && p2.x < n && p2.y >=0 && p2.y < n && arr[p2.y][p2.x])
    68                 {
    69                     q_point.push(p2);
    70                     arr[p2.y][p2.x] = false;
    71                 }
    72             }
    73             if(i < 4) break;
    74         }
    75         if(flag) cout << "YES" << endl;
    76         else cout << "NO" << endl;
    77         
    78 
    79         for(i = 0; i < n; ++i) delete [] arr[i];
    80         delete [] arr;
    81     }
    82 
    83     return 0;
    84 }
  • 相关阅读:
    tcpprep 对IPV6的支持
    the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)
    servlet service() for servlet jsp throws null pointer exception
    tomcat开机启动
    mysql 允许远程访问
    spring的helloworld
    java中的那些坑
    关于struts2中的相对路径与绝对路径
    Powercenter Source Filter
    oracle删除当前用户的表
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3558465.html
Copyright © 2011-2022 走看看